commit bdbe04ec0db077c93374dc635be68d1909aa27a5 Author: eslusarz Date: Sat May 9 20:15:54 2026 +0200 first commit diff --git a/.metadata/.lock b/.metadata/.lock new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.lock_info b/.metadata/.lock_info new file mode 100644 index 0000000..2a95543 --- /dev/null +++ b/.metadata/.lock_info @@ -0,0 +1,4 @@ +#Sat May 02 22:23:18 CEST 2026 +host=eslusarzs-MacBook-Pro.local +process-id=5824 +user=eslusarz diff --git a/.metadata/.log b/.metadata/.log new file mode 100644 index 0000000..5cc98e4 --- /dev/null +++ b/.metadata/.log @@ -0,0 +1,140 @@ +!SESSION 2026-05-02 15:49:09.790 ----------------------------------------------- +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-02 15:49:11.538 +!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized. + +!ENTRY ch.qos.logback.classic 1 0 2026-05-02 15:49:21.417 +!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-02 17:06:49.880 +!MESSAGE AbortCompilation computing hover information in VettoreOrdinabile.java at offset 1115 +!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 43 : +----- + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int |dimensione() { +----- + at org.eclipse.jdt.internal.ui.util.SelectionUtil.logException(SelectionUtil.java:157) + at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:178) + ... 3 more +!SESSION 2026-05-02 18:59:47.521 ----------------------------------------------- +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-02 18:59:49.086 +!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized. + +!ENTRY org.eclipse.core.resources 2 10035 2026-05-02 19:00:14.060 +!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes. + +!ENTRY ch.qos.logback.classic 1 0 2026-05-02 19:00:14.351 +!MESSAGE Logback config file: /Users/eslusarz/eclipse-workspace/.metadata/.plugins/org.eclipse.m2e.logback/logback.2.7.101.20251017-1242.xml +!SESSION 2026-05-02 22:23:14.251 ----------------------------------------------- +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-02 22:23:15.125 +!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized. + +!ENTRY ch.qos.logback.classic 1 0 2026-05-02 22:23:18.713 +!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.core 4 4 2026-05-08 17:27:30.428 +!MESSAGE Could not retrieve declared methods +!STACK 0 +java.lang.NullPointerException: Cannot store to object array because "this.types[this.typeid]" is null + at org.eclipse.jdt.internal.compiler.lookup.TypeSystem.getWildcard(TypeSystem.java:411) + at org.eclipse.jdt.internal.compiler.lookup.AnnotatableTypeSystem.getWildcard(AnnotatableTypeSystem.java:207) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeFromVariantTypeSignature(LookupEnvironment.java:2344) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeArgumentsFromSignature(LookupEnvironment.java:1986) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.getTypeFromTypeSignature(LookupEnvironment.java:2274) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.createMethod(BinaryTypeBinding.java:1005) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.createMethods(BinaryTypeBinding.java:1165) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.cachePartsFrom2(BinaryTypeBinding.java:601) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.cachePartsFrom(BinaryTypeBinding.java:442) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:1148) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.createBinaryTypeFrom(LookupEnvironment.java:1129) + at org.eclipse.jdt.internal.compiler.Compiler.accept(Compiler.java:183) + at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:338) + at org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding.resolve(UnresolvedReferenceBinding.java:116) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:245) + at org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.resolve(ParameterizedTypeBinding.java:1210) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveType(BinaryTypeBinding.java:216) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.resolveTypesFor(BinaryTypeBinding.java:1891) + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.methods(BinaryTypeBinding.java:1802) + at org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding.methods(ParameterizedTypeBinding.java:1128) + at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.availableMethods(ReferenceBinding.java:289) + at org.eclipse.jdt.core.dom.TypeBinding.getDeclaredMethods(TypeBinding.java:279) + at org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer.addInherited(ScopeAnalyzer.java:222) + at org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer.addInherited(ScopeAnalyzer.java:206) + at org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer.getDeclarationsInScope(ScopeAnalyzer.java:401) + at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.collectMethodProposals(UnresolvedElementsBaseSubProcessor.java:1455) + at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.getMethodProposals(UnresolvedElementsSubProcessor.java:155) + at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.process(QuickFixProcessor.java:398) + at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.getCorrections(QuickFixProcessor.java:370) + at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionCollector.safeRun(JavaCorrectionProcessor.java:381) + at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:341) + at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47) + at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.process(JavaCorrectionProcessor.java:336) + at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectCorrections(JavaCorrectionProcessor.java:465) + at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.getJavaAnnotationFixes(ProblemHover.java:115) + at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.computeCompletionProposals(ProblemHover.java:89) + at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.(ProblemHover.java:74) + at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover.createAnnotationInfo(ProblemHover.java:178) + at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover.getHoverInfo2(AbstractAnnotationHover.java:957) + 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.RuntimeException: RuntimeException loading =asdl/\/Users\/eslusarz\/.p2\/pool\/plugins\/org.eclipse.justj.openjdk.hotspot.jre.full.macosx.aarch64_21.0.10.v20260205-0638\/jre\/lib\/jrt-fs.jar`java.base=/javadoc_location=/https:\/\/docs.oracle.com\/en\/java\/javase\/21\/docs\/api\/=/=/module=/true=/|java/util/Comparator.class + at org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding.cachePartsFrom(BinaryTypeBinding.java:446) + ... 34 more diff --git a/.metadata/.mylyn/repositories.xml.zip b/.metadata/.mylyn/repositories.xml.zip new file mode 100644 index 0000000..b73fdbe Binary files /dev/null and b/.metadata/.mylyn/repositories.xml.zip differ diff --git a/.metadata/.plugins/org.eclipse.buildship.core/gradle/versions.json b/.metadata/.plugins/org.eclipse.buildship.core/gradle/versions.json new file mode 100644 index 0000000..4be6b3d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.buildship.core/gradle/versions.json @@ -0,0 +1,8288 @@ +[ { + "version" : "9.6.0-20260502004807+0000", + "buildTime" : "20260502004807+0000", + "commitId" : "b6e68b2bcae07ceceb366b4d0a900e4e93ba4c1d", + "current" : false, + "snapshot" : true, + "nightly" : true, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-bin.zip.sha256", + "checksum" : "85e34aa0a33686cfac1fb2e0ca79c6e4929be140ec8dae7956fb6b84241270b5", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260502004807+0000-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0", + "buildTime" : "20260428120530+0000", + "commitId" : "3fe117d68f3907790f3809f121aa36303a9151f8", + "current" : true, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-bin.zip.sha256", + "checksum" : "553c78f50dafcd54d65b9a444649057857469edf836431389695608536d6b746", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0-20260428014943+0000", + "buildTime" : "20260428014943+0000", + "commitId" : "3fe117d68f3907790f3809f121aa36303a9151f8", + "current" : false, + "snapshot" : true, + "nightly" : false, + "releaseNightly" : true, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-bin.zip.sha256", + "checksum" : "95be4dddffc251cf96cb85e6b7d5b1cf9ac7aa407c12c0b1ef212d8592c98ad0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.5.0-20260428014943+0000-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0-rc-4", + "buildTime" : "20260423134125+0000", + "commitId" : "0ad6dd8e143455707e444aa7e3d38327a3366513", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-4-bin.zip.sha256", + "checksum" : "cfbecb094daf3caab652ba49c80be5b5499f00ff8a27316a9aa7da8258580bb3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.6.0-milestone-1", + "buildTime" : "20260421092316+0000", + "commitId" : "aec782af05a4c94a3dec185903ef0b45acf99892", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.6.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-1-bin.zip.sha256", + "checksum" : "8bf1cd7dcadb6d5de70aecf4548465c05973a2a3db8063c72db667314405bee3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0-rc-3", + "buildTime" : "20260417122400+0000", + "commitId" : "0a75a301d6307692e72dd5010ae559e5422188a1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-3-bin.zip.sha256", + "checksum" : "13329f9b478ea0c74f80da2d4a02cdf09aaed679167f3e6d57f4067c0442374d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0-rc-2", + "buildTime" : "20260409133555+0000", + "commitId" : "b5a7d7a5cf90fe2ff4b78fc920fc0ea137991377", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-2-bin.zip.sha256", + "checksum" : "f0effd0ab258b23da888a94cae8602447f8824c65a61dc14e4014f9acdb09950", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.5.0-rc-1", + "buildTime" : "20260330120715+0000", + "commitId" : "6a1704c113de068f7e9a6744245c7eb4bc5091d0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-1-bin.zip.sha256", + "checksum" : "66d79b10eb939c954bf1ac3be9d9cde985301b56058d49542286c35782ae1e74", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7" +}, { + "version" : "9.4.1", + "buildTime" : "20260319084628+0000", + "commitId" : "2d6327017519d23b96af35865dc997fcb544fb40", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.1-bin.zip.sha256", + "checksum" : "2ab2958f2a1e51120c326cad6f385153bb11ee93b3c216c5fccebfdfbb7ec6cb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.1-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.5.0-milestone-7", + "buildTime" : "20260315084051+0000", + "commitId" : "812c56a2fbbd51caf1865e37dbfb8e169137ab13", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-7-bin.zip.sha256", + "checksum" : "be0a8f2e9f1c0f4d4764cc8903afcbce6c1134df8b980ae517583b18a0fce57b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-7-wrapper.jar.sha256", + "wrapperChecksum" : "7ef3d73bd95c047814d76ec8324f72deefb96593eb9ce87aa06ecdcdaba7ffe8" +}, { + "version" : "9.4.0", + "buildTime" : "20260304103600+0000", + "commitId" : "b631911858264c0b6e4d6603d677ff5218766cee", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-bin.zip.sha256", + "checksum" : "60ea723356d81263e8002fec0fcf9e2b0eee0c0850c7a3d7ab0a63f2ccc601f3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.4.0-rc-2", + "buildTime" : "20260227092055+0000", + "commitId" : "b5fe9efed6cae7b9f2fbdb2d380fb69af16bb752", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.4.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-2-bin.zip.sha256", + "checksum" : "1386475e18bc4b8f885d9573b1194b44462e7ab2b9afb6d70ad9f39cdfaf2d6e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.5.0-milestone-6", + "buildTime" : "20260306150512+0000", + "commitId" : "56c1a30b99058762ff8e655058dcf0404e20d0ad", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-6-bin.zip.sha256", + "checksum" : "b159ac78e31ec3ac1ea6c32457b136c0d6d85c8bde533054d919be768d205866", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-6-wrapper.jar.sha256", + "wrapperChecksum" : "7ef3d73bd95c047814d76ec8324f72deefb96593eb9ce87aa06ecdcdaba7ffe8" +}, { + "version" : "9.5.0-milestone-5", + "buildTime" : "20260227090120+0000", + "commitId" : "7928ff898563f1f324eb29a5b1d40d43798fe992", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-5-bin.zip.sha256", + "checksum" : "1bacc1c689e2c1e109208ce941a7695d2edeba5cbfc4bc2a99524147c02e6e35", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-5-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.5.0-milestone-4", + "buildTime" : "20260226092909+0000", + "commitId" : "84fcf1dafd5cd2c8502c58c2fdbbca9bb2468257", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-4-bin.zip.sha256", + "checksum" : "56d75507accf46250ab19d11eb222bcb72a2a8bc70a7abea23c4edbaa7f2b2ad", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-4-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.5.0-milestone-3", + "buildTime" : "20260216080935+0000", + "commitId" : "8e03e4fe12db77236cde88285c9fc04f4e89d0c1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-3-bin.zip.sha256", + "checksum" : "3721d2a837d9836a104a5e47fcf644fafed81eb14f4405ecebf9044d6a44a2c4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.5.0-milestone-2", + "buildTime" : "20260211140745+0000", + "commitId" : "c6762aa64d21254851c842b7d89f9d08e33a5344", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-2-bin.zip.sha256", + "checksum" : "afcaf8f53216d213ffca406d913fd651678397ff1ff9a0e0cd165c55c35c1954", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.4.0-rc-1", + "buildTime" : "20260208025655+0000", + "commitId" : "a2da7f311fe4699328dbcef381bc459c2f757e3e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.4.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-1-bin.zip.sha256", + "checksum" : "2341e5f62ce4ce9d1f51395b47428a6d53e972bc0a3f9d2bf2da5a294610c703", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.3.1", + "buildTime" : "20260129141501+0000", + "commitId" : "44f4e8d3122ee6e7cbf5a248d7e20b4ca666bda3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.1-bin.zip.sha256", + "checksum" : "b266d5ff6b90eada6dc3b20cb090e3731302e553a27c5d3e4df1f0d76beaff06", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.1-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.5.0-milestone-1", + "buildTime" : "20260129132921+0000", + "commitId" : "6e1fb6d39530a68fd3d52eb274324eab142baa5b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-1-bin.zip.sha256", + "checksum" : "aa937f5607d8729dbcd99cfb923c73cf7d54051cb1d73be41566e3865c876269", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "8.14.4", + "buildTime" : "20260123163023+0000", + "commitId" : "ad5ff774b4b0e9a8a0cf1a14ca70d7230003c3ad", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.4-bin.zip.sha256", + "checksum" : "f1771298a70f6db5a29daf62378c4e18a17fc33c9ba6b14362e0cdf40610380d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.4-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "9.4.0-milestone-5", + "buildTime" : "20260123123336+0000", + "commitId" : "dd2cf7f3826b2da07d8d6de488a2d1bc5651ca7d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-5-bin.zip.sha256", + "checksum" : "9426691ebe1a0db4729a181d86b38234b44ed48a013b4f106fa090eacb13c2eb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-5-wrapper.jar.sha256", + "wrapperChecksum" : "55243ef57851f12b070ad14f7f5bb8302daceeebc5bce5ece5fa6edb23e1145c" +}, { + "version" : "9.3.0", + "buildTime" : "20260116111422+0000", + "commitId" : "701205ed2f78811508466c8e1952304c2ea869f5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-bin.zip.sha256", + "checksum" : "0d585f69da091fc5b2beced877feab55a3064d43b8a1d46aeb07996b0915e0e0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.3.0-rc-3", + "buildTime" : "20260112110023+0000", + "commitId" : "8dd290bb13b269e80427d1c5ac5ba071dd77c211", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.3.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-3-bin.zip.sha256", + "checksum" : "d18ae4e38c4572d1e1546d585110c807f13476223e063b40324f891bf7b5e39c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.4.0-milestone-4", + "buildTime" : "20251226090225+0000", + "commitId" : "5478a4a62071a9f97660cb5f99abac82cb85eab7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-4-bin.zip.sha256", + "checksum" : "599f4cb180b3780ca83859e84b072b951156f0af4554d7d21654cc4429c47284", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-4-wrapper.jar.sha256", + "wrapperChecksum" : "08f6dc44dc11cd44fdf485274f3262acd459acbcbc3c1160da2d530cecc9d1ad" +}, { + "version" : "9.3.0-rc-2", + "buildTime" : "20251224090548+0000", + "commitId" : "02c7d66609684db888d7324300770ad59c295291", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.3.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-2-bin.zip.sha256", + "checksum" : "a3c1f84c0519f57a3cf6b0c8f9e2a80ece6e75aadb554ead218a4c71d4f2a901", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.4.0-milestone-3", + "buildTime" : "20251202075550+0000", + "commitId" : "8d9cbbb408ff3c4cd499ac46c358cff1fc490227", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-3-bin.zip.sha256", + "checksum" : "2c2402e0741735bf96742d834ccc236d1b6ebef8f3b249df059a0f19cce07efc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.3.0-rc-1", + "buildTime" : "20251128163838+0000", + "commitId" : "7103de78a66088931f83b1eea19b299af989c114", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.3.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-1-bin.zip.sha256", + "checksum" : "e03f4a72f4fa7160823cf9d602c8e06f9be818c454536bdf13739fe96902c21c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.3.0-milestone-2", + "buildTime" : "20251126100558+0000", + "commitId" : "590ab5b91ff01acdafce939cdf6a6c8a21c0760a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.3.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-2-bin.zip.sha256", + "checksum" : "aefd1d28321c1c5f700f11c697acfb7d0318ae88f3f1b71d0ecbdadfd7eadbb6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.4.0-milestone-2", + "buildTime" : "20251121214025+0000", + "commitId" : "9fe2118fed52d1925a19acf16fed9be083cd449a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-2-bin.zip.sha256", + "checksum" : "279f47796403c4dc7660a4e126948ba9813fa0ae776762f1b28f32b4fce46625", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.4.0-milestone-1", + "buildTime" : "20251119221522+0000", + "commitId" : "ad03b1c1840b420d7bd866835e399610c2f31510", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-1-bin.zip.sha256", + "checksum" : "12d4bad5c207a83f8a28a9939bafead9bf37225b3cb2a15c774422eaa5246419", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.4.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "b3a875ddc1f044746e1b1a55f645584505f4a10438c1afea9f15e92a7c42ec13" +}, { + "version" : "9.2.1", + "buildTime" : "20251117134048+0000", + "commitId" : "30ecdc708db275e8f8769ea0620f6dd919a58f76", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.1-bin.zip.sha256", + "checksum" : "72f44c9f8ebcb1af43838f45ee5c4aa9c5444898b3468ab3f4af7b6076c5bc3f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.3.0-milestone-1", + "buildTime" : "20251104131839+0000", + "commitId" : "d28b435db30c93c2501d0434266a9026fc4d2483", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.3.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-1-bin.zip.sha256", + "checksum" : "b53d70b79bc4652f66e7f85a50b8a12537171ddee64b3fdc761921bde7761c19", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.3.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "d652a0436e78e0cd4d00f52057fcf2179163762a71a02370ec7623ab8f84c334" +}, { + "version" : "9.2.0", + "buildTime" : "20251029135323+0000", + "commitId" : "d9d6bbce03b3d88c67ef5a0ff31f7ae5e332d6bf", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-bin.zip.sha256", + "checksum" : "df67a32e86e3276d011735facb1535f64d0d88df84fa87521e90becc2d735444", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.2.0-rc-3", + "buildTime" : "20251024101831+0000", + "commitId" : "8a6b6c9294de54f1eae1b62e4155ee02153f1f6c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.2.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-3-bin.zip.sha256", + "checksum" : "8f29578613e216bde733f1a160076ae7b889b46f9802c731e20f0c43c4586fae", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.2.0-rc-2", + "buildTime" : "20251017103705+0000", + "commitId" : "2759e2f4d683d6d48b2da682aee1ed15d5242462", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.2.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-2-bin.zip.sha256", + "checksum" : "054e3862d342ba9965127f80e1c56048bff2228e6d548ddd604b739c329893af", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.2.0-rc-1", + "buildTime" : "20251003125514+0000", + "commitId" : "17156c692b5e460d4e68b0b4342c0c7872a4a6d9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.2.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-1-bin.zip.sha256", + "checksum" : "2fdde961d051e8fc4c9918aa40d3eee24fd9f1d32f705f517af9c89d758af3be", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.1.0", + "buildTime" : "20250918130556+0000", + "commitId" : "e45a8dbf2470c2e2474ccc25be9f49331406a07e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-bin.zip.sha256", + "checksum" : "a17ddd85a26b6a7f5ddb71ff8b05fc5104c0202c6e64782429790c933686c806", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.2.0-milestone-2", + "buildTime" : "20250918081430+0000", + "commitId" : "136322a57afae21682616403597aeb7b900277a2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.2.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-2-bin.zip.sha256", + "checksum" : "264492cfe4471ed1290a23af27a94b45343bb548b40f6512a130f4e416ffdc1b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.2.0-milestone-1", + "buildTime" : "20250916093851+0000", + "commitId" : "fdecc3c95828bb9a1c1bb6114483fe5b16f9159d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.2.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-1-bin.zip.sha256", + "checksum" : "b4d2b27513235176e11a079aa8afa5a1d858c14a02130cc9754b20071d244fa7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.2.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "423cb469ccc0ecc31f0e4e1c309976198ccb734cdcbb7029d4bda0f18f57e8d9" +}, { + "version" : "9.1.0-rc-4", + "buildTime" : "20250915153701+0000", + "commitId" : "769a0dee8eb81a087a64b7286b5e0ef3ca945723", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-4-bin.zip.sha256", + "checksum" : "91b3bb52f09bab37df8f922d47d7df6ba76c00a52494a75feafad956f4d2a360", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.1.0-rc-3", + "buildTime" : "20250908083750+0000", + "commitId" : "e8b16b23493d8480116e5cf66a06296794c3d1f5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-3-bin.zip.sha256", + "checksum" : "6f13d82a9943498b6651a1d8b9903a663dee9a752be8e3480b6c44f0c89d9dac", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.1.0-rc-2", + "buildTime" : "20250902143701+0000", + "commitId" : "80fea90a8e4c089ceb397f17f935538c00398324", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-2-bin.zip.sha256", + "checksum" : "366fb4c50aa898723a9d5f07a164d4134eb264c1749a07766819be8649792be9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.1.0-rc-1", + "buildTime" : "20250813123732+0000", + "commitId" : "d99e08080200de174ff11b3a06689d996d338ad7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-1-bin.zip.sha256", + "checksum" : "f80a20ef870555c9aae51c4d1868b6a31608296a0fd4f8de69a2687243b90f6f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.1.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.0.0", + "buildTime" : "20250731163512+0000", + "commitId" : "328772c6bae126949610a8beb59cb227ee580241", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-bin.zip.sha256", + "checksum" : "8fad3d78296ca518113f3d29016617c7f9367dc005f932bd9d93bf45ba46072b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.0.0-rc-4", + "buildTime" : "20250728151002+0000", + "commitId" : "a0a6deb6f9937f9e9894a4460f4158267a13ad03", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.0.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-4-bin.zip.sha256", + "checksum" : "f09991ce63e889bf8a5f579a467a82bdf7621bc93d59cd2f8fe5982f39f28e2a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.0.0-rc-3", + "buildTime" : "20250717124800+0000", + "commitId" : "2db9560bb68c367a265b10516c856c840f9bed8d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.0.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-3-bin.zip.sha256", + "checksum" : "19ce31d8a4f2e59a99931cc13834c70c0e502804851c0640f31a1af9a7d5b003", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.0.0-rc-2", + "buildTime" : "20250711152331+0000", + "commitId" : "2ae39f19925c59560b0b708b9eda586857cddb81", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.0.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-2-bin.zip.sha256", + "checksum" : "4223eafcc72c463e99bad60333a90c974a89aa359ee683e4f329330e513a3736", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "9.0.0-milestone-10", + "buildTime" : "20250708052607+0000", + "commitId" : "fd90cd1f4415bd77322abacfd66147724c432cde", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-10-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-10-bin.zip.sha256", + "checksum" : "0e2ac1877ee872cf65c2d8aa6a6aefcfd57226421d54c29937d21f492fdbde86", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-10-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "8.14.3", + "buildTime" : "20250704131544+0000", + "commitId" : "e5ee1df3d88b8ca3a8074787a94f373e3090e1db", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.3-bin.zip.sha256", + "checksum" : "bd71102213493060956ec229d946beee57158dbd89d0e62b91bca0fa2c5f3531", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.3-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "7.6.6", + "buildTime" : "20250704103426+0000", + "commitId" : "c0be2570bd17666c8955ca5b66363f671f50bbd7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.6-bin.zip.sha256", + "checksum" : "673d9776f303bc7048fc3329d232d6ebf1051b07893bd9d11616fad9a8673be0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.6-wrapper.jar.sha256", + "wrapperChecksum" : "14dfa961b6704bb3decdea06502781edaa796a82e6da41cd2e1962b14fbe21a3" +}, { + "version" : "9.0.0-rc-1", + "buildTime" : "20250618075324+0000", + "commitId" : "8493ac58b4a7baddb64080d415b11659be78dbea", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "9.0.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-1-bin.zip.sha256", + "checksum" : "5086034b0656a79fee07a04cfadc103984b075ac85d3a20748649671283be1bb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "8.14.2", + "buildTime" : "20250605133201+0000", + "commitId" : "30db2a3bdfffa9f8b40e798095675f9dab990a9a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.2-bin.zip.sha256", + "checksum" : "7197a12f450794931532469d4ff21a59ea2c1cd59a3ec3f89c035c3c420a6999", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.2-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "7.6.5", + "buildTime" : "20250604130222+0000", + "commitId" : "13a09d0cc61025596acd4acf05d1980a79838906", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.5-bin.zip.sha256", + "checksum" : "b812fec0edb7d27e0ae35955887bb2954536fa3e44edaf481150da058e154d9a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.5-wrapper.jar.sha256", + "wrapperChecksum" : "14dfa961b6704bb3decdea06502781edaa796a82e6da41cd2e1962b14fbe21a3" +}, { + "version" : "9.0.0-milestone-9", + "buildTime" : "20250526083131+0000", + "commitId" : "1ceb6a55a8b9f772540ea2655f7ec71cf71e773d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-9-bin.zip.sha256", + "checksum" : "f682abba2db33587373c58f558c9387b25f7ea592298b57defbf33fc5488c43d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-9-wrapper.jar.sha256", + "wrapperChecksum" : "76805e32c009c0cf0dd5d206bddc9fb22ea42e84db904b764f3047de095493f3" +}, { + "version" : "8.14.1", + "buildTime" : "20250522134409+0000", + "commitId" : "c174b82566a79e3575bac8c7648c7b36cd815e94", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.1-bin.zip.sha256", + "checksum" : "845952a9d6afa783db70bb3b0effaae45ae5542ca2bb7929619e8af49cb634cf", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.1-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "9.0.0-milestone-8", + "buildTime" : "20250516073511+0000", + "commitId" : "2de7d5ba412c2a9e33f43076dfc4560488359ad2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-8-bin.zip.sha256", + "checksum" : "9a69d11f90bb8b689b171889437d99a5925d17e7507413472ab45fbbc3949ef9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0.0-milestone-8-wrapper.jar.sha256", + "wrapperChecksum" : "f98ed44dd3f067fa699b5c34661be67c7f0097ea58b0017255141a08d2eb5f87" +}, { + "version" : "9.0-milestone-7", + "buildTime" : "20250513065613+0000", + "commitId" : "3c890746756262d3778e12eaa5155d661d7cbdf2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-7-bin.zip.sha256", + "checksum" : "8e8fc68b78a272aa8da4e6fd608502874f7b9d29257fe1345c7209b725cb7565", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-7-wrapper.jar.sha256", + "wrapperChecksum" : "f98ed44dd3f067fa699b5c34661be67c7f0097ea58b0017255141a08d2eb5f87" +}, { + "version" : "9.0-milestone-6", + "buildTime" : "20250508062448+0000", + "commitId" : "0436db0122ff0c2c8f9ae3db45371f2c067ebfbd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-6-bin.zip.sha256", + "checksum" : "47177c00fc3574e3557902ed9f4bab073597d22c6d9a3fcde8bf8bbc8286f220", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-6-wrapper.jar.sha256", + "wrapperChecksum" : "f98ed44dd3f067fa699b5c34661be67c7f0097ea58b0017255141a08d2eb5f87" +}, { + "version" : "9.0-milestone-5", + "buildTime" : "20250429093659+0000", + "commitId" : "34d31f8f53af29e93357bc88a9a47a1a8001a4ef", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-5-bin.zip.sha256", + "checksum" : "d9f0549d640c07389b2d83717a9ae4ca9a49ed2120f95d14015da227ecba16fc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-5-wrapper.jar.sha256", + "wrapperChecksum" : "c605ee1b011c0da3bc468735530c1d43ea3ede0fb8ce978b5479c7f688343ef5" +}, { + "version" : "9.0-milestone-4", + "buildTime" : "20250428144937+0000", + "commitId" : "ad8b92fc649c0f2efe95287bc43e61a5c0cf618a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-4-bin.zip.sha256", + "checksum" : "ad3873e76d4127629e605d8551fa2b1f893b59d9171138c9463bd5c2001981a9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-4-wrapper.jar.sha256", + "wrapperChecksum" : "c605ee1b011c0da3bc468735530c1d43ea3ede0fb8ce978b5479c7f688343ef5" +}, { + "version" : "8.14", + "buildTime" : "20250425092908+0000", + "commitId" : "34c560e3be961658a6fbcd7170ec2443a228b109", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-bin.zip.sha256", + "checksum" : "61ad310d3c7d3e5da131b76bbf22b5a4c0786e9d892dae8c1658d4b484de3caa", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "8.14-rc-3", + "buildTime" : "20250423120032+0000", + "commitId" : "8afb1d3427f2721ac7aaa95c1b82c73651870d49", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-3-bin.zip.sha256", + "checksum" : "d6442642e2f9d920bad96115c91e30a38afd6cdce61f9043ee45e2ab1e1a2972", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "9.0-milestone-3", + "buildTime" : "20250422030543+0000", + "commitId" : "b86fa516155d1d3baef664ba5288ad578b337b07", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-3-bin.zip.sha256", + "checksum" : "d9a2c448ef8adb196c65deda1b9680060372528946aed78c7bf24c0ab6443518", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "c605ee1b011c0da3bc468735530c1d43ea3ede0fb8ce978b5479c7f688343ef5" +}, { + "version" : "8.14-rc-2", + "buildTime" : "20250417124738+0000", + "commitId" : "c6a073de26dd590617441ea3805bc2743fb315eb", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-2-bin.zip.sha256", + "checksum" : "64caf15a08354b01c3c32de0e433179e6678041e8dfe8aa2a27da06ec81c7e35", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "9.0-milestone-2", + "buildTime" : "20250414083159+0000", + "commitId" : "ce1ff4a2ca36b5f7959f30e211eb2025b9433c89", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-2-bin.zip.sha256", + "checksum" : "19507539796ff63011c87493b926038e2028b94f200f3ba02e5c776e1cdc20ca", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "c605ee1b011c0da3bc468735530c1d43ea3ede0fb8ce978b5479c7f688343ef5" +}, { + "version" : "9.0-milestone-1", + "buildTime" : "20250410115011+0000", + "commitId" : "db647b24a4039507bbcac1273883745322ae84c3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "9.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-1-bin.zip.sha256", + "checksum" : "6963d0abe43cacb1e159bbc28a0e4e80c1622c0384549c3792257981d89d37ed", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "9687bf4e8beb2c293cfd1393cbf2854ef68f921757302116033f476289abf7b0" +}, { + "version" : "8.14-rc-1", + "buildTime" : "20250409084650+0000", + "commitId" : "266969fceec7f1ad8c1d39e1354d963ce2857269", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-1-bin.zip.sha256", + "checksum" : "ea87df6204bb94a432cf58d9a915408b1588a8bf146643a347e31fabd7b0e5a8", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172" +}, { + "version" : "8.14-milestone-8", + "buildTime" : "20250408011208+0000", + "commitId" : "31bca59472c3050181a775f6ca54e5337b8a75e3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-8-bin.zip.sha256", + "checksum" : "6b6e36374f80d3e5594d9fda7040bf45e1e4617ecb95529f4f5cae77d4c6cd8f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-8-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-7", + "buildTime" : "20250324073617+0000", + "commitId" : "48de48758fc3bc62eccebd723f68f9b8d8173e66", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-7-bin.zip.sha256", + "checksum" : "13c012a0343d1d310966e6ee1347b3a9b90f210f06a1b1e54ce57fbea8a1233d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-7-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-5", + "buildTime" : "20250320082137+0000", + "commitId" : "9728ad5ef05b47255083a31f83ed0fbc3f5035e3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-5-bin.zip.sha256", + "checksum" : "4d7f406f60d84969fb10722f627067cd7a505da593a64afc5769ca7b96b65109", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-5-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-4", + "buildTime" : "20250306073633+0000", + "commitId" : "2f19ff9006b6e7d3d81d614ee87ee6a1f251a3b0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-4-bin.zip.sha256", + "checksum" : "c348e50e30d188490f99bc7bd9a27262f2bf2b63b6df29a126660fbf8dc64a10", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-4-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.13", + "buildTime" : "20250225092214+0000", + "commitId" : "073314332697ba45c16c0a0ce1891fa6794179ff", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-bin.zip.sha256", + "checksum" : "20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-3", + "buildTime" : "20250221141330+0000", + "commitId" : "8b48d0ed4b1711a8c7a8ce63183970b23c1e9490", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-3-bin.zip.sha256", + "checksum" : "b232697f41807f172f991dfb78e5013b6ed71d8c7d0256b2d93df41eba18c5cd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.13-rc-2", + "buildTime" : "20250220142623+0000", + "commitId" : "160d52df5c7663f9854ab7fddf0123a8f044c530", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.13", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-2-bin.zip.sha256", + "checksum" : "264353f17a13391626fc8d0e86ae8023f30ea334a470caae0cfee02fe6cd1f3d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-2", + "buildTime" : "20250220124058+0000", + "commitId" : "affa639ae530da704951a05c80cef66583558ace", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-2-bin.zip.sha256", + "checksum" : "2db4c39174c3dc0ced756b60b2b8d151eb0d12b65e539c91ac7f7d36b7e7dd2d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.14-milestone-1", + "buildTime" : "20250214133101+0000", + "commitId" : "dc47699e834411a85029a51a0723e2381afced09", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.14", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-1-bin.zip.sha256", + "checksum" : "0978afc2b32e0f05dbe881681de850a2413f85f6ebe96848719e2b710b9fbde7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.13-rc-1", + "buildTime" : "20250212094604+0000", + "commitId" : "e1568ff83ddecf64248260376da957b986d625e7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.13", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-1-bin.zip.sha256", + "checksum" : "3b3565efd2df2dd999774b6ef8ea571878c5532cbac6dbaaeb4b2731f42e6704", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "81a82aaea5abcc8ff68b3dfcb58b3c3c429378efd98e7433460610fecd7ae45f" +}, { + "version" : "8.12.1", + "buildTime" : "20250124125512+0000", + "commitId" : "0b1ee1ff81d1f4a26574ff4a362ac9180852b140", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-bin.zip.sha256", + "checksum" : "8d97a97984f6cbd2b85fe4c60a743440a347544bf18818048e611f5288d46c94", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.13-milestone-3", + "buildTime" : "20250121164636+0000", + "commitId" : "008d02e96f845b2d5a628758831c97d305983d68", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.13", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-3-bin.zip.sha256", + "checksum" : "f22cfbc3bd7e2e762910ed22926614b11af5af23456cec8730cde390b3e6da58", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.12.1-milestone-1", + "buildTime" : "20250121110021+0000", + "commitId" : "6d9f96d13ea6d230832feb4bf74b71af5df083d6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.12.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-milestone-1-bin.zip.sha256", + "checksum" : "003255422dfe4b7306412cd39a120a4382d5db51df9acac33f997b2cb54c4ccd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.12.1-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.13-milestone-2", + "buildTime" : "20250110085439+0000", + "commitId" : "bf974bdd5b1a046611eb50a5ec863c07f7630bfd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.13", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-2-bin.zip.sha256", + "checksum" : "e8b04d5491ad67bb5e2cd6a3168631dde9b5ab8d0faf5ed1fd3081526cded0ea", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.13-milestone-1", + "buildTime" : "20250109202014+0000", + "commitId" : "017df4898587f8decd1ee7dae2dd33f94c9f5a8a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.13", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-1-bin.zip.sha256", + "checksum" : "524079334d60703b233cab4a467574eac0208783ae00c70ef722d349e3db3623", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.13-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.12", + "buildTime" : "20241220154653+0000", + "commitId" : "a3cacb207fec727859be9354c1937da2e59004c1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.12-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-bin.zip.sha256", + "checksum" : "7a00d51fb93147819aab76024feece20b6b84e420694101f276be952e08bef03", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.12-rc-2", + "buildTime" : "20241217162852+0000", + "commitId" : "98f15d7fbb920b0d3b5388dd0a3e6fb244302f8f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.12", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-2-bin.zip.sha256", + "checksum" : "4c403eac4e3957107a6e954ae2b59ba6cf9823f683276f1f9e20ef79b8d350a2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.12-rc-1", + "buildTime" : "20241212152352+0000", + "commitId" : "e385beb86216461fe39ad37767b95bb0fa6ff777", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.12", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-1-bin.zip.sha256", + "checksum" : "4d9d7ad4cf8842f279649213d2f87d8f7e9a03ae75ac4389517aa574b1404b2a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.12-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11.1", + "buildTime" : "20241120165646+0000", + "commitId" : "481cb05a490e0ef9f8620f7873b83bd8a72e7c39", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11.1-bin.zip.sha256", + "checksum" : "f397b287023acdba1e9f6fc5ea72d22dd63669d59ed4a289a29b1a76eee151c6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11.1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11", + "buildTime" : "20241111135801+0000", + "commitId" : "b2ef976169a05b3c76d04f0fa76a940859f96fa4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-bin.zip.sha256", + "checksum" : "57dafb5c2622c6cc08b993c85b7c06956a2f53536432a30ead46166dbca0f1e9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11-rc-3", + "buildTime" : "20241107134628+0000", + "commitId" : "ca3040e548f099c5c879ec2ef93f2f0496551d4b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-3-bin.zip.sha256", + "checksum" : "8e4b38e068e5d73272498ce4743d86adccebfdadaa4233dd15bfb6c6b82b9152", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11-rc-2", + "buildTime" : "20241031150259+0000", + "commitId" : "484cebf8e46e7e0150107705c7ef8d62d72af447", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-2-bin.zip.sha256", + "checksum" : "7565ee2f3d2193fce6e752f474be66eb0c6252f0c9ef792a8bffda6e7fd31b36", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11-rc-1", + "buildTime" : "20241017104024+0000", + "commitId" : "4eab5987e618554d2d5e51fbc7b1dd1a3c7e8cb8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-1-bin.zip.sha256", + "checksum" : "2522b1593704b6b26e03cb2dec86d792b1c6ca5c33d10e078fab7a146354130e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.11-milestone-1", + "buildTime" : "20241006083426+0000", + "commitId" : "4a6676a6310b36abe94e9451436a8621e113728a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.11", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.11-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-milestone-1-bin.zip.sha256", + "checksum" : "2c406ba10eaed701b69d01de924dfaf36106d4764e2090175d103b2e7acba211", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.11-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.10.2", + "buildTime" : "20240923212839+0000", + "commitId" : "415adb9e06a516c44b391edff552fd42139443f7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-bin.zip.sha256", + "checksum" : "31c55713e40233a8303827ceb42ca48a47267a0ad4bab9177123121e71524c26", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.10.2-milestone-1", + "buildTime" : "20240919234735+0000", + "commitId" : "c3ea2131e78003a1506533a3ddc952014217cee3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.10.2", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-milestone-1-bin.zip.sha256", + "checksum" : "b6650db1f41b146b4a780c2a183e14602a6cf861a7797d97ea530fb1384d6336", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.2-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.10.1", + "buildTime" : "20240909074256+0000", + "commitId" : "8716158d3ec8c59e38f87a67f1f311f297b79576", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.10.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.1-bin.zip.sha256", + "checksum" : "1541fa36599e12857140465f3c91a97409b4512501c26f9631fb113e392c5bd1", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.10.1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.10", + "buildTime" : "20240814110745+0000", + "commitId" : "fef2edbed8af1022cefaf44d4c0514c5f89d7b78", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.10-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.10-bin.zip.sha256", + "checksum" : "5b9c5eb3f9fc2c94abaea57d90bd78747ca117ddbbf96c859d3741181a12bf2a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.10-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.10-rc-1", + "buildTime" : "20240808060755+0000", + "commitId" : "4f143ee84909970e5148c38b3ac7db3ff826a022", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.10-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.10-rc-1-bin.zip.sha256", + "checksum" : "de2ad3b249d89af001ce3dace548f8149675c692ef8bccf271444985b590d284", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.10-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "2db75c40782f5e8ba1fc278a5574bab070adccb2d21ca5a6e5ed840888448046" +}, { + "version" : "8.9", + "buildTime" : "20240711143741+0000", + "commitId" : "d536ef36a19186ccc596d8817123e5445f30fef8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-bin.zip.sha256", + "checksum" : "d725d707bfabd4dfdc958c624003b3c80accc03f7037b5122c4b1d0ef15cecab", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-wrapper.jar.sha256", + "wrapperChecksum" : "498495120a03b9a6ab5d155f5de3c8f0d986a449153702fb80fc80e134484f17" +}, { + "version" : "8.9-rc-2", + "buildTime" : "20240705153825+0000", + "commitId" : "fcee8d48f54051165ef816487636dfc0aa4c88a0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-2-bin.zip.sha256", + "checksum" : "c2b58a752b82346a89a9d5384ba0882d9c3f0c29e0700c8e050002b81d6d4ff9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "498495120a03b9a6ab5d155f5de3c8f0d986a449153702fb80fc80e134484f17" +}, { + "version" : "8.9-rc-1", + "buildTime" : "20240621131308+0000", + "commitId" : "a667c19ff4d039304aa43268948139dc9ca6baa6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-1-bin.zip.sha256", + "checksum" : "3ae7f7d3d433936b546c13505bb55adbae7708e715bfc4cf36cfc22650e6a881", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.9-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "498495120a03b9a6ab5d155f5de3c8f0d986a449153702fb80fc80e134484f17" +}, { + "version" : "8.8", + "buildTime" : "20240531214656+0000", + "commitId" : "4bd1b3d3fc3f31db5a26eecb416a165b8cc36082", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-bin.zip.sha256", + "checksum" : "a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.8-rc-2", + "buildTime" : "20240527070937+0000", + "commitId" : "d0f4982aba27e390b46c383277db130abfc1b44e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-2-bin.zip.sha256", + "checksum" : "b50250702d1b238f797c0778259f8b58714d5d16fbfec41fb19135bf86c64fe8", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.8-rc-1", + "buildTime" : "20240426151958+0000", + "commitId" : "000ef8587278729b8ea91c89b1c527ba0164338a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-1-bin.zip.sha256", + "checksum" : "a2e1cfee7ffdeee86015b85b2dd2a435032c40eedc01d8172285556c7d8fea13", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.8-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.7", + "buildTime" : "20240322155246+0000", + "commitId" : "650af14d7653aa949fce5e886e685efc9cf97c10", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-bin.zip.sha256", + "checksum" : "544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.7-rc-4", + "buildTime" : "20240320140334+0000", + "commitId" : "b9db7ce7ad5d1f2adf1928955ae926329262cf82", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-4-bin.zip.sha256", + "checksum" : "a14ad975075d5d2dcc1fa1667e622597c6f8721e2ca42dff54ebe40c07eb1bc5", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.7-rc-3", + "buildTime" : "20240311171310+0000", + "commitId" : "ab30a66001c3476658e07c46e1a9c5e862aed438", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-3-bin.zip.sha256", + "checksum" : "f0183d411f68b72d37112e47b539c6d91d1870842f5f2ce9cffefa0a7756ae52", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.7-rc-2", + "buildTime" : "20240229125337+0000", + "commitId" : "cf4549b56c9b4d7c453795158e19a544b2a0dd60", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-2-bin.zip.sha256", + "checksum" : "3348170800c324fc97ff201fdc139ad465c073ab19760cfe22a872b5b858560f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "8.7-rc-1", + "buildTime" : "20240222213949+0000", + "commitId" : "6cd7d3cf5929e1bd9f917b7dc67d03cd65d22d08", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-1-bin.zip.sha256", + "checksum" : "4354bc6c46d1f1e1e37cb371b7c31556a4427eb3f57d03c23e5aae8aa606aedb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.7-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "cb0da6751c2b753a16ac168bb354870ebb1e162e9083f116729cec9c781156b8" +}, { + "version" : "7.6.4", + "buildTime" : "20240205142918+0000", + "commitId" : "e0bb3fc8cefad8432c9033cdfb12dc14facc9dd9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.4-bin.zip.sha256", + "checksum" : "bed1da33cca0f557ab13691c77f38bb67388119e4794d113e051039b80af9bb1", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.4-wrapper.jar.sha256", + "wrapperChecksum" : "14dfa961b6704bb3decdea06502781edaa796a82e6da41cd2e1962b14fbe21a3" +}, { + "version" : "8.6", + "buildTime" : "20240202164716+0000", + "commitId" : "d55c486870a0dc6f6278f53d21381396d0741c6e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-bin.zip.sha256", + "checksum" : "9631d53cf3e74bfa726893aee1f8994fee4e060c401335946dba2156f440f24c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.6-rc-4", + "buildTime" : "20240131130628+0000", + "commitId" : "f1b03c0a136f87f0d33406b8ea3f2039caec2a32", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-4-bin.zip.sha256", + "checksum" : "1fb8db52c64b78eb27358dbe7119040090d227cb7de0436c0a657863c44b84c6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.6-rc-3", + "buildTime" : "20240123150619+0000", + "commitId" : "40b1a053f84abb1d167d88142af835c96eb4c3d7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-3-bin.zip.sha256", + "checksum" : "281b47656267472c9f853e7ed54002bfd435bfdcd292286709705324fd96f5dc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.6-rc-2", + "buildTime" : "20240112144903+0000", + "commitId" : "cee3a34b18b771fe4174800920b5a0009a980034", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-2-bin.zip.sha256", + "checksum" : "3a36cedd25c02335d991e3684e17985239150e24b744a8513d466543083ca250", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.6-rc-1", + "buildTime" : "20231228064048+0000", + "commitId" : "19db0f499fd22a228355c1a6594b9159176a466e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-1-bin.zip.sha256", + "checksum" : "a2da4ba435f6728b43554c5845f6f88f79589c3e0018c29ab33eb23bd781255b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.6-milestone-1", + "buildTime" : "20231208093905+0000", + "commitId" : "68a2e7c3355e027b514e59fe9e4671d780409535", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.6", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.6-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-milestone-1-bin.zip.sha256", + "checksum" : "0bd40db84cb02df49bde4c3f45b6fa7267f78c075447cb70bac3cc79aed233b7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.6-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.5", + "buildTime" : "20231129140857+0000", + "commitId" : "28aca86a7180baa17117e0e5ba01d8ea9feca598", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-bin.zip.sha256", + "checksum" : "9d926787066a081739e8200858338b4a69e837c3a821a33aca9db09dd4a41026", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.5-rc-4", + "buildTime" : "20231124091900+0000", + "commitId" : "ff7bf18d018392c80aead72f93d4530726e9504d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-4-bin.zip.sha256", + "checksum" : "614587c584e4d97a00368c8f5e6749d3e38345b7810bcfef1c7b8e002127b3a3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.5-rc-3", + "buildTime" : "20231117134310+0000", + "commitId" : "d8463b2b64aa2952b741fcb1a7da11710ffc2c5a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-3-bin.zip.sha256", + "checksum" : "7207f771dac48bfe19d5990c8f1e7e5f3d5e8b2b98e09ad9a4c484af537f86b2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.5-rc-2", + "buildTime" : "20231114141630+0000", + "commitId" : "9bfdf5b90ff69b6775c2fe163808a99eac2dc543", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-2-bin.zip.sha256", + "checksum" : "547d33f88a30b6d9666c79fde614b1e5a1a3e2f86216bc70c4c7e3421f0d4627", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.5-rc-1", + "buildTime" : "20231107155056+0000", + "commitId" : "3dc993b32164f697b540e88c2426977e097d3aff", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-1-bin.zip.sha256", + "checksum" : "8c74462b1d83f8b17c4838c925fc4c46d1fbb4467b18b8a17f5cdaaee45b7f09", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.5-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "d3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd" +}, { + "version" : "8.4", + "buildTime" : "20231004205213+0000", + "commitId" : "e9251e572c9bd1d01e503a0dfdf43aedaeecdc3f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-bin.zip.sha256", + "checksum" : "3e1af3ae886920c3ac87f7a91f816c0c7c436f276a6eefdb3da152100fef72ae", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "7.6.3", + "buildTime" : "20231004155947+0000", + "commitId" : "1694251d59e0d4752d547e1fd5b5020b798a7e71", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.3-bin.zip.sha256", + "checksum" : "740c2e472ee4326c33bf75a5c9f5cd1e69ecf3f9b580f6e236c86d1f3d98cfac", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.3-wrapper.jar.sha256", + "wrapperChecksum" : "14dfa961b6704bb3decdea06502781edaa796a82e6da41cd2e1962b14fbe21a3" +}, { + "version" : "8.4-rc-3", + "buildTime" : "20230929135700+0000", + "commitId" : "7788100a0736a67692b30f53fe3b2442dbb9816d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-3-bin.zip.sha256", + "checksum" : "cb1945fa73552ddf99cf32ec2fcd065771fcb6bf4a366b9408e79f9561601853", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.4-rc-2", + "buildTime" : "20230926114721+0000", + "commitId" : "21dd48a6b94a5dfff7fa19c4e3c65b5522c75cb3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-2-bin.zip.sha256", + "checksum" : "f7dca5ae44f1a45ee7950fa3e01418a664eaa04136ff594a99bebf7ee353a943", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.4-rc-1", + "buildTime" : "20230920065917+0000", + "commitId" : "ebdfd04b7e181147b7ce87af0f72b950261085f7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-1-bin.zip.sha256", + "checksum" : "7eec87a597388fb67df02d0cc4f8bc566abd5adf5838dab1f4db0e83fa96bd4f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.4-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.3", + "buildTime" : "20230817070647+0000", + "commitId" : "8afbf24b469158b714b36e84c6f4d4976c86fcd5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-bin.zip.sha256", + "checksum" : "591855b517fc635b9e04de1d05d5e76ada3f89f5fc76f87978d1b245b4f69225", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.3-rc-4", + "buildTime" : "20230814082841+0000", + "commitId" : "45d5bd72d0d837799c297e94bc1fbca9fee637eb", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-4-bin.zip.sha256", + "checksum" : "e4d88e0e636799134179376b5b058b0c691e58a1e9150c1d0a3898ffd402abaa", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.3-rc-3", + "buildTime" : "20230802172145+0000", + "commitId" : "5137ec0e9b3df9f4d40bc9e055d5f0b1024d9327", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-3-bin.zip.sha256", + "checksum" : "7af08310663d2849d8288d075cc2b74ca56dbacfa7ae60257c02aab0e8fe3111", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.3-rc-2", + "buildTime" : "20230726152039+0000", + "commitId" : "c48101dc7f9c766efa8206574aa9c2195d2b7960", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-2-bin.zip.sha256", + "checksum" : "222818637ce0a4cb82e322bf847ea49ac319aecdb363d81acabd9e81315d08f6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.3-rc-1", + "buildTime" : "20230719075404+0000", + "commitId" : "d036e6078ed80de7a7e1ad6a17aae6be115b5d0d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-1-bin.zip.sha256", + "checksum" : "05d10c69f03ef1ed1569171e637fc1737828bceaf4bb4a1e87407a4a7d1c01e6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.3-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "0336f591bc0ec9aa0c9988929b93ecc916b3c1d52aed202c7381db144aa0ef15" +}, { + "version" : "8.2.1", + "buildTime" : "20230710121235+0000", + "commitId" : "a38ec64d3c4612da9083cc506a1ccb212afeecaa", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2.1-bin.zip.sha256", + "checksum" : "03ec176d388f2aa99defcadc3ac6adf8dd2bce5145a129659537c0874dea5ad1", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "a8451eeda314d0568b5340498b36edf147a8f0d692c5ff58082d477abe9146e4" +}, { + "version" : "8.2", + "buildTime" : "20230630180230+0000", + "commitId" : "5f4a070a62a31a17438ac998c2b849f4f6892877", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-bin.zip.sha256", + "checksum" : "38f66cd6eef217b4c35855bb11ea4e9fbc53594ccccb5fb82dfd317ef8c2c5a3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-wrapper.jar.sha256", + "wrapperChecksum" : "a8451eeda314d0568b5340498b36edf147a8f0d692c5ff58082d477abe9146e4" +}, { + "version" : "7.6.2", + "buildTime" : "20230630154251+0000", + "commitId" : "dab132169006b16e7ada4ab2456e0c9d6415b52a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.2-bin.zip.sha256", + "checksum" : "a01b6587e15fe7ed120a0ee299c25982a1eee045abd6a9dd5e216b2f628ef9ac", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.2-wrapper.jar.sha256", + "wrapperChecksum" : "14dfa961b6704bb3decdea06502781edaa796a82e6da41cd2e1962b14fbe21a3" +}, { + "version" : "8.2-rc-3", + "buildTime" : "20230628070204+0000", + "commitId" : "66d698f44a1d71c01e80664f984a5c86a83bd28d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-3-bin.zip.sha256", + "checksum" : "345389a51e2ec796b1102c1ce513ac8f571978fd698e3fb10b6e0457091b6edc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "a8451eeda314d0568b5340498b36edf147a8f0d692c5ff58082d477abe9146e4" +}, { + "version" : "8.2-rc-2", + "buildTime" : "20230606183136+0000", + "commitId" : "5b4d99e3745b5e4c9929621c9377b6f8fb97f3fd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-2-bin.zip.sha256", + "checksum" : "0906569bf96e8ebefbc1aa56318c74aeafd9710455b2817c70d709c5d77785c4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "5c9a1a6f50b4f8c0264b1ac69013bef9f8363733275fafa56c70c84be3276bb8" +}, { + "version" : "8.2-rc-1", + "buildTime" : "20230531181205+0000", + "commitId" : "2a2778f51363d9574f808ae80d34d8b5bd99d98b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-1-bin.zip.sha256", + "checksum" : "f93546eb9b9ca57eb1d22f6e6d50a181e70720d4ba6d6af8fc96037fe4c8daf6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "55e949185c26ba3ddcd2c6a4217d043bfa0ce3cc002bbbb52b709a181a513e81" +}, { + "version" : "8.2-milestone-1", + "buildTime" : "20230426164225+0000", + "commitId" : "0f7d6b14b563ca06bfd34d2dde25d0f1f93279e3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.2", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.2-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-milestone-1-bin.zip.sha256", + "checksum" : "4d06de8a5bfe1d53646d78cbe193ceae1d16b2145f522b3724b69d394c38e44f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.2-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "55e949185c26ba3ddcd2c6a4217d043bfa0ce3cc002bbbb52b709a181a513e81" +}, { + "version" : "8.1.1", + "buildTime" : "20230421123126+0000", + "commitId" : "1cf537a851c635c364a4214885f8b9798051175b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1.1-bin.zip.sha256", + "checksum" : "e111cb9948407e26351227dabce49822fb88c37ee72f1d1582a69c68af2e702f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1.1-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.1", + "buildTime" : "20230412120745+0000", + "commitId" : "40ba32cde9d6daf2b92c39376d2758909dd6b813", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-bin.zip.sha256", + "checksum" : "a62c5f99585dd9e1f95dab7b9415a0e698fa9dd1e6c38537faa81ac078f4d23e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.1-rc-4", + "buildTime" : "20230411093751+0000", + "commitId" : "a63ac23cdbaf9c9e5e43b9fdb7fa478780fc40b2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-4-bin.zip.sha256", + "checksum" : "75cbca1c36f3695ec15b39ac1f22103e5c84ba949b672497e70e9a99dc2ca195", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.1-rc-3", + "buildTime" : "20230404095841+0000", + "commitId" : "7eb689e589a42dcabd23aa8ccffa9a020c2010d2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-3-bin.zip.sha256", + "checksum" : "4087d4c8eb90335f7c635082424cf34480721544ad36b595dcf8c143c653c149", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.1-rc-2", + "buildTime" : "20230329080943+0000", + "commitId" : "b7cde67fa9c3fcbf03a4cefdb2214d52963a8678", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-2-bin.zip.sha256", + "checksum" : "c8e041dcbf7b747bf967b607942d8cf9a7cf992f4c05c90b1c8826901c183617", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.1-rc-1", + "buildTime" : "20230320143921+0000", + "commitId" : "cd44247d421f76903decf83a0efad7bbc1552dc1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-1-bin.zip.sha256", + "checksum" : "0a0143227c5a1699753e5e37f0c03bd784b3bd6c617d19e4f0c4df12c32b0fe2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.1-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "ed2c26eba7cfb93cc2b7785d05e534f07b5b48b5e7fc941921cd098628abca58" +}, { + "version" : "8.0.2", + "buildTime" : "20230303164137+0000", + "commitId" : "7d6581558e226a580d91d399f7dfb9e3095c2b1d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0.2-bin.zip.sha256", + "checksum" : "ff7bf6a86f09b9b2c40bb8f48b25fc19cf2b2664fd1d220cd7ab833ec758d0d7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0.2-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "7.6.1", + "buildTime" : "20230224135442+0000", + "commitId" : "3905fe8ac072bbd925c70ddbddddf4463341f4b4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.1-bin.zip.sha256", + "checksum" : "6147605a23b4eff6c334927a86ff3508cb5d6722cd624c97ded4c2e8640f1f87", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6.1-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "6.9.4", + "buildTime" : "20230222084312+0000", + "commitId" : "7f9380f27d6dc6a1ee6dfc466b834b0408d0b0c4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.4-bin.zip.sha256", + "checksum" : "3e240228538de9f18772a574e99a0ba959e83d6ef351014381acd9631781389a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.4-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "8.0.1", + "buildTime" : "20230217200948+0000", + "commitId" : "68959bf76cef4d28c678f2e2085ee84e8647b77a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0.1-bin.zip.sha256", + "checksum" : "1b6b558be93f29438d3df94b7dfee02e794b94d9aca4611a92cdb79b6b88e909", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0.1-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0", + "buildTime" : "20230213131521+0000", + "commitId" : "62ab9b7c7f884426cf79fbedcf07658b2dbe9e97", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-bin.zip.sha256", + "checksum" : "4159b938ec734a8388ce03f52aa8f3c7ed0d31f5438622545de4f83a89b79788", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-rc-5", + "buildTime" : "20230209233943+0000", + "commitId" : "47a354e154889b2656373419e3571410291404a7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-5-bin.zip.sha256", + "checksum" : "c01b46042141d23aea23ae5b09a12410a7996bac8c7fdf367547acd1b3c772d9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-rc-4", + "buildTime" : "20230209172637+0000", + "commitId" : "5eb67a82c4283959484a3b93cb3ede1a08a2504b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-4-bin.zip.sha256", + "checksum" : "c1e2a61fd7257fdb869020808930487a69179488490a27664b06e472ecbf4ed7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-rc-3", + "buildTime" : "20230206094910+0000", + "commitId" : "0acb34c510be84f8fad1923e3a62c17eecbc886a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-3-bin.zip.sha256", + "checksum" : "9fb87bbe73f963a4c23daede3a5ed78e7ae770f3e980d3cb15d8df04518b542d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-rc-2", + "buildTime" : "20230117102518+0000", + "commitId" : "8dcd942c9efaac6cb7f577f3b7a7521d0801704d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-2-bin.zip.sha256", + "checksum" : "28ebe9afc20564bcdc39bfe36f6b60a373e40be2c3c307a0028b545b8ccf6ba0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-rc-1", + "buildTime" : "20221229123250+0000", + "commitId" : "ae81f85f0e184196c188b96016eb3d9e38f60673", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "8.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-1-bin.zip.sha256", + "checksum" : "d5bb29e784426547e4f455fbc0e6512d7a6a67d7d890cf24d601309287128b79", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-milestone-6", + "buildTime" : "20221220081158+0000", + "commitId" : "6abc8d20bdd7b25edb3a24f62454f0e56257f552", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-6-bin.zip.sha256", + "checksum" : "5db785194950909ded9d2bcad3cd6874e7578bc93c11bf1aaa2d3e89aa8c9deb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-6-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-milestone-5", + "buildTime" : "20221208181122+0000", + "commitId" : "3fb0faa58efc3d984465da3316663a0b89e0b728", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-5-bin.zip.sha256", + "checksum" : "0288540bc5ad21c4c921743464da8b7c8c8403750ddb20d050dc088282814664", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-5-wrapper.jar.sha256", + "wrapperChecksum" : "91941f522fbfd4431cf57e445fc3d5200c85f957bda2de5251353cf11174f4b5" +}, { + "version" : "8.0-milestone-4", + "buildTime" : "20221201090656+0000", + "commitId" : "d06a65fb5320997b66461bbfa74266aa050a3b1d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-4-bin.zip.sha256", + "checksum" : "8ecb39c34977c75381299045cb238f6e4d3368e67b7221fcd9ac34ad344e4b63", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-4-wrapper.jar.sha256", + "wrapperChecksum" : "577b2de036000db2e0f04f2ec842a4f1e648c8b6f9c87f29a8d896acb1732538" +}, { + "version" : "7.6", + "buildTime" : "20221125133510+0000", + "commitId" : "daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-bin.zip.sha256", + "checksum" : "7ba68c54029790ab444b39d7e293d3236b2632631fb5f2e012bb28b4ff669e4b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.6-rc-4", + "buildTime" : "20221122094210+0000", + "commitId" : "87a8037940defe7c18ef332efac18f0405baa37b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-4-bin.zip.sha256", + "checksum" : "eb93e4f8abf8db59d5bd1bbdd716f971d010bfc82fd01fe46905d499305718a0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.6-rc-3", + "buildTime" : "20221111142340+0000", + "commitId" : "6c339330ec227cafd6049ff3245ccef45ae8c7d4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-3-bin.zip.sha256", + "checksum" : "21e2bf8e6c6ac310182c817776ffc6cca80142f5de4b2b4151f9f72b9ec35b50", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.6-rc-2", + "buildTime" : "20221108130606+0000", + "commitId" : "9e7785b963b6627bf6a0d8599d971f4c56f22a5a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-2-bin.zip.sha256", + "checksum" : "1a0c892659d3eec2a33ab23857784ee3c9674b681cab068f67780da412d3f61f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "8.0-milestone-3", + "buildTime" : "20221028140616+0000", + "commitId" : "fbe9699332c66d9dc7d28aaa4982f7ffc7aab81a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-3-bin.zip.sha256", + "checksum" : "ad9460264653b6ed16cc8aebf9ee4dc12d1a301351323233eb905fce5d522ab4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.6-rc-1", + "buildTime" : "20221028075211+0000", + "commitId" : "62973455a8df25d003949f3f573593cd10d083a3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-1-bin.zip.sha256", + "checksum" : "458550f35b74ad9fbbdf3a1eb69c3f4faef7944cffa3d7fb91b46327875b0b17", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "6.9.3", + "buildTime" : "20221017074402+0000", + "commitId" : "a6198e44749b18b37e26b3b3467db17e034bcff4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.3-bin.zip.sha256", + "checksum" : "dcf350b8ae1aa192fc299aed6efc77b43825d4fedb224c94118ae7faf5fb035d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "8.0-milestone-2", + "buildTime" : "20221009102113+0000", + "commitId" : "2fa96f7c0db3ece3385edf57f44e89589cd59486", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-2-bin.zip.sha256", + "checksum" : "7fe8e8147543e80b4e1a11dfaf819d80d5bd32d1d922b11944e768a15c7c6a7b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "8.0-milestone-1", + "buildTime" : "20220923110128+0000", + "commitId" : "60cd44b5204ccedca6bf0f194bc4b17b8a3a7ad5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "8.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-1-bin.zip.sha256", + "checksum" : "573715e1f25829fc3c2ad2e97578e9f8433050da87ddab5413e587663a621ebe", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.6-milestone-1", + "buildTime" : "20220923093944+0000", + "commitId" : "d4f94bd96c2dfb7374cd71abbc4b1f628a2d6a2b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "7.6", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.6-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-milestone-1-bin.zip.sha256", + "checksum" : "9ca7bc6d09c52b365cc61f4ef9e0410f79f6122bc126786c76847b987a4b7cbe", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.6-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "c5a643cf80162e665cc228f7b16f343fef868e47d3a4836f62e18b7e17ac018a" +}, { + "version" : "7.5.1", + "buildTime" : "20220805211756+0000", + "commitId" : "d1daa0cbf1a0103000b71484e1dbfe096e095918", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5.1-bin.zip.sha256", + "checksum" : "f6b8596b10cce501591e92f229816aa4046424f3b24d771751b06779d58c8ec4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5.1-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5", + "buildTime" : "20220714124815+0000", + "commitId" : "c7db7b958189ad2b0c1472b6fe663e6d654a5103", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-bin.zip.sha256", + "checksum" : "cb87f222c5585bd46838ad4db78463a5c5f3d336e5e2b98dc7c0c586527351c2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5-rc-5", + "buildTime" : "20220712114039+0000", + "commitId" : "0da5e9cce20a3cb79ede91fbc52d3efec9e309a9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-5-bin.zip.sha256", + "checksum" : "c0bbfe570634634271f472aa30ec8d059a2e97b920be687d4c6446efbad9cc58", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5-rc-4", + "buildTime" : "20220706171202+0000", + "commitId" : "a641d1caebe02ea109a69ca5fc0f59c20bceb9de", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-4-bin.zip.sha256", + "checksum" : "d98c4322d882e7b115fbf2a33ccd3e03677653b88c4f245e33cbea28b531a173", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5-rc-3", + "buildTime" : "20220629120328+0000", + "commitId" : "db02cfd2c0a9d1503dec236222fcc5b545413102", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-3-bin.zip.sha256", + "checksum" : "d10d8e6206d613313379834f3ed94ec7b781790f22edd44b0f2afa76708e2050", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5-rc-2", + "buildTime" : "20220610165005+0000", + "commitId" : "e97f154bbdce2d3da1d46161417f7a7eaa9e00f2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-2-bin.zip.sha256", + "checksum" : "ba761aa1563f5d893d1a6e7ddca48bbdc4385fdd527974e6472b873b7eae9a32", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.5-rc-1", + "buildTime" : "20220512190923+0000", + "commitId" : "8d531f6b22fb3a97fb623f949c5e7bae8a2e1fe2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-1-bin.zip.sha256", + "checksum" : "8ba57a37e1e0b8c415e4d91718d51035223aa73131cf719a50c95a2a88269eb2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.5-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "91a239400bb638f36a1795d8fdf7939d532cdc7d794d1119b7261aac158b1e60" +}, { + "version" : "7.4.2", + "buildTime" : "20220331152529+0000", + "commitId" : "540473b8118064efcc264694cbcaa4b677f61041", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.4.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.4.2-bin.zip.sha256", + "checksum" : "29e49b10984e585d8118b7d0bc452f944e386458df27371b49b4ac1dec4b7fda", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.4.2-wrapper.jar.sha256", + "wrapperChecksum" : "575098db54a998ff1c6770b352c3b16766c09848bee7555dab09afc34e8cf590" +}, { + "version" : "7.4.1", + "buildTime" : "20220309150447+0000", + "commitId" : "36dc52588e09b4b72f2010bc07599e0ee0434e2e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.4.1-bin.zip.sha256", + "checksum" : "e5444a57cda4a95f90b0c9446a9e1b47d3d7f69057765bfb54bd4f482542d548", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.4.1-wrapper.jar.sha256", + "wrapperChecksum" : "575098db54a998ff1c6770b352c3b16766c09848bee7555dab09afc34e8cf590" +}, { + "version" : "7.4", + "buildTime" : "20220208095838+0000", + "commitId" : "f0d9291c04b90b59445041eaa75b2ee744162586", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-bin.zip.sha256", + "checksum" : "8cc27038d5dbd815759851ba53e70cf62e481b87494cc97cfd97982ada5ba634", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-wrapper.jar.sha256", + "wrapperChecksum" : "575098db54a998ff1c6770b352c3b16766c09848bee7555dab09afc34e8cf590" +}, { + "version" : "7.4-rc-2", + "buildTime" : "20220202150044+0000", + "commitId" : "88ab9b652933bc3b2e3161b31ad8b8f4f0516351", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-2-bin.zip.sha256", + "checksum" : "21491c9f0656e1529ccb39cbd587d01c33ba00d25f994b10240748ed0d45894a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "575098db54a998ff1c6770b352c3b16766c09848bee7555dab09afc34e8cf590" +}, { + "version" : "7.4-rc-1", + "buildTime" : "20220117201507+0000", + "commitId" : "693625f25f2210d1a3ca866897d69ac221de6811", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-1-bin.zip.sha256", + "checksum" : "b2a452bc37637aa453f0f3fb526240caed02878c47a9736f3059590605893bdb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.4-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "575098db54a998ff1c6770b352c3b16766c09848bee7555dab09afc34e8cf590" +}, { + "version" : "7.3.3", + "buildTime" : "20211222123754+0000", + "commitId" : "6f556c80f945dc54b50e0be633da6c62dbe8dc71", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-bin.zip.sha256", + "checksum" : "b586e04868a22fd817c8971330fec37e298f3242eb85c374181b12d637f80302", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3.3-rc-1", + "buildTime" : "20211222090657+0000", + "commitId" : "6f556c80f945dc54b50e0be633da6c62dbe8dc71", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-rc-1-bin.zip.sha256", + "checksum" : "07a1cd283d6cfe437eb08fe08936dc1af5f12946e67dc9f5e0a9f4b948ebfd3a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.3-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "6.9.2", + "buildTime" : "20211221172537+0000", + "commitId" : "5d94aa68c0fdbe443838bb977080e3b9f273e889", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.2-bin.zip.sha256", + "checksum" : "8b356fd8702d5ffa2e066ed0be45a023a779bba4dd1a68fd11bc2a6bdc981e8f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.3.2", + "buildTime" : "20211215112231+0000", + "commitId" : "26c186eb63b2b02e68d83b0dbc6ec69ab118653a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.2-bin.zip.sha256", + "checksum" : "23b89f8eac363f5f4b8336e0530c7295c55b728a9caa5268fdd4a532610d5392", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.2-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3.1", + "buildTime" : "20211201154220+0000", + "commitId" : "2c62cec93e0b15a7d2cd68746f3348796d6d42bd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.1-bin.zip.sha256", + "checksum" : "9afb3ca688fc12c761a0e9e4321e4d24e977a4a8916c8a768b1fe05ddb4d6b66", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3.1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3", + "buildTime" : "20211109204036+0000", + "commitId" : "96754b8c44399658178a768ac764d727c2addb37", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-bin.zip.sha256", + "checksum" : "de8f52ad49bdc759164f72439a3bf56ddb1589c4cde802d3cec7d6ad0e0ee410", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3-rc-5", + "buildTime" : "20211105184337+0000", + "commitId" : "426f267f7b768154fd63b27e055ba56b357e6e4e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-5-bin.zip.sha256", + "checksum" : "dca199cee2553ec2497e840bf17969e56d4f99df661a693740b90de7ed0efbeb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3-rc-4", + "buildTime" : "20211105160540+0000", + "commitId" : "426f267f7b768154fd63b27e055ba56b357e6e4e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-4-bin.zip.sha256", + "checksum" : "488d250f4a1ae7744dcdf53fef0708f4a3e373023b8c134eda1be98aba4ed260", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3-rc-3", + "buildTime" : "20211026162420+0000", + "commitId" : "c49d04ba7bd0708b11bf2ecf2e2d0bbd12a5123a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-3-bin.zip.sha256", + "checksum" : "d57eb720003db00f85915c48ff4a5ac3c7e8a8a34bd34ced16ca0c7a31c10110", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3-rc-2", + "buildTime" : "20211020220023+0000", + "commitId" : "93609146327efb378cbea19477ed638d03be1e57", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-2-bin.zip.sha256", + "checksum" : "e6a7b81aa2fd2fa05efba3cf7d0c79d629b4d3b189aa4d4b57729933a211af79", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.3-rc-1", + "buildTime" : "20211012072418+0000", + "commitId" : "d71b6cdb688f94d57ff4c27c7b185afbcb338a5f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-1-bin.zip.sha256", + "checksum" : "d23a3a902b3c4a8cc6952ff236fb83279a266aec1854b01286ed04788a5d3d9a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.3-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "6.9.1", + "buildTime" : "20210820111518+0000", + "commitId" : "f0ddb54aaae0e44f0a7209c3c0274d506ea742a0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.1-bin.zip.sha256", + "checksum" : "8c12154228a502b784f451179846e518733cf856efc7d45b2e6691012977b2fe", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9.1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.2", + "buildTime" : "20210817095903+0000", + "commitId" : "a773786b58bb28710e3dc96c4d1a7063628952ad", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-bin.zip.sha256", + "checksum" : "f581709a9c35e9cb92e16f585d2c4bc99b2b1a5f85d2badbd3dc6bff59e1e6dd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.2-rc-3", + "buildTime" : "20210812075909+0000", + "commitId" : "c85cebe8534173e68d3c36cbf78ee1eaaa408301", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-3-bin.zip.sha256", + "checksum" : "12215a4146ab05309893c24fb7d0c32366ab6b548e2de84185d781ec639a6283", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.2-rc-2", + "buildTime" : "20210805122137+0000", + "commitId" : "9a8406d602d05d95ddea924dd2443954f9b9e0c8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-2-bin.zip.sha256", + "checksum" : "e7ed72455ea34234ffb244621d384c94e86c4adfcb0bc9e7c60ff4849ee69a77", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.2-rc-1", + "buildTime" : "20210729075128+0000", + "commitId" : "d25e5bd8039ad18a2d63706b5a97cdc12e906ac3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-1-bin.zip.sha256", + "checksum" : "fb0e3b9890cbe993819154d099b9cbcfdfccb4a6bf78f9b6189871e36663ce44", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.2-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.1.1", + "buildTime" : "20210702121643+0000", + "commitId" : "774525a055494e0ece39f522ac7ad17498ce032c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.1.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.1.1-bin.zip.sha256", + "checksum" : "bf8b869948901d422e9bb7d1fa61da6a6e19411baa7ad6ee929073df85d6365d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.1.1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.1", + "buildTime" : "20210614144726+0000", + "commitId" : "989ccc9952b140ee6ab88870e8a12f1b2998369e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-bin.zip.sha256", + "checksum" : "2debee19271e1b82c6e41137d78e44e6e841035230a1a169ca47fd3fb09ed87b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.1-rc-2", + "buildTime" : "20210608153520+0000", + "commitId" : "79bbe8b8ea2f705f4af44f8e88af0d67fee6a898", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-2-bin.zip.sha256", + "checksum" : "caa8e323577de2fa421b47dded5278e7376c2bb6420221fbaf7908dfa9be8657", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.1-rc-1", + "buildTime" : "20210531095845+0000", + "commitId" : "c2d15c6444215f4b5a788a110922e3ee48a14f08", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-1-bin.zip.sha256", + "checksum" : "bac27c9878c4aa5b4b35f92105ca71de2ad39c323bc81117e611c65f2dffd941", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.1-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "33ad4583fd7ee156f533778736fa1b4940bd83b433934d1cc4e9f608e99a6a89" +}, { + "version" : "7.0.2", + "buildTime" : "20210514120231+0000", + "commitId" : "1ef1b260d39daacbf9357f9d8594a8a743e2152e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0.2-bin.zip.sha256", + "checksum" : "0e46229820205440b48a5501122002842b82886e76af35f0f3a069243dca4b3c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0.2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0.1", + "buildTime" : "20210510160858+0000", + "commitId" : "67e618faef187783dadd03a34fdab9dc71b85b19", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0.1-bin.zip.sha256", + "checksum" : "dccda8aa069563c8ba2f6cdfd0777df0e34a5b4d15138ca8b9757e94f4e8a8cb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0.1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.9", + "buildTime" : "20210507072853+0000", + "commitId" : "afe2e24ababc7b0213ccffff44970aa18035fc0e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-bin.zip.sha256", + "checksum" : "765442b8069c6bee2ea70713861c027587591c6b1df2c857a23361512560894e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.9-rc-2", + "buildTime" : "20210505141217+0000", + "commitId" : "56bca94e761b37ff00dfa1afdeda4bed4f15ae45", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-2-bin.zip.sha256", + "checksum" : "ce62238c6f84286a524590cb60706b7e6ecf00c3be5bddfa0c78345b94d0d127", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.9-rc-1", + "buildTime" : "20210427125445+0000", + "commitId" : "b110653415a762d00576628358f6bf6b9edeff25", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-1-bin.zip.sha256", + "checksum" : "0edea03c77d94adbbb02c77a8f20c73afb8caf206f364727be0517136f6fb347", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.9-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0", + "buildTime" : "20210409222731+0000", + "commitId" : "d5661e3f0e07a8caff705f1badf79fb5df8022c4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-bin.zip.sha256", + "checksum" : "eb8b89184261025b0430f5b2233701ff1377f96da1ef5e278af6ae8bac5cc305", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0-rc-2", + "buildTime" : "20210401212639+0000", + "commitId" : "912a3368b654b71250dfc92234da35a20d620393", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-2-bin.zip.sha256", + "checksum" : "a5db0c919dfd184f7408ca761b438853b55ceab20d7ebb41ea3f72befd8bfb3f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0-rc-1", + "buildTime" : "20210323010230+0000", + "commitId" : "f5bf7ade373b74058e49f07749083b4c3075549a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "7.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-1-bin.zip.sha256", + "checksum" : "12b807b5d6b065f05e0e47d8d00e9d55fe26d3cfc6cdb22d6825a93940edec90", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0-milestone-3", + "buildTime" : "20210313010321+0000", + "commitId" : "2c491436533ea8ed227f70e9246801d92848ba4a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "7.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-3-bin.zip.sha256", + "checksum" : "2e6b95b41001bd9f099b17a772c3308fe59356f8b7f995ffd7bf74400db0f13c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0-milestone-2", + "buildTime" : "20210223230254+0000", + "commitId" : "b3ebdd96f5efa645c12a2b0af93025a715197e86", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "7.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-2-bin.zip.sha256", + "checksum" : "a971b1cd25348e0dbeb56e4b7a7bb7dd06d7b092c166116f5247427f32fc4281", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8.3", + "buildTime" : "20210222161328+0000", + "commitId" : "9e26b4a9ebb910eaa1b8da8ff8575e514bc61c78", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.3-bin.zip.sha256", + "checksum" : "7faa7198769f872826c8ef4f1450f839ec27f0b4d5d1e51bade63667cbccd205", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8.2", + "buildTime" : "20210205125300+0000", + "commitId" : "b9bd4a5c6026ac52f690eaf2829ee26563cad426", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.2-bin.zip.sha256", + "checksum" : "8de6efc274ab52332a9c820366dd5cf5fc9d35ec7078fd70c8ec6913431ee610", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "7.0-milestone-1", + "buildTime" : "20210203220023+0000", + "commitId" : "d86fce68d4c6d73845da89f55390a3ef594d1767", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "7.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-1-bin.zip.sha256", + "checksum" : "627fc72166c8a6ae9bf3e6d9b59c0a8cc4efe5cd5417a2fc525b5c7d05409aa5", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-7.0-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8.1", + "buildTime" : "20210122132008+0000", + "commitId" : "31f14a87d93945024ab7a78de84102a3400fa5b2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.1-bin.zip.sha256", + "checksum" : "fd591a34af7385730970399f473afabdb8b28d57fd97d6625c388d090039d6fd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8.1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8", + "buildTime" : "20210108163846+0000", + "commitId" : "b7e82460c5373e194fb478a998c4fcfe7da53a7e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-bin.zip.sha256", + "checksum" : "e2774e6fb77c43657decde25542dea710aafd78c4022d19b196e7e78d79d8c6c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-rc-5", + "buildTime" : "20210104202203+0000", + "commitId" : "d668fa996848d20ec622982d226fe84a6a538dcf", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-5-bin.zip.sha256", + "checksum" : "569bebddc0c174475cc25d708e54af793f25fd37e5a72e53674f8ef628b6ddfd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-rc-4", + "buildTime" : "20201222084410+0000", + "commitId" : "b8b010210c7645fd62d2d73ac876445f2c2b94ae", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-4-bin.zip.sha256", + "checksum" : "6cb03b71767b169eda987338091172cbc84eb0cd53a93c17e6661688411e267e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-rc-3", + "buildTime" : "20201217142452+0000", + "commitId" : "183b081d53044028848dab1b00a05506c564dab2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-3-bin.zip.sha256", + "checksum" : "6c929633a439d248413d3d1bff477195ee662ccadc6fdc31f6f50f8059a2be36", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-rc-1", + "buildTime" : "20201126092543+0000", + "commitId" : "6817ac1108f950a6ebd6ed3f74e4e86efef9246a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-1-bin.zip.sha256", + "checksum" : "155bafc3a46f81b7af481ad7dba391a3b9c12818f27da8684178ffeaa40e6d7e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7.1", + "buildTime" : "20201116170924+0000", + "commitId" : "2972ff02f3210d2ceed2f1ea880f026acfbab5c0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7.1-bin.zip.sha256", + "checksum" : "3239b5ed86c3838a37d983ac100573f64c1f3fd8e1eb6c89fa5f9529b5ec091d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7.1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-milestone-3", + "buildTime" : "20201115200053+0000", + "commitId" : "31e2ecb5fb775e8ec16cbd3fafe61cc29a27fb9d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.8", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-3-bin.zip.sha256", + "checksum" : "47fa64cc30ab93a78901bdae68f33410da070c46f750f7e7978a6fb2a7b7ab50", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-milestone-2", + "buildTime" : "20201028055518+0000", + "commitId" : "d75d27c882fd80a18d853e0eb8aa0a41f9fdf6b0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.8", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-2-bin.zip.sha256", + "checksum" : "0e15751be3270c3cbff10742cc259cbd3b92f379204d645397541f676b0667f6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7", + "buildTime" : "20201014161312+0000", + "commitId" : "312ba9e0f4f8a02d01854d1ed743b79ed996dfd3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-bin.zip.sha256", + "checksum" : "8ad57759019a9233dc7dc4d1a530cefe109dc122000d57f7e623f8cf4ba9dfc4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.8-milestone-1", + "buildTime" : "20201013065906+0000", + "commitId" : "cffde3dcc3794fbe1f08373bf38df91b8a5abbc8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.8", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-1-bin.zip.sha256", + "checksum" : "cddca6851af8503eb7c8bf5ba778c4c9e878a7b6cf74708fda09cae1952656fc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.8-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7-rc-5", + "buildTime" : "20201012190940+0000", + "commitId" : "e019f250eece15efaddc7c812d56b34583950a40", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-5-bin.zip.sha256", + "checksum" : "c7c33aa33d32e4bd68df71deb5c6d00f2c4675d88b8a14748661196a717dfe2c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7-rc-4", + "buildTime" : "20201008165738+0000", + "commitId" : "40a302302ce9adfc1dc21dbc4cecae16dd980e5d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-4-bin.zip.sha256", + "checksum" : "01b4f3804b6c3f15ff3eb39e56efae55e265b6007c9fa3bd58b83b2dc4697dc4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7-rc-3", + "buildTime" : "20200930191651+0000", + "commitId" : "836e96a80625c9c48b612e662e3b13bd2e2f4c3b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-3-bin.zip.sha256", + "checksum" : "f4a236ffc777d6a4c25cdf7b1c45fe7024910afc65c88a9c9027aab9a765d295", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7-rc-2", + "buildTime" : "20200925201444+0000", + "commitId" : "a57db4f9f8ee3f033fd1137d97d351d6b9c637fd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-2-bin.zip.sha256", + "checksum" : "34d87e1fe52cb50b9670cd7c817843273d40afa572a2d4f1b6f866daedfee8af", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.7-rc-1", + "buildTime" : "20200911161705+0000", + "commitId" : "fe783600075485236c778e073cad5640729cf8f5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-1-bin.zip.sha256", + "checksum" : "77822eaca04dd0e1c59065877ab22b96f0b0c5d5a65df8f63a58ae31f8938c8a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.7-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6.1", + "buildTime" : "20200825162912+0000", + "commitId" : "f2d1fb54a951d8b11d25748e4711bec8d128d7e3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6.1-bin.zip.sha256", + "checksum" : "7873ed5287f47ca03549ab8dcb6dc877ac7f0e3d7b1eb12685161d10080910ac", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6.1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6", + "buildTime" : "20200810220619+0000", + "commitId" : "d119144684a0c301aea027b79857815659e431b9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-bin.zip.sha256", + "checksum" : "e6f83508f0970452f56197f610d13c5f593baaf43c0e3c6a571e5967be754025", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-6", + "buildTime" : "20200805195744+0000", + "commitId" : "d4d3dd932ea87b4f1c470c93df56dfe00862d710", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-6-bin.zip.sha256", + "checksum" : "5d24a11971f2324bf55899e9c5cd84e85fe94c63ff0456b10a53f93f7a14d288", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-6-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-5", + "buildTime" : "20200803185444+0000", + "commitId" : "899b462147bbdf177c655173433336ffe6a93de9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-5-bin.zip.sha256", + "checksum" : "076f1c7d463cb8fa5fec51e1818e18fed9e6c5c6dc06ffa6325eef07a9746661", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-5-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-4", + "buildTime" : "20200730144824+0000", + "commitId" : "b573ec13e0627bf831dcaa0cfc67403c0a93fb33", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-4-bin.zip.sha256", + "checksum" : "f743d14deb36c6f1cfdf0480452e369d3098f63034c4e2c92fc1154740cb7bce", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-3", + "buildTime" : "20200724140409+0000", + "commitId" : "13b5dcc4f294b395e61d937a3601fd47be38e76e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-3-bin.zip.sha256", + "checksum" : "030078a5900466f172f1e59e0f3351280328693929de08ab10747103fb6d9faf", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-2", + "buildTime" : "20200720214344+0000", + "commitId" : "ff063bb2d5573aeee22c08133a268c6545195153", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-2-bin.zip.sha256", + "checksum" : "c981f305803093525a64aeee8adb356ef2b909b01b44f11a70c6ed6d6ea92fc5", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-rc-1", + "buildTime" : "20200713135325+0000", + "commitId" : "1ed79f4fbe18d90df8cb759235804f95a99b30a2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-1-bin.zip.sha256", + "checksum" : "2253ab246976353b225002ecba04859b5dbad0620f57052ef057fdcfcb888c5c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-milestone-3", + "buildTime" : "20200707160951+0000", + "commitId" : "e3fcd2f83cbe862e41a1c12df3213be5698041b5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.6", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-3-bin.zip.sha256", + "checksum" : "c85644cecb8276ee7621ed7b7309897f5927b175a3e658b54d25889aaf7cfeb3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.5.1", + "buildTime" : "20200630063247+0000", + "commitId" : "66bc713f7169626a7f0134bf452abde51550ea0a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.5.1-bin.zip.sha256", + "checksum" : "50a7d30529fa939721fe9268a0205142f3f2302bcac5fb45b27a3902e58db54a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.5.1-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.6-milestone-2", + "buildTime" : "20200623122834+0000", + "commitId" : "19fa87a998bd6ec4c21ba50dbf6fe9d12f374193", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.6", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-2-bin.zip.sha256", + "checksum" : "c7a17a7816572ec0ec3632fcae4e2d877db03cb53b9127e9f5ab60529e1699e2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.6-milestone-1", + "buildTime" : "20200611202418+0000", + "commitId" : "bc05809b2ebf24012a91dcb7f8a5cab83f94016a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.6", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-1-bin.zip.sha256", + "checksum" : "287d42827c2da3a27c8826cb791af3788ac7ed3bceed415125512c4140706c14", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.6-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "e996d452d2645e70c01c11143ca2d3742734a28da2bf61f25c82bdc288c9e637" +}, { + "version" : "6.5", + "buildTime" : "20200602204621+0000", + "commitId" : "a27f41e4ae5e8a41ab9b19f8dd6d86d7b384dad4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-bin.zip.sha256", + "checksum" : "23e7d37e9bb4f8dabb8a3ea7fdee9dd0428b9b1a71d298aefd65b11dccea220f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.5-rc-1", + "buildTime" : "20200526200448+0000", + "commitId" : "2ce1bc8db4b993f0dd1d8834be5b53e43f657985", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-rc-1-bin.zip.sha256", + "checksum" : "fd64fa437cb3dfb53a037fff72cbe24e1280bc83ceb3bd35b2f96637ab6efeff", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4.1", + "buildTime" : "20200515194340+0000", + "commitId" : "1a04183c502614b5c80e33d603074e0b4a2777c5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4.1-bin.zip.sha256", + "checksum" : "e58cdff0cee6d9b422dcd08ebeb3177bc44eaa09bd9a2e838ff74c408fe1cbcd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4.1-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.5-milestone-2", + "buildTime" : "20200515094006+0000", + "commitId" : "a1fbc4cd16fd3f9c51345111a4bd8a03364e34cd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.5", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-2-bin.zip.sha256", + "checksum" : "3e269c619d1d19cbbcbd74165487a973d8ad307def9552883e601a93b60ced4b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.5-milestone-1", + "buildTime" : "20200507135506+0000", + "commitId" : "c123669c38bdc112ea577e999a0d6c1bd43af8a1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.5", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-1-bin.zip.sha256", + "checksum" : "7e6ac72a36af92474ee8c4ddae73cbc3cc5e6be33fb088ff6668465b25addcd9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.5-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4", + "buildTime" : "20200505191855+0000", + "commitId" : "42f7c3d0c3066b7b38bd0726760d4881e86fd19f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-bin.zip.sha256", + "checksum" : "b888659f637887e759749f6226ddfcb1cb04f828c58c41279de73c463fdbacc9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4-rc-4", + "buildTime" : "20200430212330+0000", + "commitId" : "9aca99e4db8808881606548487fa98e012fb39e1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-4-bin.zip.sha256", + "checksum" : "7307d73b87b4e2416eeb6a376ac7f1bcce6c7cff8eb87e6dca2e130f0016858b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4-rc-3", + "buildTime" : "20200428190039+0000", + "commitId" : "87fd55432350f0e70a2498bf643a9be319d3bb4a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-3-bin.zip.sha256", + "checksum" : "b80df15a8398f020e5689233d912704e42c9dc567cd32c539a3c59f8616e8954", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4-rc-2", + "buildTime" : "20200422163205+0000", + "commitId" : "20a24c0cf0c107d7560b7656a96bd346bae4947b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-2-bin.zip.sha256", + "checksum" : "a74a1d4ff45e7b6f42c7384546bba2620e23a1172d4b3dcfc1418000ad5d793c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.4-rc-1", + "buildTime" : "20200415202839+0000", + "commitId" : "4162fd0e34887a8c600495239d421cecaa5f17ff", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-1-bin.zip.sha256", + "checksum" : "f8e14fc40ebc468d578601cfeaef402844048ff8fc727d452e6b28922c53459c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.4-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "70239e6ca1f0d5e3b2808ef6d82390cf9ad58d3a3a0d271677a51d1b89475857" +}, { + "version" : "6.3", + "buildTime" : "20200324195207+0000", + "commitId" : "bacd40b727b0130eeac8855ae3f9fd9a0b207c60", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-bin.zip.sha256", + "checksum" : "038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-wrapper.jar.sha256", + "wrapperChecksum" : "1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06" +}, { + "version" : "6.3-rc-4", + "buildTime" : "20200320053945+0000", + "commitId" : "507eaa2e999827ec256a700ffc413c5e92a9e69d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-4-bin.zip.sha256", + "checksum" : "eb17f321c7892267869541962484de3c375b12dfe98dfe2a55139996d1e71aec", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06" +}, { + "version" : "6.3-rc-3", + "buildTime" : "20200318152649+0000", + "commitId" : "da54f723f8f1c5f94526bc72bf78f15b0f1409ec", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-3-bin.zip.sha256", + "checksum" : "492b013ee962016f83a49defd221bd1fdc80003a9d50a9ad3c5f869c8f48c864", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06" +}, { + "version" : "6.3-rc-2", + "buildTime" : "20200317124036+0000", + "commitId" : "2cc0a9fd7e59c7876fe2fac480124f07ea31be5b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-2-bin.zip.sha256", + "checksum" : "7431e8b723dbe8fea36b635ec578a676df5458a21faa7aaabc8279dc5f9c86dc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06" +}, { + "version" : "6.3-rc-1", + "buildTime" : "20200311214114+0000", + "commitId" : "7bc8f57a5b03db954a651cf24b5247aa9629e72f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-1-bin.zip.sha256", + "checksum" : "b72707eb36a9e6fffb8dbe1653c59d8cbd7e4b839f4ef6131c46629dd9fa5686", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.3-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "1cef53de8dc192036e7b0cc47584449b0cf570a00d560bfaa6c9eabe06e1fc06" +}, { + "version" : "6.2.2", + "buildTime" : "20200304084931+0000", + "commitId" : "7d0bf6dcb46c143bcc3b7a0fa40a8e5ca28e5856", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2.2-bin.zip.sha256", + "checksum" : "0f6ba231b986276d8221d7a870b4d98e0df76e6daf1f42e7c0baec5032fb7d17", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2.2-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.2.1", + "buildTime" : "20200224202410+0000", + "commitId" : "aacbcb7e587faa6a8e7851751a76183b6187b164", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2.1-bin.zip.sha256", + "checksum" : "a68ca7ba57f3404c3f6fc1f70a02d3a7d78652e6b46bbfaff83fc9a17168c279", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.2", + "buildTime" : "20200217083201+0000", + "commitId" : "61d3320259a1a0d31519bf208eb13741679a742f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-bin.zip.sha256", + "checksum" : "b93a5f30d01195ec201e240f029c8b42d59c24086b8d1864112c83558e23cf8a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.2-rc-3", + "buildTime" : "20200213012914+0000", + "commitId" : "68b48dea20ab558725a7f61ab1e523253df94495", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-3-bin.zip.sha256", + "checksum" : "3e8db98ac4398938ffcdb95241518eadf8858cee65f951f5f75e22cf5efb4cc2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.2-rc-2", + "buildTime" : "20200211221400+0000", + "commitId" : "8a7f6425a217ceeff6c5a76f8dd533b770606110", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-2-bin.zip.sha256", + "checksum" : "cf53ad90046348ab326f07b77f7362d6e8f101125ddd815e46df2f296ed2a0ee", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.2-rc-1", + "buildTime" : "20200203123003+0000", + "commitId" : "3931d0c37f00de3bd45f713520bd47a09b6bad30", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-1-bin.zip.sha256", + "checksum" : "4e40b36d619f5e7a9eb9a97d6652c062a13df828995be79137b7215c062b9139", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.2-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1.1", + "buildTime" : "20200124223024+0000", + "commitId" : "a8c3750babb99d1894378073499d6716a1a1fa5d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1.1-bin.zip.sha256", + "checksum" : "9d94e6e4a28ad328072ef6e56bce79a810494ae756751fdcedffdeaf27c093b1", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1.1-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1", + "buildTime" : "20200115235646+0000", + "commitId" : "539d277fdba571ebcc9617a34329c83d7d2b259e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-bin.zip.sha256", + "checksum" : "d0c43d14e1c70a48b82442f435d06186351a2d290d72afd5b8866f15e6d7038a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1-rc-3", + "buildTime" : "20200113162324+0000", + "commitId" : "52aebd9acc0e395deba9a17db22f6cffe7d61cf1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-3-bin.zip.sha256", + "checksum" : "f150eccc7ab05d14bd89a04cdfc4de94e320d5962828e69d10c43bfff12d176d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1-rc-2", + "buildTime" : "20200107211705+0000", + "commitId" : "8ea1a9823c6a4bfe11b14a3fcbed0630992b126f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-2-bin.zip.sha256", + "checksum" : "f451724e3f6157893e6efd98f91e26fbed1bac89d43fd248d4535cc918b83ee7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1-rc-1", + "buildTime" : "20191219215643+0000", + "commitId" : "091da954cc24262bb056965f70f172e1e587e9ca", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-1-bin.zip.sha256", + "checksum" : "8262a8509cdd281ab19b87c79f03d4a5d0629f2a03a28c284203431f12c70960", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "96f793a18e056c23ffeec67c1f3bb8eccff5a4a407fc9ceac183527e7eedf4b6" +}, { + "version" : "6.1-milestone-3", + "buildTime" : "20191203141333+0000", + "commitId" : "b360d725d00cc62bad1e1f95f66077b18b5cf9a0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-3-bin.zip.sha256", + "checksum" : "31b51f1c7db4aa3927c3621a5732e777f7655f7b4d769225337c251cfbb8cdf6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-3-wrapper.jar.sha256", + "wrapperChecksum" : "3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce" +}, { + "version" : "6.1-milestone-2", + "buildTime" : "20191128101755+0000", + "commitId" : "0f4f797500d442efec2e41485029d66470049b4a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-2-bin.zip.sha256", + "checksum" : "3c2040ce128a323e96163eb2e87ac7a50475756f767fa7a139e0edc34447548e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-2-wrapper.jar.sha256", + "wrapperChecksum" : "3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce" +}, { + "version" : "6.1-milestone-1", + "buildTime" : "20191120132405+0000", + "commitId" : "0e10d43391411ce56234fec67bfa38066df5f191", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "6.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-1-bin.zip.sha256", + "checksum" : "b13a85987bf9b2b8aa7c24a9b8e68c46a32acb6b623477ac457bca8b0ea9fc04", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.1-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce" +}, { + "version" : "6.0.1", + "buildTime" : "20191118202501+0000", + "commitId" : "fad121066a68c4701acd362daf4287a7c309a0f5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.0.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.0.1-bin.zip.sha256", + "checksum" : "d364b7098b9f2e58579a3603dc0a12a1991353ac58ed339316e6762b21efba44", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.0.1-wrapper.jar.sha256", + "wrapperChecksum" : "28b330c20a9a73881dfe9702df78d4d78bf72368e8906c70080ab6932462fe9e" +}, { + "version" : "6.0", + "buildTime" : "20191108181212+0000", + "commitId" : "0a5b531749138f2f983f7c888fa7790bfc52d88a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-bin.zip.sha256", + "checksum" : "5a3578b9f0bb162f5e08cf119f447dfb8fa950cedebb4d2a977e912a11a74b91", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-wrapper.jar.sha256", + "wrapperChecksum" : "28b330c20a9a73881dfe9702df78d4d78bf72368e8906c70080ab6932462fe9e" +}, { + "version" : "6.0-rc-3", + "buildTime" : "20191105175639+0000", + "commitId" : "74fa127e4a6787a1983bd8467e9443662fa0fe3d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-3-bin.zip.sha256", + "checksum" : "d30c88a6644e28ba5a45923ae3a4850d55ba0f35dd14a3108595359cdfb0401e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "28b330c20a9a73881dfe9702df78d4d78bf72368e8906c70080ab6932462fe9e" +}, { + "version" : "5.6.4", + "buildTime" : "20191101204200+0000", + "commitId" : "dd870424f9bd8e195d614dc14bb140f43c22da98", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.4-bin.zip.sha256", + "checksum" : "1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.4-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "6.0-rc-2", + "buildTime" : "20191030160321+0000", + "commitId" : "990710705df1acf6809b5982a37307677a436d6f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-2-bin.zip.sha256", + "checksum" : "dbb366685bd15d08e592e384744bc19715d00203adeacf43dcf4c8c3a46b6b01", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "28b330c20a9a73881dfe9702df78d4d78bf72368e8906c70080ab6932462fe9e" +}, { + "version" : "6.0-rc-1", + "buildTime" : "20191018192416+0000", + "commitId" : "5ed5fea1446b3300afa321c661d6e865ab3157a1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "6.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-1-bin.zip.sha256", + "checksum" : "e73ab0c91f71e051f9798c6c89e0b9c0ecc95f0877e683554425c68835998509", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-6.0-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "28b330c20a9a73881dfe9702df78d4d78bf72368e8906c70080ab6932462fe9e" +}, { + "version" : "5.6.3", + "buildTime" : "20191018002836+0000", + "commitId" : "bd168bbf5d152c479186a897f2cea494b7875d13", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.3-bin.zip.sha256", + "checksum" : "60a6d8f687e3e7a4bc901cc6bc3db190efae0f02f0cc697e323e0f9336f224a3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.3-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.6.2", + "buildTime" : "20190905161354+0000", + "commitId" : "55a5e53d855db8fc7b0e494412fc624051a8e781", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.2-bin.zip.sha256", + "checksum" : "32fce6628848f799b0ad3205ae8db67d0d828c10ffe62b748a7c0d9f4a5d9ee0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.2-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.6.1", + "buildTime" : "20190828024934+0000", + "commitId" : "b6bd8e7934ca41d9e52610058aa7cb834df81fc4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.1-bin.zip.sha256", + "checksum" : "0986244820e4a35d32d91df2ec4b768b5ba5d6c8246753794f85159f9963ec12", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6.1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.6", + "buildTime" : "20190814210525+0000", + "commitId" : "f0b9d60906c7b8c42cd6c61a39ae7b74767bb012", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-bin.zip.sha256", + "checksum" : "15c02ef5dd3631ec02ac52e8725703e0285d9a7eecbf4e5939aa9e924604d01d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.6-rc-2", + "buildTime" : "20190807150342+0000", + "commitId" : "19680ca6170bfa0a541cc161004c95fe23bb3e36", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-2-bin.zip.sha256", + "checksum" : "26c4451c7297dd160314fe509273f97c4f1ee68527dc3fda3d87f0973df8eecb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.6-rc-1", + "buildTime" : "20190729112626+0000", + "commitId" : "f51e6f079cea308de4ef2fb04bdc3b108db6eeaf", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-1-bin.zip.sha256", + "checksum" : "e09932eeca0e94f08bf81b1b58bc199ee8b8044d553c9ed5e1a906b641ce9c90", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.6-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5.1", + "buildTime" : "20190710203812+0000", + "commitId" : "3245f748c7061472da4dc184991919810f7935a5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5.1-bin.zip.sha256", + "checksum" : "222a03fcf2fcaf3691767ce9549f78ebd4a77e73f9e23a396899fb70b420cd00", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5.1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5", + "buildTime" : "20190628173605+0000", + "commitId" : "83820928f3ada1a3a1dbd9a6c0d47eb3f199378f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-bin.zip.sha256", + "checksum" : "8d78b2ed63e7f07ad169c1186d119761c4773e681f332cfe1901045b1b0141bc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5-rc-4", + "buildTime" : "20190624152432+0000", + "commitId" : "787552139af6f0f19c748ac6a917a54d33f15ab0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-4-bin.zip.sha256", + "checksum" : "8d0844d974a2da25b741382094cb63d035ad2e9572b978e63bdceb927231e9ea", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-4-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5-rc-3", + "buildTime" : "20190614231538+0000", + "commitId" : "37d86b7ed068116d20e7a31c88d9dfe3e8ec3bd8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-3-bin.zip.sha256", + "checksum" : "1d3f37c0b53084ba5e6642691a9b9fb5f588a801f8ca613f15d82042b2c7dad8", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5-rc-2", + "buildTime" : "20190607090657+0000", + "commitId" : "360949f7e785578195c7fe7e4b6ca2dc566d55c5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-2-bin.zip.sha256", + "checksum" : "440429bd2195030d944b8d148c0d056edb2463813a04132ee7bf7123b471858a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.5-rc-1", + "buildTime" : "20190529115119+0000", + "commitId" : "6c070eb6e47979d5642a181e3efa11bf237dc69c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-1-bin.zip.sha256", + "checksum" : "7fe31cb63223b292fae0c42b124de3d8d028a3567992e9caf08a240c10d48f6c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.5-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.4.1", + "buildTime" : "20190426081442+0000", + "commitId" : "261d171646b36a6a28d5a19a69676cd098a4c19d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.4.1-bin.zip.sha256", + "checksum" : "7bdbad1e4f54f13c8a78abc00c26d44dd8709d4aedb704d913fb1bb78ac025dc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.4.1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.4", + "buildTime" : "20190416024416+0000", + "commitId" : "a4f3f91a30d4e36d82cc7592c4a0726df52aba0d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.4-bin.zip.sha256", + "checksum" : "c8c17574245ecee9ed7fe4f6b593b696d1692d1adbfef425bef9b333e3a0e8de", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.4-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.4-rc-1", + "buildTime" : "20190410011532+0000", + "commitId" : "efacce4b4916e5709f25141b61fc3ec594d3893d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.4-rc-1-bin.zip.sha256", + "checksum" : "86dbc7499f0cc0810c2ca8683b5adcd0dd7841bfe14544401de0e49b885542a2", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.4-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.3.1", + "buildTime" : "20190328090923+0000", + "commitId" : "f2fae6ba563cfb772c8bc35d31e43c59a5b620c3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.3.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.3.1-bin.zip.sha256", + "checksum" : "1c59a17a054e9c82f0dd881871c9646e943ec4c71dd52ebc6137d17f82337436", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.3.1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.3", + "buildTime" : "20190320110329+0000", + "commitId" : "f5c64796748a98efdbf6f99f44b6afe08492c2a0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-bin.zip.sha256", + "checksum" : "bed2bdd3955be5a09ca7e0201e9d131f194f7f6c466e1795a733733ccfb09f25", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.3-rc-3", + "buildTime" : "20190313202708+0000", + "commitId" : "4b3a8d84d9a8983836f9bb3006c39baec692ca4b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-3-bin.zip.sha256", + "checksum" : "63b7114caa8629f5dc3074aab09b87476fbfba548ea20aefe97068287621acb7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.3-rc-2", + "buildTime" : "20190311210726+0000", + "commitId" : "cb19a113017a9351f8d9fa767d50a9a0f590617a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-2-bin.zip.sha256", + "checksum" : "4110201a819b28600bfe06ef02950c6b749616c2197ed0da2514451a378c709b", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.3-rc-1", + "buildTime" : "20190305205202+0000", + "commitId" : "ec8a8bc2dd4c0761880ccec4f28c5db79c2244bb", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-1-bin.zip.sha256", + "checksum" : "5650e801e191cc8f12f6ea3f27e081ad0a0012d44d53ba226b7c148d3282956c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.3-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "3dc39ad650d40f6c029bd8ff605c6d95865d657dbfdeacdb079db0ddfffedf9f" +}, { + "version" : "5.2.1", + "buildTime" : "20190208190010+0000", + "commitId" : "f02764e074c32ee8851a4e1877dd1fea8ffb7183", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.2.1-bin.zip.sha256", + "checksum" : "748c33ff8d216736723be4037085b8dc342c6a0f309081acf682c9803e407357", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.2", + "buildTime" : "20190204111648+0000", + "commitId" : "840644a429c8b8b9ae399867eb1660e3109bc7a3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.2-bin.zip.sha256", + "checksum" : "ff322863250159595e93b5a4d17a6f0d21c59a1a0497c1e1cf1d53826485503f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.2-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.2-rc-1", + "buildTime" : "20190128225604+0000", + "commitId" : "633c8d5c1c568402d7a26e94c1213d747bfa594b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.2-rc-1-bin.zip.sha256", + "checksum" : "9e882e3899d7ef999f862f71a4a7494505a9a3f87c1cea427a3ae44941b94c70", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.2-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.1.1", + "buildTime" : "20190110230502+0000", + "commitId" : "3c9abb645fb83932c44e8610642393ad62116807", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1.1-bin.zip.sha256", + "checksum" : "4953323605c5d7b89e97d0dc7779e275bccedefcdac090aec123375eae0cc798", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1.1-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.1", + "buildTime" : "20190102185747+0000", + "commitId" : "d09c2e354576ac41078c322815cc6db2b66d976e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-bin.zip.sha256", + "checksum" : "7506638a380092a0406364c79d6c87d03d23017fc25a5770379d1ce23c3fcd4d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.1-rc-3", + "buildTime" : "20181221230345+0000", + "commitId" : "bf267654ac894c160b5b6c5e5c06d72f1ecaa23c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-3-bin.zip.sha256", + "checksum" : "aba05e1d60c4b976e8bb9ea1911aaa8b19783cdc63e788320fcb75e9613ba45d", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-3-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.1-rc-2", + "buildTime" : "20181217224201+0000", + "commitId" : "4f93941ff2f23ed981b9416a66af36ab0a8ef4de", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-2-bin.zip.sha256", + "checksum" : "a5f43f07530f2e02f4df3b0fe14300586d6302ad4462d0ed95971e6bf107b66a", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-2-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "5.1-rc-1", + "buildTime" : "20181213060004+0000", + "commitId" : "0937d57caa43dabc31d331dea6c9c0cf72de8607", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-1-bin.zip.sha256", + "checksum" : "160b5b497b076c8d490fd7f2de6d389db4972dc495d5b86e5713ebdf63bf47fd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-rc-1-wrapper.jar.sha256", + "wrapperChecksum" : "76b12da7f4a7cdd025e5996811a2e49bf5df0fb62d72554ab555c0e434b63aae" +}, { + "version" : "4.10.3", + "buildTime" : "20181205005054+0000", + "commitId" : "e76905e3a1034e6f724566aeb985621347ff43bc", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.3-bin.zip.sha256", + "checksum" : "8626cbf206b4e201ade7b87779090690447054bc93f052954c78480fa6ed186e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.3-wrapper.jar.sha256", + "wrapperChecksum" : "660ab018b8e319e9ae779fdb1b7ac47d0321bde953bf0eb4545f14952cfdcaa3" +}, { + "version" : "5.1-milestone-1", + "buildTime" : "20181203174147+0000", + "commitId" : "00340d696f2e7161233ac9a363cd4e5238588318", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "5.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.1-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-milestone-1-bin.zip.sha256", + "checksum" : "f7f38376ef3cf461eaf4da9d7abf4a9a4325cbbc93fbe19efbdb2f35c4171757", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.1-milestone-1-wrapper.jar.sha256", + "wrapperChecksum" : "8ff6bee43c55efc0cce9e1147860a76fc970398fbef587e64b6e7a5a7e0291df" +}, { + "version" : "5.0", + "buildTime" : "20181126114843+0000", + "commitId" : "7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-bin.zip.sha256", + "checksum" : "6157ac9f3410bc63644625b3b3e9e96c963afd7910ae0697792db57813ee79a6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-wrapper.jar.sha256", + "wrapperChecksum" : "f1a597a1f2b23089deec11d5b924d074f9e4ed810f2093be7021ded01c8073ad" +}, { + "version" : "5.0-rc-5", + "buildTime" : "20181122195845+0000", + "commitId" : "42e59c2484a49702234030881640aa8d8bfa6d43", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-5-bin.zip.sha256", + "checksum" : "369ff446973f5309c550b9ef543d5cdf45470a0db20e3f6c00dcca122faa045d" +}, { + "version" : "5.0-rc-4", + "buildTime" : "20181120092642+0000", + "commitId" : "50c28fc7284c0aa377c8edb49723d9258565780f", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-4-bin.zip.sha256", + "checksum" : "414d6ffa6a4ec14355eb78b7577576ab63d07658ac640ec41b5e63b573400de0" +}, { + "version" : "5.0-rc-3", + "buildTime" : "20181114160147+0000", + "commitId" : "63f11c722124617f7cbe2f95ad5a5e045b8b42f6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-3-bin.zip.sha256", + "checksum" : "684a20ad90fc0d133c7b03da392c21e2a211b5df32faacb9f72e6847628aea20" +}, { + "version" : "5.0-rc-2", + "buildTime" : "20181112080541+0000", + "commitId" : "bcbd24a720641ddf900df0503def5b5c08c4a8b4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-2-bin.zip.sha256", + "checksum" : "6585344d4a6047bcd19894c9d75e32d62a0a908b6886c3f21049b7d9e3a14d02" +}, { + "version" : "5.0-rc-1", + "buildTime" : "20181030135922+0000", + "commitId" : "0b94d7e5908d1d96b8a353d1f28d9c443d767327", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "5.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-rc-1-bin.zip.sha256", + "checksum" : "6494ddd4191b99d1a5e8d31fc179d1cab68631ebbac9adfcaa229f56496c5427" +}, { + "version" : "5.0-milestone-1", + "buildTime" : "20181002224615+0000", + "commitId" : "abaf9d545140f5775f3442e4cfe9c61603d4fb3e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "5.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-5.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-5.0-milestone-1-bin.zip.sha256", + "checksum" : "0c4e5366b479934844da39c156c20d509f6b2c40b978c10598221fd591c0cf57" +}, { + "version" : "4.10.2", + "buildTime" : "20180919181015+0000", + "commitId" : "b4d8d5d170bb4ba516e88d7fe5647e2323d791dd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.2-bin.zip.sha256", + "checksum" : "b49c6da1b2cb67a0caf6c7480630b51c70a11ca2016ff2f555eaeda863143a29", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.2-wrapper.jar.sha256", + "wrapperChecksum" : "ad63ba21fb91e490e0f6fd0ca7d4049241f0f68a454b0b3075c041c4554e611c" +}, { + "version" : "4.10.1", + "buildTime" : "20180912113327+0000", + "commitId" : "76c9179ea9bddc32810f9125ad97c3315c544919", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.1-bin.zip.sha256", + "checksum" : "e53ce3a01cf016b5d294eef20977ad4e3c13e761ac1e475f1ffad4c6141a92bd", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.10.1-wrapper.jar.sha256", + "wrapperChecksum" : "d8a69ca8efe271d8de080c42a2ea4b08fc9e85c41aa2d163255c70d9da239db0" +}, { + "version" : "4.10", + "buildTime" : "20180827183506+0000", + "commitId" : "ee3751ed9f2034effc1f0072c2b2ee74b5dce67d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10-bin.zip.sha256", + "checksum" : "248cfd92104ce12c5431ddb8309cf713fe58de8e330c63176543320022f59f18", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.10-wrapper.jar.sha256", + "wrapperChecksum" : "778e7f46bd67eaea2de5bcbdbb40878c6614656014ba59a72ce8648eaf43a925" +}, { + "version" : "4.10-rc-3", + "buildTime" : "20180823120756+0000", + "commitId" : "1b827f2119c11123e178e731421ae79d52a8991a", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-3-bin.zip.sha256", + "checksum" : "4e5f3a2e0c4f1f0313211253d799dd66570afdf0ebb3d785ee97a97d942c632d" +}, { + "version" : "4.10-rc-2", + "buildTime" : "20180814180001+0000", + "commitId" : "7d610b59feb93583b5d19b01ce63477472d87696", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-2-bin.zip.sha256", + "checksum" : "e90d3c32910e259814bcca82b3911172ecca1ff1ab5ed69b4de3c1df8b378b40" +}, { + "version" : "4.10-rc-1", + "buildTime" : "20180809061937+0000", + "commitId" : "97951b7f541f1da43de291246cc7b17507e75a14", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.10-rc-1-bin.zip.sha256", + "checksum" : "f68220df44d943418a49aecccf0d5b8547f52201926bc8f13c2c3e5b81eb763a" +}, { + "version" : "4.9", + "buildTime" : "20180716081403+0000", + "commitId" : "efcf8c1cf533b03c70f394f270f46a174c738efc", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.9-bin.zip.sha256", + "checksum" : "e66e69dce8173dd2004b39ba93586a184628bc6c28461bc771d6835f7f9b0d28", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.9-wrapper.jar.sha256", + "wrapperChecksum" : "e55e7e47a79e04c26363805b31e2f40b7a9cc89ea12113be7de750a3b2cede85" +}, { + "version" : "4.9-rc-2", + "buildTime" : "20180711004640+0000", + "commitId" : "9b5ff22063b9eb8bab8a2f304d47903b531054c7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.9-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.9-rc-2-bin.zip.sha256", + "checksum" : "651e3d8c41221e7e767f23a5ead0bbe06ada6abec017e7b6ce99f3cbcb4808e5" +}, { + "version" : "4.9-rc-1", + "buildTime" : "20180703131446+0000", + "commitId" : "c38f1f17599655fc0de0ea44a8d366aebcfdb9f2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.9-rc-1-bin.zip.sha256", + "checksum" : "52e32ccfa4edc679e6bfad93e0f9fcd6358dc39dfb7bf8f2e1222b8d18ead359" +}, { + "version" : "4.8.1", + "buildTime" : "20180621075306+0000", + "commitId" : "0abdea078047b12df42e7750ccba34d69b516a22", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.8.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.8.1-bin.zip.sha256", + "checksum" : "af334d994b5e69e439ab55b5d2b7d086da5ea6763d78054f49f147b06370ed71", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.8.1-wrapper.jar.sha256", + "wrapperChecksum" : "080e30657661539701b66827b96eb0043191e0a7a73090e8a57bd6735e5af5c5" +}, { + "version" : "4.8", + "buildTime" : "20180604103958+0000", + "commitId" : "9e1261240e412cbf61a5e3a5ab734f232b2f887d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.8-bin.zip.sha256", + "checksum" : "f3e29692a8faa94eb0b02ebf36fa263a642b3ae8694ef806c45c345b8683f1ba", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.8-wrapper.jar.sha256", + "wrapperChecksum" : "4160d5a6d8d6efc6af336582bbbba8194e4d7a742835f7b0fd3964cbd419c994" +}, { + "version" : "4.8-rc-3", + "buildTime" : "20180531140436+0000", + "commitId" : "9803e11cf1218511e0fd1595bdc66e0a6210a0b6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-3-bin.zip.sha256", + "checksum" : "459195ae148201ef4c1235cfca08aaa20b1293f280fb643c666499a58e228dac" +}, { + "version" : "4.8-rc-2", + "buildTime" : "20180524092821+0000", + "commitId" : "0b1824635622a85b201776fc1245b6a40f4a5b22", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-2-bin.zip.sha256", + "checksum" : "a89682f4c2ec77a2b49ca139628e95de7b7648552643edd2ea3988cbd0a0a07b" +}, { + "version" : "4.8-rc-1", + "buildTime" : "20180515173233+0000", + "commitId" : "cfb66ee34d37b52c741150a398663618a94d2045", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.8-rc-1-bin.zip.sha256", + "checksum" : "c6ba15fbb51ac0c8b370dfd3a239c3b48b1cee5bbfdf5597cb586a216f8690b0" +}, { + "version" : "4.7", + "buildTime" : "20180418090912+0000", + "commitId" : "b9a962bf70638332300e7f810689cb2febbd4a6c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.7-bin.zip.sha256", + "checksum" : "fca5087dc8b50c64655c000989635664a73b11b9bd3703c7d6cabd31b7dcdb04", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.7-wrapper.jar.sha256", + "wrapperChecksum" : "7d6fe0a055f133226409de2457fb0e887d1f6b096f36c8d5fd76fb7a9357cd45" +}, { + "version" : "4.7-rc-2", + "buildTime" : "20180413125323+0000", + "commitId" : "943291f9d94f68a0172ce7648b6a9dab7711fd47", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.7-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.7-rc-2-bin.zip.sha256", + "checksum" : "b9289ecb1f9ce8bc325ce71805ee779f3e03da22e394deced6e682cd6297d110" +}, { + "version" : "4.7-rc-1", + "buildTime" : "20180403113602+0000", + "commitId" : "2f0b7b15068980c05470af1882d79e9c8ef5c71b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.7-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.7-rc-1-bin.zip.sha256", + "checksum" : "fbf286558c27f6026c0da6daa57750ce1539e6535227e6738a6f4909dc130d12" +}, { + "version" : "4.6", + "buildTime" : "20180228133636+0000", + "commitId" : "8fa6ce7945b640e6168488e4417f9bb96e4ab46c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.6-bin.zip.sha256", + "checksum" : "98bd5fd2b30e070517e03c51cbb32beee3e2ee1a84003a5a5d748996d4b1b915", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.6-wrapper.jar.sha256", + "wrapperChecksum" : "381dff8aa434499aa93bc25572b049c8c586a67faff2c02f375e4f23e17e49de" +}, { + "version" : "4.6-rc-2", + "buildTime" : "20180221152842+0000", + "commitId" : "819e0059da49f469d3e9b2896dc4e72537c4847d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.6-rc-2-bin.zip.sha256", + "checksum" : "1fab26f72fe5f52d5773c2cf4bb0beaec60c3b24a5f49d09be5a092a7473bfdd" +}, { + "version" : "4.6-rc-1", + "buildTime" : "20180216162807+0000", + "commitId" : "ac6267d33138856ffe71b159ab8154e71e9da72e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.6-rc-1-bin.zip.sha256", + "checksum" : "f8ed6380bcc3db71ec524950aa53d78ccf1da0ead900f379f3573dfef60500c9" +}, { + "version" : "4.5.1", + "buildTime" : "20180205132249+0000", + "commitId" : "37007e1c012001ff09973e0bd095139239ecd3b3", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.5.1-bin.zip.sha256", + "checksum" : "3e2ea0d8b96605b7c528768f646e0975bd9822f06df1f04a64fd279b1a17805e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.5.1-wrapper.jar.sha256", + "wrapperChecksum" : "3bf04e39ed259ff0a1217a875199a11775855d2a29207b98318ac79178249de8" +}, { + "version" : "4.5", + "buildTime" : "20180124170452+0000", + "commitId" : "77d0ec90636f43669dc794ca17ef80dd65457bec", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.5-bin.zip.sha256", + "checksum" : "03f2a43a314ff0fb843a85ef68078e06d181c4549c1e5fb983f289382b59b5e3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.5-wrapper.jar.sha256", + "wrapperChecksum" : "d283a04caee0e97b666b09e50ab394834934bee6225c2e9cd4650afdeac43828" +}, { + "version" : "4.5-rc-2", + "buildTime" : "20180122164244+0000", + "commitId" : "459d2caa04b8145b40955950cc6037034e9123b1", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.5-rc-2-bin.zip.sha256", + "checksum" : "401363cc3491055b878bba54463129c3bb0d3a76e337e5e515729a6b6fa4c883" +}, { + "version" : "4.5-rc-1", + "buildTime" : "20180109010527+0000", + "commitId" : "d6e5a1c698884ebc887980c82687c8c11ad22dd6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.5-rc-1-bin.zip.sha256", + "checksum" : "fa8f4721d121d4501871877a9c1bf83db5ba44bb0ee9dd177a4981c11cede7c6" +}, { + "version" : "4.4.1", + "buildTime" : "20171220154523+0000", + "commitId" : "10ed9dc355dc39f6307cc98fbd8cea314bdd381c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4.1-bin.zip.sha256", + "checksum" : "e7cf7d1853dfc30c1c44f571d3919eeeedef002823b66b6a988d27e919686389", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.4.1-wrapper.jar.sha256", + "wrapperChecksum" : "4e318d74d06aa7b998091345c397a3c7c4b291b59da31e6f9c772a596711acac" +}, { + "version" : "4.4", + "buildTime" : "20171206090506+0000", + "commitId" : "cf7821a6f79f8e2a598df21780e3ff7ce8db2b82", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-bin.zip.sha256", + "checksum" : "fa4873ae2c7f5e8c02ec6948ba95848cedced6134772a0169718eadcb39e0a2f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-wrapper.jar.sha256", + "wrapperChecksum" : "88b5b31f390a268ab3773df580d83fd1e388f49c2b685f78a16600577bd72fe2" +}, { + "version" : "4.4-rc-6", + "buildTime" : "20171204084815+0000", + "commitId" : "cf7821a6f79f8e2a598df21780e3ff7ce8db2b82", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-6-bin.zip.sha256", + "checksum" : "03480998491b94e27d95759c47ae6ac864e95b9db17cd5890cd37ff536431ba0" +}, { + "version" : "4.4-rc-5", + "buildTime" : "20171130152137+0000", + "commitId" : "f4daa310b16df9abd4f37257bae8312b5293a251", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-5-bin.zip.sha256", + "checksum" : "b52d338f285dd255f3abf6bcd3e0b447cc62aa8dc83a281c42f8ec1aec10dc89" +}, { + "version" : "4.4-rc-4", + "buildTime" : "20171128123659+0000", + "commitId" : "f0a427cdb7eaa95779c9ccd8147878970fbb53fb", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-4-bin.zip.sha256", + "checksum" : "d663fe846e3b2e30285b578e0bb957c4fb846cca0f36694ab32d5e5ba25c0cb8" +}, { + "version" : "4.4-rc-3", + "buildTime" : "20171127103138+0000", + "commitId" : "340c20678da78bf34d58da72071f95bb60d037e9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-3-bin.zip.sha256", + "checksum" : "607b5c3493dc78da8d6207edd43c680132d9b69912fa1de2fa42f24be72f30cd" +}, { + "version" : "4.4-rc-2", + "buildTime" : "20171124103528+0000", + "commitId" : "9ab11aefa43e1dedcf68294d5f15f246bbe54221", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-2-bin.zip.sha256", + "checksum" : "939ec641887df3677dd3ac1ed8cb6e2815dae0f33bf36db8ae64a71b4c32f8f8" +}, { + "version" : "4.4-rc-1", + "buildTime" : "20171121153105+0000", + "commitId" : "089fe1e08ca477b49583c6e00abf87f4675d0c06", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.4-rc-1-bin.zip.sha256", + "checksum" : "3b5d35b41d5807f6471e90ca81e4d1bf4fee48deca7d99e0e579f758b4db85b1" +}, { + "version" : "4.3.1", + "buildTime" : "20171108085945+0000", + "commitId" : "e4f4804807ef7c2829da51877861ff06e07e006d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3.1-bin.zip.sha256", + "checksum" : "15ebe098ce0392a2d06d252bff24143cc88c4e963346582c8d88814758d93ac7", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.3.1-wrapper.jar.sha256", + "wrapperChecksum" : "383f4efa709b52632a520708e8a07353961970941ab3867ab8ac182132ce1c54" +}, { + "version" : "4.3", + "buildTime" : "20171030154329+0000", + "commitId" : "c684c202534c4138b51033b52d871939b8d38d72", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-bin.zip.sha256", + "checksum" : "8dcbf44eef92575b475dcb1ce12b5f19d38dc79e84c662670248dc8b8247654c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-wrapper.jar.sha256", + "wrapperChecksum" : "ba496e7e0e03ffa432eaf715bb1466fac2ffc8491a71e7164a5438c48c79d8ea" +}, { + "version" : "4.3-rc-4", + "buildTime" : "20171026162557+0000", + "commitId" : "89af43c4d0506f69980f00dde78c97b2f81437f8", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-4-bin.zip.sha256", + "checksum" : "6615d3d67b7c5ca936ef34464495aeac4145dcac8df65d8fdd20f4085e48fbb6" +}, { + "version" : "4.3-rc-3", + "buildTime" : "20171024142633+0000", + "commitId" : "d13daf4f8c434761df3ba0af55fb1f3d9513624e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-3-bin.zip.sha256", + "checksum" : "3e3fad053c2d14a7dbe49006d09034576517e1f6c3f15aca7e5c594f17d08bbf" +}, { + "version" : "4.3-rc-2", + "buildTime" : "20171018073714+0000", + "commitId" : "3fb6d5b8810eeecce1472faa7f12ef1318870d9d", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-2-bin.zip.sha256", + "checksum" : "e55a78b879a5d45840003a1692fb00d1d1780298bcd696c7f10370e7922aad4f" +}, { + "version" : "4.3-rc-1", + "buildTime" : "20171012161416+0000", + "commitId" : "168523089264845c0560d994b565cae4c6e93563", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.3-rc-1-bin.zip.sha256", + "checksum" : "a2fbaa80f98b59509e3f956c9fdaeb593516ea633ce80946735fa7cb7dd74a55" +}, { + "version" : "4.2.1", + "buildTime" : "20171002153621+0000", + "commitId" : "a88ebd6be7840c2e59ae4782eb0f27fbe3405ddf", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.2.1-bin.zip.sha256", + "checksum" : "b551cc04f2ca51c78dd14edb060621f0e5439bdfafa6fd167032a09ac708fbc0", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "c536e519e65a4bb787e071c6a90d23bd219c9d409a2123db649d3684acbf3ae7" +}, { + "version" : "4.2", + "buildTime" : "20170920144823+0000", + "commitId" : "5ba503cc17748671c83ce35d7da1cffd6e24dfbd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.2-bin.zip.sha256", + "checksum" : "515dd63d32e55a9c05667809c5e40a947529de3054444ad274b3b75af5582eae", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.2-wrapper.jar.sha256", + "wrapperChecksum" : "53aa048fef3c06a8442c6a44df5edd2e8c791ee883e42ded6189c7eed112095d" +}, { + "version" : "4.2-rc-2", + "buildTime" : "20170913122310+0000", + "commitId" : "e7e9502dbcf5bf8e4ca5e0c1763a301372a9ed83", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.2-rc-2-bin.zip.sha256", + "checksum" : "e32c378a7e49e074647c59a2a618444db3871ca05e4a151122041252f2dcb640" +}, { + "version" : "4.2-rc-1", + "buildTime" : "20170907081216+0000", + "commitId" : "73d54c86eca92ae26decccd774f49bfc2b01c314", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.2-rc-1-bin.zip.sha256", + "checksum" : "7d4f7ebe327047eeb713566de36b466f6f966aa9fd471d0b5a89f76337845829" +}, { + "version" : "4.1", + "buildTime" : "20170807143848+0000", + "commitId" : "941559e020f6c357ebb08d5c67acdb858a3defc2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.1-bin.zip.sha256", + "checksum" : "d55dfa9cfb5a3da86a1c9e75bb0b9507f9a8c8c100793ccec7beb6e259f9ed43", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-4.1-wrapper.jar.sha256", + "wrapperChecksum" : "f4d953f31fbf6c38a8c330d19171c8ba6e0d1ff59d4d5c5c2d3ed821c9f3d5a3" +}, { + "version" : "4.1-rc-2", + "buildTime" : "20170731142943+0000", + "commitId" : "941559e020f6c357ebb08d5c67acdb858a3defc2", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.1-rc-2-bin.zip.sha256", + "checksum" : "c3ea162956646b681bdbf697e45737e654492c825b7958892b44be286a4594f6" +}, { + "version" : "4.0.2", + "buildTime" : "20170726161918+0000", + "commitId" : "108c593aa7b43852f39045337ee84ee1d87c87fd", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0.2-bin.zip.sha256", + "checksum" : "79ac421342bd11f6a4f404e0988baa9c1f5fabf07e3c6fa65b0c15c1c31dda22" +}, { + "version" : "4.1-rc-1", + "buildTime" : "20170720152215+0000", + "commitId" : "e9f4cb3be4b0a16506ad54b44485b7f0862f6c59", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.1-rc-1-bin.zip.sha256", + "checksum" : "782866e286d0e7d527f8110337e96e7e0c886376d9a35966d74ca4024ede7153" +}, { + "version" : "4.0.1", + "buildTime" : "20170707140241+0000", + "commitId" : "38e5dc0f772daecca1d2681885d3d85414eb6826", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0.1-bin.zip.sha256", + "checksum" : "d717e46200d1359893f891dab047fdab98784143ac76861b53c50dbd03b44fd4" +}, { + "version" : "4.1-milestone-1", + "buildTime" : "20170620170546+0000", + "commitId" : "3ad5af92d482b963cc6b00fb5ad53b608606d657", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "4.1", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.1-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.1-milestone-1-bin.zip.sha256", + "checksum" : "48edfe37620b5de2fd6e111a65b384dd730c377890c26a01a1ae55ae0220abc1" +}, { + "version" : "3.5.1", + "buildTime" : "20170616143627+0000", + "commitId" : "d4c3bb4eac74bd0a3c70a0d213709e484193e251", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.5.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.5.1-bin.zip.sha256", + "checksum" : "8dce35f52d4c7b4a4946df73aa2830e76ba7148850753d8b5e94c5dc325ceef8" +}, { + "version" : "4.0", + "buildTime" : "20170614151108+0000", + "commitId" : "316546a5fcb4e2dfe1d6aa0b73a4e09e8cecb5a5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-bin.zip.sha256", + "checksum" : "56bd2dde29ba2a93903c557da1745cafd72cdd8b6b0b83c05a40ed7896b79dfe" +}, { + "version" : "4.0-rc-3", + "buildTime" : "20170612150444+0000", + "commitId" : "78d6b5b1e5cdb3dd4e233eb2f2e2bf721c53ca9e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-3-bin.zip.sha256", + "checksum" : "f4229169204b98dbc2718386b36538cef2fd28e249aec4e9472eb820a93beeb3" +}, { + "version" : "4.0-rc-2", + "buildTime" : "20170607152236+0000", + "commitId" : "14679a04a9d5afc2038c57d9392a36d8cc2e850e", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-2-bin.zip.sha256", + "checksum" : "35f92e48b73f6f6d99997094c7dec298fbdae5a3ee989805c6cde0abe24ff6fe" +}, { + "version" : "4.0-rc-1", + "buildTime" : "20170530162617+0000", + "commitId" : "f114de85c48350feb8cf352522f1af471e832a33", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "4.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-rc-1-bin.zip.sha256", + "checksum" : "19d7be3fd349bcf0d36d5a29ded4dd704e5d391e8e8751f32d5bb199636df053" +}, { + "version" : "4.0-milestone-2", + "buildTime" : "20170517143324+0000", + "commitId" : "0e3bb607e7ee1e805455d1c6388f6830f0ab9eca", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-milestone-2-bin.zip.sha256", + "checksum" : "87120d217d97704702136984e51002d979fec49ee2ff1524ed2769d98dd1f5c5" +}, { + "version" : "4.0-milestone-1", + "buildTime" : "20170428074509+0000", + "commitId" : "c1e7532b308e20803a3678ad5b4cfbe682dbb9fc", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "4.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-4.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-4.0-milestone-1-bin.zip.sha256", + "checksum" : "56a98b39db79d947e9227646b3fc72376bdc3daf09ce4d885973510057841738" +}, { + "version" : "3.5", + "buildTime" : "20170410133725+0000", + "commitId" : "b762622a185d59ce0cfc9cbc6ab5dd22469e18a6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.5-bin.zip.sha256", + "checksum" : "0b7450798c190ff76b9f9a3d02e18b33d94553f708ebc08ebe09bdf99111d110" +}, { + "version" : "3.5-rc-3", + "buildTime" : "20170404150413+0000", + "commitId" : "b762622a185d59ce0cfc9cbc6ab5dd22469e18a6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-3-bin.zip.sha256", + "checksum" : "add85f5073e3051a23008e9d371fc49bbb6719b503c9f43a7cf6ab61337e05b9" +}, { + "version" : "3.5-rc-2", + "buildTime" : "20170327155623+0000", + "commitId" : "ec63970cd167993d3dcda346a1d00a286a23b1c9", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip.sha256", + "checksum" : "c23b319547d87b35f16d0bd251a13495b8bc26fe8c7356f678bae766bb5f6ab7" +}, { + "version" : "3.5-rc-1", + "buildTime" : "20170322151607+0000", + "commitId" : "f1f4d6c61299f1225fb2190ca02802c8b821c5ec", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.5-rc-1-bin.zip.sha256", + "checksum" : "ed17b488dcc8969503aabcce7e1462fb99518e2dddd6e766b0a18e99685dad50" +}, { + "version" : "3.4.1", + "buildTime" : "20170303194541+0000", + "commitId" : "9eb76efdd3d034dc506c719dac2955efb5ff9a93", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.4.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.4.1-bin.zip.sha256", + "checksum" : "db1db193d479cc1202be843f17e4526660cfb0b21b57d62f3a87f88c878af9b2" +}, { + "version" : "3.4", + "buildTime" : "20170220144926+0000", + "commitId" : "73f32d68824582945f5ac1810600e8d87794c3d4", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.4-bin.zip.sha256", + "checksum" : "72d0cd4dcdd5e3be165eb7cd7bbd25cf8968baf400323d9ab1bba622c3f72205" +}, { + "version" : "3.4-rc-3", + "buildTime" : "20170213145527+0000", + "commitId" : "6e76fb227406ebcf649a0cb109747149677c4f27", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-3-bin.zip.sha256", + "checksum" : "c492b318d35f65ba1038289067e04d08598d85e0ea98b33430effd0f1b04bc7c" +}, { + "version" : "3.4-rc-2", + "buildTime" : "20170206211728+0000", + "commitId" : "4a4d6ae7c800239c63561020194df4d7a5e82cd7", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-2-bin.zip.sha256", + "checksum" : "a6184aab4ea11f0264711e989dabc4486045bc6aaaa30110fff3fd4b5250061f" +}, { + "version" : "3.4-rc-1", + "buildTime" : "20170126154849+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.4-rc-1-bin.zip.sha256", + "checksum" : "c8323ca48a9f3e7993bce566e3edc464f459a5a8bfcedbd6ce7e8b7d0c45fd46" +}, { + "version" : "3.3", + "buildTime" : "20170103153104+0000", + "commitId" : "075893a3d0798c0c1f322899b41ceca82e4e134b", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.3-bin.zip.sha256", + "checksum" : "c58650c278d8cf0696cab65108ae3c8d95eea9c1938e0eb8b997095d5ca9a292" +}, { + "version" : "3.3-rc-1", + "buildTime" : "20161219160724+0000", + "commitId" : "f381de5273f05b2e7075bbd79f33e8be31247bfc", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.3-rc-1-bin.zip.sha256", + "checksum" : "845135efa934b533f732cb96a34538f10c5703cea39474c148e18ac69d4db49f" +}, { + "version" : "3.2.1", + "buildTime" : "20161122151954+0000", + "commitId" : "83b485b914fd4f335ad0e66af9d14aad458d2cc5", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.2.1-bin.zip.sha256", + "checksum" : "9843a3654d3e57dce54db06d05f18b664b95c22bf90c6becccb61fc63ce60689", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-3.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "020ef0245a07b33ca48b12f59415e7e5083cf701ef02690464a8cc2ab3984608" +}, { + "version" : "3.2", + "buildTime" : "20161114123259+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.2-bin.zip.sha256", + "checksum" : "5321b36837226dc0377047a328f12010f42c7bf88ee4a3b1cee0c11040082935", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-3.2-wrapper.jar.sha256", + "wrapperChecksum" : "d2d3abae74e89cc4200f48d4a08a7e5960363c33ee62272ef5ffbb39f4c7f83e" +}, { + "version" : "3.2-rc-3", + "buildTime" : "20161107153233+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-3-bin.zip.sha256", + "checksum" : "dadae3f8ff6c60637ac749693a074ae181315a7f43c5a6d313565371cbcab740" +}, { + "version" : "3.2-rc-2", + "buildTime" : "20161031160822+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-2-bin.zip.sha256", + "checksum" : "45e362f471b9d112f820f3d4cf3901f9c5b112efb309324963078f53a80cff40" +}, { + "version" : "3.2-rc-1", + "buildTime" : "20161024194725+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.2-rc-1-bin.zip.sha256", + "checksum" : "fe2ec80d91ab79ddb72e30d25e3646091fafa7cac379c1f46b743bcad52ed4f3" +}, { + "version" : "3.1", + "buildTime" : "20160919105353+0000", + "commitId" : "13f38ba699afd86d7cdc4ed8fd7dd3960c0b1f97", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.1-bin.zip.sha256", + "checksum" : "c7de3442432253525902f7e8d7eac8b5fd6ce1623f96d76916af6d0e383010fc", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-3.1-wrapper.jar.sha256", + "wrapperChecksum" : "0f49043be582d7a39b671f924c66bd9337b92fa88ff5951225acc60560053067" +}, { + "version" : "3.1-rc-1", + "buildTime" : "20160912123424+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.1-rc-1-bin.zip.sha256", + "checksum" : "9d67811a2dee405c920e6bced2d687142e2378eb33c3af960bdbaef39bcb2485" +}, { + "version" : "3.0", + "buildTime" : "20160815131501+0000", + "commitId" : "ad76ba00f59ecb287bd3c037bd25fc3df13ca558", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-bin.zip.sha256", + "checksum" : "39c906941a474444afbddc38144ed44166825acb0a57b0551dddb04bbf157f80", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-wrapper.jar.sha256", + "wrapperChecksum" : "42d7a2f636983aa09d21dfeb6e90d21d7a8dad905351390643ce60cc82c8f8a9" +}, { + "version" : "3.0-rc-2", + "buildTime" : "20160808133410+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-rc-2-bin.zip.sha256", + "checksum" : "874e3bb85a85961f17cb371cd1291f9c0015bcc1e4bcc9df6e2c29b249438a73" +}, { + "version" : "3.0-rc-1", + "buildTime" : "20160802061335+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "3.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-rc-1-bin.zip.sha256", + "checksum" : "98c51e7f465b61d17578098f9ca8dc081014a551fb4dff66cce52fbfcb23376f" +}, { + "version" : "2.14.1", + "buildTime" : "20160718063837+0000", + "commitId" : "d9e2113d9fb05a5caabba61798bdb8dfdca83719", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-bin.zip.sha256", + "checksum" : "cfc61eda71f2d12a572822644ce13d2919407595c2aec3e3566d2aab6f97ef39", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-wrapper.jar.sha256", + "wrapperChecksum" : "8e47da0b2656354d059609cae9d44b196d4f9b14512e688ffee4e0eb7e723ae9" +}, { + "version" : "2.14.1-rc-2", + "buildTime" : "20160711105827+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-rc-2-bin.zip.sha256", + "checksum" : "784175796a5dc8180c90f3e26249e858f62d400aaec3db81153992f9d749cda6" +}, { + "version" : "2.14.1-rc-1", + "buildTime" : "20160629051303+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14.1-rc-1-bin.zip.sha256", + "checksum" : "c20590dc930c78d21218d10ddf2c637ca0a2ee895e9c3476b670789aa60b5da4" +}, { + "version" : "3.0-milestone-2", + "buildTime" : "20160621224128+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "3.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-milestone-2-bin.zip.sha256", + "checksum" : "5c3e8e9a38c92ae85e05df609c6c1b6f51e5a08b39a26d61bd8a7044268135e7" +}, { + "version" : "2.14", + "buildTime" : "20160614071637+0000", + "commitId" : "cba5fea19f1e0c6a00cc904828a6ec4e11739abc", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-bin.zip.sha256", + "checksum" : "993b4f33b652c689e9721917d8e021cab6bbd3eae81b39ab2fd46fdb19a928d5", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-wrapper.jar.sha256", + "wrapperChecksum" : "b5ca811c057b3eb4164c78f4155d667c6092ff98ba91a4c90d29e127426f37a7" +}, { + "version" : "2.14-rc-6", + "buildTime" : "20160610181946+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-6-bin.zip.sha256", + "checksum" : "f26dd25087dc68bd41ab5fdf31532eaf682c3b2693052d4cf9f7c727e2a47231" +}, { + "version" : "3.0-milestone-1", + "buildTime" : "20160608013148+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "3.0", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-3.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-3.0-milestone-1-bin.zip.sha256", + "checksum" : "dfff9f7fbf75d025cf5b4f7b61ea9e24757f74a6934ba7249658e7e2de9d329f" +}, { + "version" : "2.14-rc-5", + "buildTime" : "20160607135817+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-5-bin.zip.sha256", + "checksum" : "dcdd1021345cfabd2c06a345700afe537bde5478cfb8c3ac59d6348eeb0647e9" +}, { + "version" : "2.14-rc-4", + "buildTime" : "20160603110827+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-4-bin.zip.sha256", + "checksum" : "471424c0998851572c8b0ac71807215fc717de1373dcb5b64889e0aa5d147d26" +}, { + "version" : "2.14-rc-3", + "buildTime" : "20160530112111+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-3-bin.zip.sha256", + "checksum" : "6b076728aef0adb281bcc31db7b4f83a061caaa36d61f00dc43e606a3de85578" +}, { + "version" : "2.14-rc-2", + "buildTime" : "20160527175624+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-2-bin.zip.sha256", + "checksum" : "eb824186223dec65fc3f9bb2755934b38c1cd005a0e6f0d80e282a2390214735" +}, { + "version" : "2.14-rc-1", + "buildTime" : "20160518093824+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.14", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.14-rc-1-bin.zip.sha256", + "checksum" : "2ff85eb9eda60f13bc8debec69849e1e2a72b7bfc5af5d26e61a293b5dcbceba" +}, { + "version" : "2.13", + "buildTime" : "20160425041010+0000", + "commitId" : "3b427b1481e46232107303c90be7b05079b05b1c", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.13-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.13-bin.zip.sha256", + "checksum" : "0f665ec6a5a67865faf7ba0d825afb19c26705ea0597cec80dd191b0f2cbb664", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.13-wrapper.jar.sha256", + "wrapperChecksum" : "0c3c576e28b44eddcab6b8b4854f484363dfbcd047657d41654e839835da2c53" +}, { + "version" : "2.13-rc-2", + "buildTime" : "20160419145240+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.13", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.13-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.13-rc-2-bin.zip.sha256", + "checksum" : "949853dedce0b63898a1aedde39a8e8610ca061a59268a9bc70b496f91e60854" +}, { + "version" : "2.13-rc-1", + "buildTime" : "20160406110211+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.13", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.13-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.13-rc-1-bin.zip.sha256", + "checksum" : "75941a17c6db68b83e82ca27b8d813cc8855b3b8999228b82b1704798f2d4b6b" +}, { + "version" : "2.12", + "buildTime" : "20160314083203+0000", + "commitId" : "b29fbb64ad6b068cb3f05f7e40dc670472129bc0", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.12-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.12-bin.zip.sha256", + "checksum" : "e77064981906cd0476ff1e0de3e6fef747bd18e140960f1915cca8ff6c33ab5c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.12-wrapper.jar.sha256", + "wrapperChecksum" : "4894520b03c007bf38e983bf933320c483a9790010d997029fa8985dc6128559" +}, { + "version" : "2.12-rc-1", + "buildTime" : "20160307174118+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.12", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.12-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.12-rc-1-bin.zip.sha256", + "checksum" : "bb4ccf5399301d34a1d2a6ae6ae6dfb4bd4db61b499581a1bcbd976008a278a2" +}, { + "version" : "2.11", + "buildTime" : "20160208075916+0000", + "commitId" : "584db1c7c90bdd1de1d1c4c51271c665bfcba978", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.11-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.11-bin.zip.sha256", + "checksum" : "8d7437082356c9fd6309a4479c8db307673965546daea445c6c72759cd6b1ed6", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.11-wrapper.jar.sha256", + "wrapperChecksum" : "0bc7b16a0a3fa52af674de44d1fea48abc4dee3431f3d4829cd9ea329836e596" +}, { + "version" : "2.11-rc-3", + "buildTime" : "20160203190922+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-3-bin.zip.sha256", + "checksum" : "6191865dbe7e2e45125285d6c4316f99d02280dadf8375c290c16e9c995c3188" +}, { + "version" : "2.11-rc-2", + "buildTime" : "20160202162431+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-2-bin.zip.sha256", + "checksum" : "7112bffc0e69c35040d4f0a6e71c3593a629831d698e8e85797b2afafae18cee" +}, { + "version" : "2.11-rc-1", + "buildTime" : "20160120084154+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.11-rc-1-bin.zip.sha256", + "checksum" : "4af1d57f3f3679452944cfeac172949991ff76257b6e511287b2c02951dd7706" +}, { + "version" : "2.10", + "buildTime" : "20151221211504+0000", + "commitId" : "276bdcded730f53aa8c11b479986aafa58e124a6", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.10-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.10-bin.zip.sha256", + "checksum" : "66406247f745fc6f05ab382d3f8d3e120c339f34ef54b86f6dc5f6efc18fbb13", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.10-wrapper.jar.sha256", + "wrapperChecksum" : "16caeaf66d57a0d1d2087fef6a97efa62de8da69afa5b908f40db35afc4342da" +}, { + "version" : "2.10-rc-2", + "buildTime" : "20151218164026+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.10-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.10-rc-2-bin.zip.sha256", + "checksum" : "49650433ba07e8d1cdcb4a3ee6568fbf82ccff61b8cb76e2e7040a2c2f1a9c7b" +}, { + "version" : "2.10-rc-1", + "buildTime" : "20151208224811+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.10-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.10-rc-1-bin.zip.sha256", + "checksum" : "f1799317f138612764db1c9cf53638e100038b3494803bf38966d55e8db58cef" +}, { + "version" : "2.9", + "buildTime" : "20151117070217+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.9-bin.zip.sha256", + "checksum" : "c9159ec4362284c0a38d73237e224deae6139cbde0db4f0f44e1c7691dd3de2f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.9-wrapper.jar.sha256", + "wrapperChecksum" : "b92386e36a96da6be89e91f71087d1394a26c0450231ba0b22e28ee1ee8fa14b" +}, { + "version" : "2.9-rc-1", + "buildTime" : "20151103230218+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.9-rc-1-bin.zip.sha256", + "checksum" : "d278e80f9f0590f6eaa9c8b9c69c4d66a281c5a9bbb736524fdba5f74c4875a1" +}, { + "version" : "2.8", + "buildTime" : "20151020034636+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.8-bin.zip.sha256", + "checksum" : "a88db9c2f104defdaa8011c58cf6cda6c114298ae3695ecfb8beb30da3a903cb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.8-wrapper.jar.sha256", + "wrapperChecksum" : "198159fcd7d29533c0d37423d66c44729982d5280c9e2c7c5f4b7bc6a9317f6b" +}, { + "version" : "2.8-rc-2", + "buildTime" : "20151015124208+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.8-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.8-rc-2-bin.zip.sha256", + "checksum" : "659d6057613a33211f91ce17ce99cfeea03171db771ce842a1968ec49be17696" +}, { + "version" : "2.8-rc-1", + "buildTime" : "20150930171102+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.8-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.8-rc-1-bin.zip.sha256", + "checksum" : "048735c9f06c654b925156e8ca6237c623982ac3b425c3925b8097c13c77dfab" +}, { + "version" : "2.7", + "buildTime" : "20150914072616+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.7-bin.zip.sha256", + "checksum" : "cde43b90945b5304c43ee36e58aab4cc6fb3a3d5f9bd9449bb1709a68371cb06", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.7-wrapper.jar.sha256", + "wrapperChecksum" : "d7e1975ccf2dc079d4f0b1010febdad466506d1565c5aa8017c88ebc5e471604" +}, { + "version" : "2.7-rc-2", + "buildTime" : "20150905140615+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.7-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.7-rc-2-bin.zip.sha256", + "checksum" : "0854997cd8d77f79a58f3ebc2f19b0b9891d30579fe5392cb5a746b563c20e78" +}, { + "version" : "2.7-rc-1", + "buildTime" : "20150828140928+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.7-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.7-rc-1-bin.zip.sha256", + "checksum" : "5f54dea2792e1c94bbf2f76189a6b8404cc2492a56ffc06bda6dd4aef2d9d8f8" +}, { + "version" : "2.6", + "buildTime" : "20150810131506+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.6-bin.zip.sha256", + "checksum" : "18a98c560af231dfa0d3f8e0802c20103ae986f12428bb0a6f5396e8f14e9c83", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.6-wrapper.jar.sha256", + "wrapperChecksum" : "695089a2b306f55f0bd63140fbcc5ead8c383819018188ce484cd5a055bec6e4" +}, { + "version" : "2.6-rc-2", + "buildTime" : "20150804104806+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.6-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.6-rc-2-bin.zip.sha256", + "checksum" : "82ad20a1c6408176b50e9c07dae8a1c1e2bf9cdd02d11ace8ee92539672f45db" +}, { + "version" : "2.6-rc-1", + "buildTime" : "20150728171643+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.6-rc-1-bin.zip.sha256", + "checksum" : "5d98379be8076d4c11eaaae8a17c6cbe58cc3d6d76ddd08d962ce8e76c2df67e" +}, { + "version" : "2.5", + "buildTime" : "20150708073837+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.5-bin.zip.sha256", + "checksum" : "3f953e0cb14bb3f9ebbe11946e84071547bf5dfd575d90cfe9cc4e788da38555", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.5-wrapper.jar.sha256", + "wrapperChecksum" : "718d7b25ea60b357fc4cb2212ce10b3f03dfd0e6fe5f23f565b15553ec46bb7e" +}, { + "version" : "2.5-rc-2", + "buildTime" : "20150702151559+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.5-rc-2-bin.zip.sha256", + "checksum" : "8807bc2e29e215e5d4760fb665815477ff7a07b7fddf3d56c38260e5981a2825" +}, { + "version" : "2.5-rc-1", + "buildTime" : "20150623180452+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.5-rc-1-bin.zip.sha256", + "checksum" : "1c712b049108d6d0630c78ada345d9adbfc3e1ca4fc6a2bcaecca25634292809" +}, { + "version" : "2.4", + "buildTime" : "20150505080924+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.4-bin.zip.sha256", + "checksum" : "c4eaecc621a81f567ded1aede4a5ddb281cc02a03a6a87c4f5502add8fc2f16f", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.4-wrapper.jar.sha256", + "wrapperChecksum" : "98420079ffe3e24b1013180d9b9bc2e2ee6a9d867ee232004b75a961d9c18e27" +}, { + "version" : "2.4-rc-2", + "buildTime" : "20150429045020+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.4-rc-2-bin.zip.sha256", + "checksum" : "f36496775b847302e7081b6b051086296882804c7365c0b5e073227ff1cec8ac" +}, { + "version" : "2.4-rc-1", + "buildTime" : "20150423031523+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.4-rc-1-bin.zip.sha256", + "checksum" : "ebd8ebc8954b0fb30696d561e41d6a717db42af6c0640099af8e688b10813135" +}, { + "version" : "2.3", + "buildTime" : "20150216050933+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-bin.zip.sha256", + "checksum" : "010dd9f31849abc3d5644e282943b1c1c355f8e2635c5789833979ce590a3774", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-wrapper.jar.sha256", + "wrapperChecksum" : "b18a1114ebe81fb7502d40ad9a4f86cef82fff244a865ad45533b4d5e7ff0cc8" +}, { + "version" : "2.3-rc-4", + "buildTime" : "20150211163922+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-4-bin.zip.sha256", + "checksum" : "34878db3896bd7d2b29406cbdb0c156078ab62f3361f9b4df689cfb3814d8906" +}, { + "version" : "2.3-rc-3", + "buildTime" : "20150208183759+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-3-bin.zip.sha256", + "checksum" : "6ce92b77f67f3bf0f543cfb723796919964b969b454e630c8e8b882692e90f23" +}, { + "version" : "2.3-rc-2", + "buildTime" : "20150204105413+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-2-bin.zip.sha256", + "checksum" : "1a90fbaf3db2b0b86d7b8b7df5c1760732ef94f984ffd71264b1821076e268c9" +}, { + "version" : "2.3-rc-1", + "buildTime" : "20150127140232+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.3-rc-1-bin.zip.sha256", + "checksum" : "a78846df63ed81971eed2db62efca4ce0d595a53a9f1d08e341c7ddae0c04b56" +}, { + "version" : "2.2.1", + "buildTime" : "20141124094535+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.2.1-bin.zip.sha256", + "checksum" : "420aa50738299327b611c10b8304b749e8d3a579407ee9e755b15921d95ff418", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.2.1-wrapper.jar.sha256", + "wrapperChecksum" : "5f73d431fd1c5dcc2cf11555b8e486c43249c1099f678ccc6088b05be600a2e1" +}, { + "version" : "2.2.1-rc-1", + "buildTime" : "20141117101237+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.2.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.2.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.2.1-rc-1-bin.zip.sha256", + "checksum" : "65b87391af213341f19bf74b15f2a506a0136bb8aa466d87ac2b7d55d3ddab71" +}, { + "version" : "2.2", + "buildTime" : "20141110133144+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.2-bin.zip.sha256", + "checksum" : "91e5655fe11ef414449f218c4fa2985b3a49b7903c57556da109c84fa26e1dfb", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.2-wrapper.jar.sha256", + "wrapperChecksum" : "fa9b4294d47cf8db7039cb9b2435de3dd1accb0d3d67926705775a0579dfa397" +}, { + "version" : "2.2-rc-2", + "buildTime" : "20141103170404+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.2-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.2-rc-2-bin.zip.sha256", + "checksum" : "d430d7e508116a7c917c394879f85c29e1bdce921f15d12ddf8473fab0564d92" +}, { + "version" : "2.2-rc-1", + "buildTime" : "20141020163257+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.2-rc-1-bin.zip.sha256", + "checksum" : "58292023856cc9ceb7e6376efece7ee3c2f95b7d1986d7f5f95b7db5f2120767" +}, { + "version" : "2.1", + "buildTime" : "20140908104039+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-bin.zip.sha256", + "checksum" : "3eee4f9ea2ab0221b89f8e4747a96d4554d00ae46d8d633f11cfda60988bf878", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-wrapper.jar.sha256", + "wrapperChecksum" : "5e27c39c2336c25748f279d8b105162d14b1a39eb7839d0b658432282d0ce79f" +}, { + "version" : "2.1-rc-4", + "buildTime" : "20140903092053+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-4-bin.zip.sha256", + "checksum" : "9894bc2d60e5a1cca564313f6e68af0b57cd21b95cd68e96c8bd215cab8a944d" +}, { + "version" : "2.1-rc-3", + "buildTime" : "20140901103549+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-3-bin.zip.sha256", + "checksum" : "8a6839bac38e97d2b55fa1ad1e10c0223f3a1e39f4ced9178a8f666fd017829f" +}, { + "version" : "2.1-rc-2", + "buildTime" : "20140828023229+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-2-bin.zip.sha256", + "checksum" : "6a641aa38dd88b572d1baa35ac14858b4d7f0f4b83f1677fbb3adfe4391950ce" +}, { + "version" : "2.1-rc-1", + "buildTime" : "20140819194409+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.1-rc-1-bin.zip.sha256", + "checksum" : "eeff5d7f43b4ef1c1c29916b29c50c4d8cda32b65ab4e322a6d50f04b7601ae1" +}, { + "version" : "2.0", + "buildTime" : "20140701074534+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.0-bin.zip.sha256", + "checksum" : "a1eb880c8755333c4d33c4351b269bebe517002532d3142c0b6164c9e8c081c3", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-2.0-wrapper.jar.sha256", + "wrapperChecksum" : "80a33ca14e3bca3116bc8749550397f739f126190c82bb6399fdc8d10f49661f" +}, { + "version" : "2.0-rc-2", + "buildTime" : "20140623080236+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.0-rc-2-bin.zip.sha256", + "checksum" : "932fbabf535876be50ff1d07a4d7fab7f7ae03275a2c5615da2e8add5d4b7c76" +}, { + "version" : "2.0-rc-1", + "buildTime" : "20140606070241+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "2.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-2.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-2.0-rc-1-bin.zip.sha256", + "checksum" : "8240c8f325d0233146e1e21dab31de6b7e653870111187d911eaf4704798faec" +}, { + "version" : "1.12", + "buildTime" : "20140429092431+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.12-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.12-bin.zip.sha256", + "checksum" : "8734b13a401f4311ee418173ed6ca8662d2b0a535be8ff2a43ecb1c13cd406ea", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.12-wrapper.jar.sha256", + "wrapperChecksum" : "dea5ceba47b58df0b7f69a65b24357527c1927ccc72b6d4ed90658d39e461b29" +}, { + "version" : "1.12-rc-2", + "buildTime" : "20140423112055+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.12", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.12-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.12-rc-2-bin.zip.sha256", + "checksum" : "886e40b53d772cacc0cf0a145d464790c125d2cf19c28d93f534eb2ae86186bc" +}, { + "version" : "1.12-rc-1", + "buildTime" : "20140416084626+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.12", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.12-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.12-rc-1-bin.zip.sha256", + "checksum" : "3f9f970be1b02e9d05c95344fb7fd468be7285d38f2e22ea194bc3e12207a250" +}, { + "version" : "1.11", + "buildTime" : "20140211113439+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.11-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.11-bin.zip.sha256", + "checksum" : "07e235df824964f0e19e73ea2327ce345c44bcd06d44a0123d29ab287fc34091", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.11-wrapper.jar.sha256", + "wrapperChecksum" : "a14b54dd3790f5ce1dc08ebbf4b5bcc05f76c4554b43accb84696c970f29aba0" +}, { + "version" : "1.11-rc-1", + "buildTime" : "20140131033453+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.11", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.11-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.11-rc-1-bin.zip.sha256", + "checksum" : "d16d91f5081ce7665dca6562fb200d181f2abbb8c8ffef4ea8b9a3bad4079db4" +}, { + "version" : "1.10", + "buildTime" : "20131217092815+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.10-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.10-bin.zip.sha256", + "checksum" : "6e6db4fc595f27ceda059d23693b6f6848583950606112b37dfd0e97a0a0a4fe", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.10-wrapper.jar.sha256", + "wrapperChecksum" : "6a6c15e222a0458aa33985b87f67954f4222410b43b1e26866197d0a77d93cbc" +}, { + "version" : "1.10-rc-2", + "buildTime" : "20131211105454+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.10-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.10-rc-2-bin.zip.sha256", + "checksum" : "6930c2882cf984a7c3ad49b31ed179224ff956d5844d228c814055ae88afa78e" +}, { + "version" : "1.10-rc-1", + "buildTime" : "20131204154128+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.10", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.10-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.10-rc-1-bin.zip.sha256", + "checksum" : "25ca44dbfe099013a0a94ebe098da8b8b279d2d38bcc3737039067f5fc88c1f1" +}, { + "version" : "1.9", + "buildTime" : "20131119082002+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-bin.zip.sha256", + "checksum" : "097ddc2bcbc9da2bb08cbf6bf8079585e35ad088bafd42e8716bc96405db98e9", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-wrapper.jar.sha256", + "wrapperChecksum" : "134337ea7c13221f9d1a1c14288a5cf8af9f6060167b903b724b115cf5a0cf73" +}, { + "version" : "1.9-rc-4", + "buildTime" : "20131118093242+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-4-bin.zip.sha256", + "checksum" : "5bdcf356f795ee85607d6aaa5534c26423ce8bf6f2bbd1eda0fb04ae8eebd943" +}, { + "version" : "1.9-rc-3", + "buildTime" : "20131107122642+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-3-bin.zip.sha256", + "checksum" : "4690509db43b713764ce869977414df97c692594c7ba2fa7e421ad5757b256a7" +}, { + "version" : "1.9-rc-2", + "buildTime" : "20131028093320+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-2-bin.zip.sha256", + "checksum" : "356844c3e7f7b16d620348f64fdbc8c39455d805855f6975c6f9cc6a357cc89c" +}, { + "version" : "1.9-rc-1", + "buildTime" : "20131023094618+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.9-rc-1-bin.zip.sha256", + "checksum" : "667fa5360aea327f57df76a471886791756a0afb2f80fceef15efba1b1588b41" +}, { + "version" : "1.8", + "buildTime" : "20130924073233+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.8-bin.zip.sha256", + "checksum" : "a342bbfa15fd18e2482287da4959588f45a41b60910970a16e6d97959aea5703", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.8-wrapper.jar.sha256", + "wrapperChecksum" : "13f23a24252ddca0a0fabef212e3c854f5895b081c09d015c91587a5df9bf9f7" +}, { + "version" : "1.8-rc-2", + "buildTime" : "20130919053314+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.8-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.8-rc-2-bin.zip.sha256", + "checksum" : "7280197f1cdfdd00616cbbbcf16b078c1ed2bc92ec00c6b3a06b790d020995f8" +}, { + "version" : "1.8-rc-1", + "buildTime" : "20130910130452+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.8", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.8-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.8-rc-1-bin.zip.sha256", + "checksum" : "b4aaf215b700adbe6d4057bfb86361d3d1f9edc45f8ddc5a985cd5c65a6df35f" +}, { + "version" : "1.7", + "buildTime" : "20130806111956+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.7-bin.zip.sha256", + "checksum" : "360c97d51621b5a1ecf66748c718594e5f790ae4fbc1499543e0c006033c9d30", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.7-wrapper.jar.sha256", + "wrapperChecksum" : "7af529cc3331d38b3d8f8344ddd9b2d3744542b55b68318abd8bb1a6f3812a1c" +}, { + "version" : "1.7-rc-2", + "buildTime" : "20130729120421+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.7-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.7-rc-2-bin.zip.sha256", + "checksum" : "db40dbf3ac4545372fdabafbb993344a2cef44ba6ff7436573bca2aba21fc611" +}, { + "version" : "1.7-rc-1", + "buildTime" : "20130702145630+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.7", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.7-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.7-rc-1-bin.zip.sha256", + "checksum" : "d69df2d9b36d0a9c72b0975e791a50f0639cdca8e48e7df7e9aaf6c9d87f176f" +}, { + "version" : "1.6", + "buildTime" : "20130507091214+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.6-bin.zip.sha256", + "checksum" : "de3e89d2113923dcc2e0def62d69be0947ceac910abd38b75ec333230183fac4", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.6-wrapper.jar.sha256", + "wrapperChecksum" : "66dbcc9f0bc33789ea0bdb4d49c8ea037047bda5647ef696c47bdca65f785159" +}, { + "version" : "1.6-rc-1", + "buildTime" : "20130430131900+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.6", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.6-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.6-rc-1-bin.zip.sha256", + "checksum" : "3fdbb2d087d4916cb4162a6b87d0a1ffb6efcd296d9761001646fa0e21881629" +}, { + "version" : "1.5", + "buildTime" : "20130327140935+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.5-bin.zip.sha256", + "checksum" : "a5511a0659caa47d9d74fd2844c9da43157d2f78e63a0223c6289d88f5aaecbe", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.5-wrapper.jar.sha256", + "wrapperChecksum" : "9598ffdf7ee26949d8b861ece267c70c802f21f7fc52596693834792d155195a" +}, { + "version" : "1.5-rc-3", + "buildTime" : "20130321155709+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-3-bin.zip.sha256", + "checksum" : "7c99d43ccaea8215df7c5e82a3c1378908197925f4f4748315c75f69ae1dffbe" +}, { + "version" : "1.5-rc-2", + "buildTime" : "20130317011516+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-2-bin.zip.sha256", + "checksum" : "f7feac0fc31d919f4f36dc0a6b3450948c40c01e785f590e3991d4178841d415" +}, { + "version" : "1.5-rc-1", + "buildTime" : "20130311120320+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.5", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.5-rc-1-bin.zip.sha256", + "checksum" : "5e25bf4ac2de6fdd7b0867b14144bd29d16b1e3390f1855b55855e0d7a2f7705" +}, { + "version" : "1.4", + "buildTime" : "20130128034246+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.4-bin.zip.sha256", + "checksum" : "cd99e85fbcd0ae8b99e81c9992a2f10cceb7b5f009c3720ef3a0078f4f92e94e", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.4-wrapper.jar.sha256", + "wrapperChecksum" : "c95985b7b5684e133c5d45044fd90faaf6c8f7cd2493d61a11c2b8c5b71ef514" +}, { + "version" : "1.4-rc-3", + "buildTime" : "20130125035146+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-3-bin.zip.sha256", + "checksum" : "0b7b24ff52d56586d6d3aae6b3ffdff5de279549e3b80105d159556057b11151" +}, { + "version" : "1.4-rc-2", + "buildTime" : "20130122030632+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-2-bin.zip.sha256", + "checksum" : "47a90853ac8eda1294e6dc3334448584a7e48ec2c8e76acadcd05bb6f411424f" +}, { + "version" : "1.4-rc-1", + "buildTime" : "20130117001257+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.4", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.4-rc-1-bin.zip.sha256", + "checksum" : "133a12cb48dc0383d83ca84e92e8c555b0fdbad8d2b8e636216f739f2ef22921" +}, { + "version" : "1.3", + "buildTime" : "20121120113738+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.3-bin.zip.sha256", + "checksum" : "ada68561efbb9f1cae0f9063974cbde15c180351a2f92bc2f1106e39ddcae5ba", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.3-wrapper.jar.sha256", + "wrapperChecksum" : "95513eccca99e1ae1aeadc4f69cabd0e7fb64821d3f26c46a489df844c8a7353" +}, { + "version" : "1.3-rc-2", + "buildTime" : "20121115155343+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.3-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.3-rc-2-bin.zip.sha256", + "checksum" : "46b3ff676c2a71f8aa4d5345e0b206920d7669f9b777d346dcebd85638afd3d0" +}, { + "version" : "1.3-rc-1", + "buildTime" : "20121112135252+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.3", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.3-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.3-rc-1-bin.zip.sha256", + "checksum" : "f699e313f4e4681c18e1f63074af1d011dd775d8de5ec39bbaab6ce3e2a1e1ee" +}, { + "version" : "1.2", + "buildTime" : "20120912104602+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.2-bin.zip.sha256", + "checksum" : "eb53da3704d24cabb7565f34a3bf16bcd863c4b0c139917606fb15d4f27c7bdf", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.2-wrapper.jar.sha256", + "wrapperChecksum" : "5c91fa893665f3051eae14578fac2df14e737423387e75ffbeccd35f335a3d8b" +}, { + "version" : "1.2-rc-1", + "buildTime" : "20120904174954+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.2", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.2-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.2-rc-1-bin.zip.sha256", + "checksum" : "167410b26ff883d6531f74c13a6c0683f3f21013c8445f35db58ccac9613b788" +}, { + "version" : "1.1", + "buildTime" : "20120731132432+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.1-bin.zip.sha256", + "checksum" : "552c1fc9f3a1b9668b79cc447370f0263e664ffb6d5c6e1c21e77ce0c8a20d4c", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.1-wrapper.jar.sha256", + "wrapperChecksum" : "22c56a9780daeee00e5bf31621f991b68e73eff6fe8afca628a1fe2c50c6038e" +}, { + "version" : "1.1-rc-2", + "buildTime" : "20120726075103+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.1-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.1-rc-2-bin.zip.sha256", + "checksum" : "78d904a6f0ec3231c4d032beddcfcee27046075624d724a9d087e0ad7e58d01d" +}, { + "version" : "1.1-rc-1", + "buildTime" : "20120724134404+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.1", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.1-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.1-rc-1-bin.zip.sha256", + "checksum" : "655c3b09260b0db10fa0563bc72fc05d5165b67734a87baa7aa9f708eeeb71ca" +}, { + "version" : "1.0", + "buildTime" : "20120612025621+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-bin.zip.sha256", + "checksum" : "894bca0360a7e2040815096788f118a2dd106ff6694221b274efb9c32bce0384", + "wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-wrapper.jar.sha256", + "wrapperChecksum" : "87e50531ca7aab675f5bb65755ef78328afd64cf0877e37ad876047a8a014055" +}, { + "version" : "1.0-rc-3", + "buildTime" : "20120430015152+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-3-bin.zip.sha256", + "checksum" : "a4b2a83bd7c173efc8722c111347283d26774a81508a445e4f234df98f711824" +}, { + "version" : "1.0-rc-2", + "buildTime" : "20120425015237+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-2-bin.zip.sha256", + "checksum" : "3ebb5367ede52e0f21246c3d06be70b7b66a96f078b2a139fa5c8269d5076911" +}, { + "version" : "1.0-rc-1", + "buildTime" : "20120411121324+0100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "1.0", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-rc-1-bin.zip.sha256", + "checksum" : "ae4ae81fb46b108f359efad5fbbcbec50940a793e5b2bfb9929d1e82f2e10c49" +}, { + "version" : "1.0-milestone-9", + "buildTime" : "20120313171009+0100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-9-bin.zip.sha256", + "checksum" : "3499b262c1407d3965250aa40f5a154e77672a5f904e7d82e704ce9fa82719bc" +}, { + "version" : "1.0-milestone-8a", + "buildTime" : "20120220185357+0100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-8a-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-8a-bin.zip.sha256", + "checksum" : "9dbace7ad2be4f4afdfde9aa495ff7a5f4cb2666ba5c7c21f01c119d9bbf6bc7" +}, { + "version" : "1.0-milestone-8", + "buildTime" : "20120214022451+0100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-8-bin.zip.sha256", + "checksum" : "480c3fe8c832c16c0d1e0f53b33b6401827baebd73eae2d7a3e89102c1dd8603" +}, { + "version" : "1.0-milestone-7", + "buildTime" : "20120105102443+0000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-7-bin.zip.sha256", + "checksum" : "074c37682e560d398cefa857dcd8fc67c318297d6d5dcd25d9f090332cd7564c" +}, { + "version" : "1.0-milestone-6", + "buildTime" : "20111117065412+0100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-6-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-6-bin.zip.sha256", + "checksum" : "9a2c3fca6aad82002a47cf6c721e6e5a45e0cbde3e280c8ef4eebefbe6797b1c" +}, { + "version" : "1.0-milestone-5", + "buildTime" : "20111025055608+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-5-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-5-bin.zip.sha256", + "checksum" : "b00acd0fef3e6b014e43d62e7a3e6159eac037df28e18057a04dddc6728f2362" +}, { + "version" : "1.0-milestone-4", + "buildTime" : "20110728103822+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : true, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-4-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-4-bin.zip.sha256", + "checksum" : "882c8115a214495f5b6b9df3ba70dec9345698651e8c320036eed272be55ae6a" +}, { + "version" : "1.0-milestone-3", + "buildTime" : "20110425174011+1000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-3-bin.zip.sha256", + "checksum" : "06d2ee96e45a62fce8ba1f964b083876b789c3f97932b189d57c5f545000d461" +}, { + "version" : "1.0-milestone-2", + "buildTime" : "20110407163255+1000", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-2-bin.zip.sha256", + "checksum" : "4fff2aa96eed3cec23537c807154ef3595ebaa99aa46d13dc479610ffcaa0574" +}, { + "version" : "1.0-milestone-1", + "buildTime" : "20110227141320+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-1.0-milestone-1-bin.zip.sha256", + "checksum" : "c2153f01eabba7abd874e8ebad42b7cfe76c4fb978bdbe288757b522a8755d8c" +}, { + "version" : "0.9.2", + "buildTime" : "20110123133421+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9.2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9.2-bin.zip.sha256", + "checksum" : "f94d7642348c558fc05ab5fd6fb947fb1ed8fed5931ddb73dd04fb0de22d669b" +}, { + "version" : "0.9.1", + "buildTime" : "20110102114057+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9.1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9.1-bin.zip.sha256", + "checksum" : "5d48cba95db031ec109ae9ab60561e960b6507046036e8191aa78572ec27e2a5" +}, { + "version" : "0.9", + "buildTime" : "20101219125006+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9-bin.zip.sha256", + "checksum" : "994e46d4b467254a0f25ce92b602618331b9b3ac8b32a094fd84ff0e0ceec135" +}, { + "version" : "0.9-rc-3", + "buildTime" : "20101120131750+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "0.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-3-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-3-bin.zip.sha256", + "checksum" : "3d07a0de2d8aa1c8ef7e6974c8eb84f47d0300d978c905d046a7d1087a0cb1a8" +}, { + "version" : "0.9-rc-2", + "buildTime" : "20101027082405+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "0.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-2-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-2-bin.zip.sha256", + "checksum" : "94c0b7d51514062c8bdb7d1435a130f4eafda99214274b4330fb77a41b466a01" +}, { + "version" : "0.9-rc-1", + "buildTime" : "20100804080433+1100", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "0.9", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-1-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.9-rc-1-bin.zip.sha256", + "checksum" : "344b93a2825d29ecacd8c9e9c69037b5badd87b219ef56d88b35b3ba07f9e714" +}, { + "version" : "0.8", + "buildTime" : "20090928140159+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.8-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.8-bin.zip.sha256", + "checksum" : "940e623ea98e40ea9ad398770a6ebb91a61c0869d394dda81aa86b0f4f0025e7" +}, { + "version" : "0.7", + "buildTime" : "20090720085013+0200", + "commitId" : "", + "current" : false, + "snapshot" : false, + "nightly" : false, + "releaseNightly" : false, + "activeRc" : false, + "rcFor" : "", + "milestoneFor" : "", + "broken" : false, + "downloadUrl" : "https://services.gradle.org/distributions/gradle-0.7-bin.zip", + "checksumUrl" : "https://services.gradle.org/distributions/gradle-0.7-bin.zip.sha256", + "checksum" : "4e354fcb0d5c0b0e7789cd6ee900456edaf993f6dd890c4a1c217d90d2a6a6ad" +} ] \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd new file mode 100644 index 0000000..08c1e94 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/70059da43e490011162d85db97d6efcd @@ -0,0 +1,305 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd new file mode 100644 index 0000000..72f35b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/900497d880460011162d85db97d6efcd @@ -0,0 +1,31 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(); + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd new file mode 100644 index 0000000..afce174 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0421e9f5e490011162d85db97d6efcd @@ -0,0 +1,491 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/0/b0f5b1bc40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0f5b1bc40490011162d85db97d6efcd new file mode 100644 index 0000000..4cb4459 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/b0f5b1bc40490011162d85db97d6efcd @@ -0,0 +1,327 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd new file mode 100644 index 0000000..acd33d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/d003190040490011162d85db97d6efcd @@ -0,0 +1,317 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + } else { + // il nodo non è da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd new file mode 100644 index 0000000..ab6a88a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e00b44d82b470011162d85db97d6efcd @@ -0,0 +1,92 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd new file mode 100644 index 0000000..db797a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/0/e0c30c8f81460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd new file mode 100644 index 0000000..8991a01 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/0095d2f77f460011162d85db97d6efcd @@ -0,0 +1,16 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd new file mode 100644 index 0000000..74a8855 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/30e625e95c490011162d85db97d6efcd @@ -0,0 +1,468 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd new file mode 100644 index 0000000..28c340a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/401b103780490011162d85db97d6efcd @@ -0,0 +1,35 @@ +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/1/4039dfcd81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/4039dfcd81460011162d85db97d6efcd new file mode 100644 index 0000000..69e8da3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/4039dfcd81460011162d85db97d6efcd @@ -0,0 +1,51 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd new file mode 100644 index 0000000..c6b2a11 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/a077e1602f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd new file mode 100644 index 0000000..584bb15 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1/e003eb146a460011162d85db97d6efcd @@ -0,0 +1,78 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e7d3c62 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/10/10a789b53e4600111c1cd5b6f02d115c @@ -0,0 +1,5 @@ +package vettore_ordinabile; + +public class VettoriIntero { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd new file mode 100644 index 0000000..be84efc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/10/a0ab1ab53e490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd new file mode 100644 index 0000000..452ad5d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/10/b0ff7b065a490011162d85db97d6efcd @@ -0,0 +1,439 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + + } else { + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/10935b94ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/10935b94ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..569e4bf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/10935b94ed4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack; + */ + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/10cda413f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/10cda413f04a0011162d85db97d6efcd new file mode 100644 index 0000000..62bc9fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/10cda413f04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i < stack.size(); i++) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd new file mode 100644 index 0000000..f4ade3c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/7022f37f5e490011162d85db97d6efcd @@ -0,0 +1,492 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/11/c093113c394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/c093113c394b0011162d85db97d6efcd new file mode 100644 index 0000000..f6f18e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/c093113c394b0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd new file mode 100644 index 0000000..0678796 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/11/e02ffab06a460011162d85db97d6efcd @@ -0,0 +1,91 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd new file mode 100644 index 0000000..1f13806 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/1031c6fa75490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/5000980885460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/5000980885460011162d85db97d6efcd new file mode 100644 index 0000000..2bc745d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/5000980885460011162d85db97d6efcd @@ -0,0 +1,68 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree.root = root.getLeft(); + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd new file mode 100644 index 0000000..65702fe --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/70d119134b490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/904377c885460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/904377c885460011162d85db97d6efcd new file mode 100644 index 0000000..c03bf87 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/904377c885460011162d85db97d6efcd @@ -0,0 +1,74 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd new file mode 100644 index 0000000..99c0434 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/a0c89fa35d490011162d85db97d6efcd @@ -0,0 +1,488 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(node.getRight(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd new file mode 100644 index 0000000..8a079e6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/12/c0e566217c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/13/1091241ef24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/1091241ef24a0011162d85db97d6efcd new file mode 100644 index 0000000..08d201e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/1091241ef24a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(): + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd new file mode 100644 index 0000000..ceb5f29 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/20c9da0a5e490011162d85db97d6efcd @@ -0,0 +1,483 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd new file mode 100644 index 0000000..8f54683 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/a0e694456a460011162d85db97d6efcd @@ -0,0 +1,79 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) return null; + this.parent = parent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd new file mode 100644 index 0000000..03137e7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/13/e0919e1569460011162d85db97d6efcd @@ -0,0 +1,73 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/20354760ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20354760ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..8bb540f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/20354760ed4a0011162d85db97d6efcd @@ -0,0 +1,14 @@ +package stack; + +public interface MyStack { + + void push(T item); + + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd new file mode 100644 index 0000000..7215794 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/706367cf40490011162d85db97d6efcd @@ -0,0 +1,327 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + stackOfNodes.push(current); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd new file mode 100644 index 0000000..0fe706a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/a05f2dd22f470011162d85db97d6efcd @@ -0,0 +1,138 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/14/a09f06e2394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/14/a09f06e2394b0011162d85db97d6efcd new file mode 100644 index 0000000..76aca29 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/14/a09f06e2394b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + return id.compareTo(p.id); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd new file mode 100644 index 0000000..353fa04 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/206cc4926a460011162d85db97d6efcd @@ -0,0 +1,84 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd new file mode 100644 index 0000000..89b3429 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/505ac16132490011162d85db97d6efcd @@ -0,0 +1,248 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd new file mode 100644 index 0000000..c4b00ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/609e82a97f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/15/701630bb68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/701630bb68460011162d85db97d6efcd new file mode 100644 index 0000000..100bbc2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/701630bb68460011162d85db97d6efcd @@ -0,0 +1,62 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..91d2140 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f009323e2f4600111c1cd5b6f02d115c @@ -0,0 +1,9 @@ +package test; + +public class Main { + + public static void main (String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd new file mode 100644 index 0000000..4d4d0d3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/15/f0914c102d470011162d85db97d6efcd @@ -0,0 +1,106 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + + } + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd new file mode 100644 index 0000000..6607137 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/60bcdb7175460011162d85db97d6efcd @@ -0,0 +1,128 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + return this.left.equals(otherType.left) + && this.right.equals(otherType.right) + && this.parent.equals(otherType.parent) + && this.data.equals(otherType.data); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd new file mode 100644 index 0000000..2edba06 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/a0b9858d3d490011162d85db97d6efcd @@ -0,0 +1,281 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + } + + // ITERATOR + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/16/d04018a7be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/16/d04018a7be4b0011162d85db97d6efcd new file mode 100644 index 0000000..1a96ce5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/16/d04018a7be4b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd new file mode 100644 index 0000000..096c319 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/103e05502c470011162d85db97d6efcd @@ -0,0 +1,96 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd new file mode 100644 index 0000000..0fd5323 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b03944b82f470011162d85db97d6efcd @@ -0,0 +1,132 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd new file mode 100644 index 0000000..4a8eed4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/b0ab36db5f490011162d85db97d6efcd @@ -0,0 +1,494 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/17/c0f8a7f1fb4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/c0f8a7f1fb4a0011162d85db97d6efcd new file mode 100644 index 0000000..1247baf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/c0f8a7f1fb4a0011162d85db97d6efcd @@ -0,0 +1,14 @@ +package jcf_set.example; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd new file mode 100644 index 0000000..dd9371f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/17/e02b569268460011162d85db97d6efcd @@ -0,0 +1,60 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/18/803dcbe4ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/18/803dcbe4ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..11cd086 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/18/803dcbe4ef4a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = 0; i < stack.size(); i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd new file mode 100644 index 0000000..5ecd425 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/604b22da73460011162d85db97d6efcd @@ -0,0 +1,121 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd new file mode 100644 index 0000000..68b5dc6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/6072390933470011162d85db97d6efcd @@ -0,0 +1,193 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd new file mode 100644 index 0000000..bbf4c95 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/707b36db2c470011162d85db97d6efcd @@ -0,0 +1,101 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd new file mode 100644 index 0000000..7ef93ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/d052e6302c470011162d85db97d6efcd @@ -0,0 +1,94 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd new file mode 100644 index 0000000..807e8dc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/e07eb6514d490011162d85db97d6efcd @@ -0,0 +1,48 @@ +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/19/f0d1eeb280460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/19/f0d1eeb280460011162d85db97d6efcd new file mode 100644 index 0000000..73b1af4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/19/f0d1eeb280460011162d85db97d6efcd @@ -0,0 +1,31 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd new file mode 100644 index 0000000..cf5fb22 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/5001537d4c490011162d85db97d6efcd @@ -0,0 +1,31 @@ +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/1a/50aa714def4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/50aa714def4a0011162d85db97d6efcd new file mode 100644 index 0000000..435bd1d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1a/50aa714def4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package stack; + +public class StackExample { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd new file mode 100644 index 0000000..30adbac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/103a19cf59490011162d85db97d6efcd @@ -0,0 +1,432 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + + currentNode.setData(currentObject); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd new file mode 100644 index 0000000..b9a9895 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/3006a2397f490011162d85db97d6efcd @@ -0,0 +1,16 @@ +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/1b/d00ddd7035470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/d00ddd7035470011162d85db97d6efcd new file mode 100644 index 0000000..e6efac4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/d00ddd7035470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) templist.add(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd new file mode 100644 index 0000000..7a12f8f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1b/f04eb43243490011162d85db97d6efcd @@ -0,0 +1,383 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd new file mode 100644 index 0000000..163a607 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/1089a0fc85460011162d85db97d6efcd @@ -0,0 +1,83 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd new file mode 100644 index 0000000..2d90c3c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/40d7bbfe5f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/1c/50fdff1086460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50fdff1086460011162d85db97d6efcd new file mode 100644 index 0000000..e2af5e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/50fdff1086460011162d85db97d6efcd @@ -0,0 +1,87 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + + size = size - rightTree.size; + + return rightTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..b0ed4c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/707009212f4600111c1cd5b6f02d115c @@ -0,0 +1,5 @@ +package test; + +public class Main { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd new file mode 100644 index 0000000..b913eb2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/b080545780460011162d85db97d6efcd @@ -0,0 +1,26 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1c/f0557227be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/f0557227be4b0011162d85db97d6efcd new file mode 100644 index 0000000..554f7e0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1c/f0557227be4b0011162d85db97d6efcd @@ -0,0 +1,38 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd new file mode 100644 index 0000000..cc6d380 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/a0be4c3060490011162d85db97d6efcd @@ -0,0 +1,502 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/1d/e05fc4193f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/e05fc4193f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..dd83a09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1d/e05fc4193f4600111c1cd5b6f02d115c @@ -0,0 +1,28 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + public + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd new file mode 100644 index 0000000..b1ef8bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/101c63307c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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 new file mode 100644 index 0000000..68ea4aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/20d762397b490011162d85db97d6efcd @@ -0,0 +1,26 @@ +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/4001018a30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/4001018a30470011162d85db97d6efcd new file mode 100644 index 0000000..35c1f7f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/4001018a30470011162d85db97d6efcd @@ -0,0 +1,156 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd new file mode 100644 index 0000000..6ffca48 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/7091e8325b490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + //newNode.setParent(currentNode); + + if (wing == 0) { + // Left Wing + newNode.setParentAsLeftChild(currentNode); + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd new file mode 100644 index 0000000..1d2d4e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/805daf257f490011162d85db97d6efcd @@ -0,0 +1,16 @@ +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/1e/908435fef14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/908435fef14a0011162d85db97d6efcd new file mode 100644 index 0000000..9901e53 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/908435fef14a0011162d85db97d6efcd @@ -0,0 +1,15 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..d807702 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/c0a3f7d33c4600111c1cd5b6f02d115c @@ -0,0 +1,79 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + Object temp = vettore[minimo]; + vettore[minimo] = vettore[j]; + vettore[j] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd new file mode 100644 index 0000000..813a42f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1e/f0f179f12c470011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd new file mode 100644 index 0000000..7c3bfa4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/1f/40f143b075490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/2/00b2bb6bf24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/00b2bb6bf24a0011162d85db97d6efcd new file mode 100644 index 0000000..b2e0592 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/00b2bb6bf24a0011162d85db97d6efcd @@ -0,0 +1,29 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.getSize(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd new file mode 100644 index 0000000..ef3eb85 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/3034e82969460011162d85db97d6efcd @@ -0,0 +1,74 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/304281cfef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/304281cfef4a0011162d85db97d6efcd new file mode 100644 index 0000000..46b8578 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/304281cfef4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd new file mode 100644 index 0000000..90b6dcf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/a0b13da340490011162d85db97d6efcd @@ -0,0 +1,324 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd new file mode 100644 index 0000000..2c40ab2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2/f018554b2b470011162d85db97d6efcd @@ -0,0 +1,88 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/00c5738dbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/00c5738dbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..301054c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/00c5738dbe4b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd new file mode 100644 index 0000000..5b4b7b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/10af506867460011162d85db97d6efcd @@ -0,0 +1,25 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd new file mode 100644 index 0000000..3c360ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/2084099065460011162d85db97d6efcd @@ -0,0 +1,7 @@ +package binary_tree; + +public interface BinaryTree { + + public boolean isEmpty(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/5080c1f6f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/5080c1f6f74a0011162d85db97d6efcd new file mode 100644 index 0000000..7635879 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/5080c1f6f74a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package jcf_set.example; + +public class TreeSetExample2 { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e514122 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/b0bcb82a3f4600111c1cd5b6f02d115c @@ -0,0 +1,29 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/c0945593ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/c0945593ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..55a2886 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/c0945593ed4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return + */ + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd new file mode 100644 index 0000000..aa4db0d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/d0de926f6a460011162d85db97d6efcd @@ -0,0 +1,82 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) return null; + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + return oldLeft; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd new file mode 100644 index 0000000..2a26f2d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/e0aa186b76460011162d85db97d6efcd @@ -0,0 +1,138 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/20/f01b1c57fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/20/f01b1c57fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..cc2b80d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/20/f01b1c57fc4a0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd new file mode 100644 index 0000000..5b8eb1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/008bd7082d470011162d85db97d6efcd @@ -0,0 +1,105 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd new file mode 100644 index 0000000..22bde1c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/206ca7aa7b490011162d85db97d6efcd @@ -0,0 +1,36 @@ +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/21/70a4ede966460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/70a4ede966460011162d85db97d6efcd new file mode 100644 index 0000000..04a5089 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/70a4ede966460011162d85db97d6efcd @@ -0,0 +1,5 @@ +package binary_tree; + +public class BinaryNode { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/21/c0a98fd4394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/21/c0a98fd4394b0011162d85db97d6efcd new file mode 100644 index 0000000..bbd275e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/21/c0a98fd4394b0011162d85db97d6efcd @@ -0,0 +1,40 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd new file mode 100644 index 0000000..4a49194 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/22/00e13d4a85460011162d85db97d6efcd @@ -0,0 +1,71 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/22/506eb470f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/22/506eb470f24a0011162d85db97d6efcd new file mode 100644 index 0000000..a34d112 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/22/506eb470f24a0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.size(); + } + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/20fabf44ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/20fabf44ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..bc16a84 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/20fabf44ee4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.get(array.size() - 1); + } + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c new file mode 100644 index 0000000..9b45523 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/5027649b314600111c1cd5b6f02d115c @@ -0,0 +1,26 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + dimensioneCorrente++; + return true; + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd new file mode 100644 index 0000000..009c5c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/90d9521d7b490011162d85db97d6efcd @@ -0,0 +1,26 @@ +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/23/b0a5aaef67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/23/b0a5aaef67460011162d85db97d6efcd new file mode 100644 index 0000000..4ac1be1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/b0a5aaef67460011162d85db97d6efcd @@ -0,0 +1,46 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c new file mode 100644 index 0000000..069b3c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/23/c0d1ce89404600111c1cd5b6f02d115c @@ -0,0 +1,15 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + System.out.println("\nTest VettoreIntero"); + Integer i1 = Integer.valueOf(3); + Integer i2 = Integer.valueOf(389); + Integer i3 = Integer.valueOf(15); + Integer i4 = Integer.valueOf(10); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd new file mode 100644 index 0000000..d51430c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/40260ba25a490011162d85db97d6efcd @@ -0,0 +1,442 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd new file mode 100644 index 0000000..269c3a3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/70d3c00d2c470011162d85db97d6efcd @@ -0,0 +1,93 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals()) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c new file mode 100644 index 0000000..5084cae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/8045b8de374600111c1cd5b6f02d115c @@ -0,0 +1,40 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd new file mode 100644 index 0000000..b3de34a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/90e76d9a3e490011162d85db97d6efcd @@ -0,0 +1,303 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(current); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0ffb9f9f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0ffb9f9f74a0011162d85db97d6efcd new file mode 100644 index 0000000..7f350c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/24/d0ffb9f9f74a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package jcf_set.example; + +public class TreeSetExample2 { + + public static void main() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd new file mode 100644 index 0000000..52d678e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/20b0acbb3f490011162d85db97d6efcd @@ -0,0 +1,313 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + + queueOfNodes.add(node); + flags.push(); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd new file mode 100644 index 0000000..1e8d18c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/3096b49281460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd new file mode 100644 index 0000000..a9ea524 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/70a570e785460011162d85db97d6efcd @@ -0,0 +1,81 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd new file mode 100644 index 0000000..d4d46cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/25/a046cf4978490011162d85db97d6efcd @@ -0,0 +1,18 @@ +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/26/100dcca9384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/26/100dcca9384b0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/26/2061d796f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/26/2061d796f04a0011162d85db97d6efcd new file mode 100644 index 0000000..6218c39 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/26/2061d796f04a0011162d85db97d6efcd @@ -0,0 +1,11 @@ +package queue; + +public interface MyQueue { + + /** + * Aggiungi l'elemento dato + * @param item + */ + boolean offer(T item); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd new file mode 100644 index 0000000..78edc41 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/100aff2681460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd new file mode 100644 index 0000000..9f5e327 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/40b8d14b76490011162d85db97d6efcd @@ -0,0 +1,55 @@ +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/90ee430086460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/90ee430086460011162d85db97d6efcd new file mode 100644 index 0000000..bbdba77 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/90ee430086460011162d85db97d6efcd @@ -0,0 +1,86 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + + size = size - rightTree.size; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd new file mode 100644 index 0000000..de2492c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/a08d520d63490011162d85db97d6efcd @@ -0,0 +1,515 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/27/c0046786374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/27/c0046786374600111c1cd5b6f02d115c new file mode 100644 index 0000000..2462b4a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/c0046786374600111c1cd5b6f02d115c @@ -0,0 +1,36 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c new file mode 100644 index 0000000..8c9e943 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/27/f0494b33314600111c1cd5b6f02d115c @@ -0,0 +1,16 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd new file mode 100644 index 0000000..f3a5ae4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/207469946b460011162d85db97d6efcd @@ -0,0 +1,114 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd new file mode 100644 index 0000000..c6b4ab4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/702c55b85a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode; + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/d0087841f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/d0087841f34a0011162d85db97d6efcd new file mode 100644 index 0000000..bb744c0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/d0087841f34a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.size(); + } + + @Override + public boolean isEmpty() { + return queue.isEmpty(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd new file mode 100644 index 0000000..bb83a34 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/f07f62ee75490011162d85db97d6efcd @@ -0,0 +1,28 @@ +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/28/f0b79929fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/28/f0b79929fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..085ca17 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/28/f0b79929fc4a0011162d85db97d6efcd @@ -0,0 +1,24 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Tree1: " + tree1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd new file mode 100644 index 0000000..5ddce91 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2027f94b5a490011162d85db97d6efcd @@ -0,0 +1,443 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd new file mode 100644 index 0000000..6a9d00d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2051e95e2f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd new file mode 100644 index 0000000..17cea95 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/2054227e6b460011162d85db97d6efcd @@ -0,0 +1,108 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd new file mode 100644 index 0000000..db80a44 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/503997125e490011162d85db97d6efcd @@ -0,0 +1,483 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c new file mode 100644 index 0000000..4aacb98 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/50d091b5314600111c1cd5b6f02d115c @@ -0,0 +1,27 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/70c884efec4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/70c884efec4a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c new file mode 100644 index 0000000..783a71d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/80a523b6304600111c1cd5b6f02d115c @@ -0,0 +1,9 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..033bb76 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/900e07dc3f4600111c1cd5b6f02d115c @@ -0,0 +1,51 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c new file mode 100644 index 0000000..6225d1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d06f675c404600111c1cd5b6f02d115c @@ -0,0 +1,5 @@ +package vettore_ordinabile; + +public class Main { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd new file mode 100644 index 0000000..7465496 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/29/d0d2aa9b69460011162d85db97d6efcd @@ -0,0 +1,74 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd new file mode 100644 index 0000000..d174dcb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/9016855b76460011162d85db97d6efcd @@ -0,0 +1,133 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/90734e65f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/90734e65f14a0011162d85db97d6efcd new file mode 100644 index 0000000..816b749 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/90734e65f14a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package queue; + +public class ArrayListQueue { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd new file mode 100644 index 0000000..811fdba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2a/d0a5603a80490011162d85db97d6efcd @@ -0,0 +1,37 @@ +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/10e2d00a32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10e2d00a32490011162d85db97d6efcd new file mode 100644 index 0000000..c005f2f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/10e2d00a32490011162d85db97d6efcd @@ -0,0 +1,241 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd new file mode 100644 index 0000000..e772795 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/20551faf7a490011162d85db97d6efcd @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..c24a1eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/804ca40980490011162d85db97d6efcd @@ -0,0 +1,34 @@ +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/2b/a04f1c255b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/a04f1c255b490011162d85db97d6efcd new file mode 100644 index 0000000..cf6a147 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/a04f1c255b490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + //newNode.setParent(currentNode); + + if (wing == 0) { + // Left Wing + currentNode. + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b0c9eeeded4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b0c9eeeded4a0011162d85db97d6efcd new file mode 100644 index 0000000..3151524 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2b/b0c9eeeded4a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack{ + + private ArrayList array = new ArrayList(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/40fd351df04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/40fd351df04a0011162d85db97d6efcd new file mode 100644 index 0000000..a64227a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/40fd351df04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i >= 0; i--) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/606a007bef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/606a007bef4a0011162d85db97d6efcd new file mode 100644 index 0000000..4b6385b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/606a007bef4a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStak(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd new file mode 100644 index 0000000..97490a1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/7064b5ea6a460011162d85db97d6efcd @@ -0,0 +1,98 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd new file mode 100644 index 0000000..52e06c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2c/e034ebef4b490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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 new file mode 100644 index 0000000..8e8db03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/0069cdb27a490011162d85db97d6efcd @@ -0,0 +1,11 @@ +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/2d/50c9b6775d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/50c9b6775d490011162d85db97d6efcd new file mode 100644 index 0000000..00ae017 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/50c9b6775d490011162d85db97d6efcd @@ -0,0 +1,484 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..2208831 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/80cae89c3c4600111c1cd5b6f02d115c @@ -0,0 +1,77 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd new file mode 100644 index 0000000..5c455c7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/b01fc5e05c490011162d85db97d6efcd @@ -0,0 +1,468 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2d/e092ee7cfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/e092ee7cfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..213dee6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2d/e092ee7cfc4a0011162d85db97d6efcd @@ -0,0 +1,36 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd new file mode 100644 index 0000000..86c3e12 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/00ed20543e490011162d85db97d6efcd @@ -0,0 +1,299 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd new file mode 100644 index 0000000..8592580 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/b0be493c60490011162d85db97d6efcd @@ -0,0 +1,503 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/2e/f01de0c92b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f01de0c92b470011162d85db97d6efcd new file mode 100644 index 0000000..bd747d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f01de0c92b470011162d85db97d6efcd @@ -0,0 +1,90 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd new file mode 100644 index 0000000..35bd981 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2e/f0c04f6e5e490011162d85db97d6efcd @@ -0,0 +1,488 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/20ba1661ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/20ba1661ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..db99351 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/20ba1661ed4a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack + * @param item + */ + void push(T item); + + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd new file mode 100644 index 0000000..032ceee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/3033617531470011162d85db97d6efcd @@ -0,0 +1,172 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/4066f1f0384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/4066f1f0384b0011162d85db97d6efcd new file mode 100644 index 0000000..9d65563 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/4066f1f0384b0011162d85db97d6efcd @@ -0,0 +1,15 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/50787513f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/50787513f34a0011162d85db97d6efcd new file mode 100644 index 0000000..19c1906 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/50787513f34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 2; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd new file mode 100644 index 0000000..656eed2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/2f/d0d9fcef66460011162d85db97d6efcd @@ -0,0 +1,9 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd new file mode 100644 index 0000000..15fcc12 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/10b05e8535470011162d85db97d6efcd @@ -0,0 +1,225 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd new file mode 100644 index 0000000..10153c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/50f7940050490011162d85db97d6efcd @@ -0,0 +1,409 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + public LinkedBinaryTree () { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/b052b858ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/b052b858ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..c04f328 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/b052b858ef4a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package stack; + +public class StackExample { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd new file mode 100644 index 0000000..7941d03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/b0f262ea5f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/3/d00ce60a76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00ce60a76460011162d85db97d6efcd new file mode 100644 index 0000000..aa8d714 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3/d00ce60a76460011162d85db97d6efcd @@ -0,0 +1,126 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd new file mode 100644 index 0000000..ef3256a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/1029016641490011162d85db97d6efcd @@ -0,0 +1,341 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c new file mode 100644 index 0000000..750f92a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/30608b86384600111c1cd5b6f02d115c @@ -0,0 +1,56 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + + } + + // + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd new file mode 100644 index 0000000..70b4aa6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/508693975d490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd new file mode 100644 index 0000000..d13fe36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/a05c728081460011162d85db97d6efcd @@ -0,0 +1,38 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd new file mode 100644 index 0000000..b994ee6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/b0face3276490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/c0e900e45c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/c0e900e45c490011162d85db97d6efcd new file mode 100644 index 0000000..31e0fd8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/c0e900e45c490011162d85db97d6efcd @@ -0,0 +1,468 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd new file mode 100644 index 0000000..b5f28f0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/d05aa57776490011162d85db97d6efcd @@ -0,0 +1,63 @@ +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/30/f024b70943490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/30/f024b70943490011162d85db97d6efcd new file mode 100644 index 0000000..560dac0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/30/f024b70943490011162d85db97d6efcd @@ -0,0 +1,375 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd new file mode 100644 index 0000000..d857c4b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/101e633c76460011162d85db97d6efcd @@ -0,0 +1,128 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd new file mode 100644 index 0000000..04fce2a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/801ddaf83e490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd new file mode 100644 index 0000000..d275ddc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/80962c2f3d490011162d85db97d6efcd @@ -0,0 +1,274 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd new file mode 100644 index 0000000..fa17a9d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/9059d68d5c490011162d85db97d6efcd @@ -0,0 +1,463 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd new file mode 100644 index 0000000..b030917 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/e07e11da5a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/31/f0068fc6384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/31/f0068fc6384b0011162d85db97d6efcd new file mode 100644 index 0000000..be6e4dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/31/f0068fc6384b0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package parziale.p251110; + +public class Paziente { + + private String name; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd new file mode 100644 index 0000000..5275ec0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/32/20214def40490011162d85db97d6efcd @@ -0,0 +1,328 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd new file mode 100644 index 0000000..d8a478a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/32/30c53a3e2f470011162d85db97d6efcd @@ -0,0 +1,127 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/70666034f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/70666034f24a0011162d85db97d6efcd new file mode 100644 index 0000000..5089f91 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/32/70666034f24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.get(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/90c31166f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/90c31166f14a0011162d85db97d6efcd new file mode 100644 index 0000000..6bc946f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/32/90c31166f14a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package queue; + +public class ArrayListQueue implements MyQueue{ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd new file mode 100644 index 0000000..933752d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/32/e05232e17c490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/20555aab3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/20555aab3e490011162d85db97d6efcd new file mode 100644 index 0000000..2c98457 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/20555aab3e490011162d85db97d6efcd @@ -0,0 +1,305 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c new file mode 100644 index 0000000..a10bf92 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/50183093404600111c1cd5b6f02d115c @@ -0,0 +1,17 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + System.out.println("\nTest VettoreIntero"); + Integer i1 = Integer.valueOf(3); + Integer i2 = Integer.valueOf(389); + Integer i3 = Integer.valueOf(15); + Integer i4 = Integer.valueOf(10); + + VettoriIntero VI = new VettoriIntero(4); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd new file mode 100644 index 0000000..0824a88 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/708568527c490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/33/80be87bdfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/80be87bdfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..420fde6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/80be87bdfc4a0011162d85db97d6efcd @@ -0,0 +1,40 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet2: "); + System.out.println(tree2); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + System.out.println("Stampa del TreeSet3: "); + System.out.println(tree3); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/90b6c26af34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/90b6c26af34a0011162d85db97d6efcd new file mode 100644 index 0000000..350d571 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/90b6c26af34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = 0; i < queue.size(); i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/33/c0990477f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/33/c0990477f14a0011162d85db97d6efcd new file mode 100644 index 0000000..cc47403 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/33/c0990477f14a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package queue; + +public class ArrayListQueue implements MyQueue { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd new file mode 100644 index 0000000..a43a0bb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/204f00d87b490011162d85db97d6efcd @@ -0,0 +1,61 @@ +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/34/5030ac8776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/5030ac8776460011162d85db97d6efcd new file mode 100644 index 0000000..8c1828b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/5030ac8776460011162d85db97d6efcd @@ -0,0 +1,140 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd new file mode 100644 index 0000000..9280718 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/609f716535470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd new file mode 100644 index 0000000..70d0134 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/34/b0b6c0af34470011162d85db97d6efcd @@ -0,0 +1,214 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/50b50565f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/50b50565f24a0011162d85db97d6efcd new file mode 100644 index 0000000..74285ba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/50b50565f24a0011162d85db97d6efcd @@ -0,0 +1,27 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd new file mode 100644 index 0000000..bc16a9a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/60bf840461490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/35/904c74e067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/904c74e067460011162d85db97d6efcd new file mode 100644 index 0000000..16b41a9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/904c74e067460011162d85db97d6efcd @@ -0,0 +1,44 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd new file mode 100644 index 0000000..75381d3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/a077af3c5b490011162d85db97d6efcd @@ -0,0 +1,446 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + //newNode.setParent(currentNode); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd new file mode 100644 index 0000000..9199865 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/35/e072a36537470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(node, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd new file mode 100644 index 0000000..3bdb680 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/008629692f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd new file mode 100644 index 0000000..c614f58 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/607853ba34470011162d85db97d6efcd @@ -0,0 +1,219 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd new file mode 100644 index 0000000..b308136 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/8026338343490011162d85db97d6efcd @@ -0,0 +1,394 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd new file mode 100644 index 0000000..d670fcb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/36/a05800f573460011162d85db97d6efcd @@ -0,0 +1,122 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + BinaryNode otherType = (BinaryNode)other; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd new file mode 100644 index 0000000..37833fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/201620542f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + temporaryList.add(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd new file mode 100644 index 0000000..17bebbd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/70952f4a80490011162d85db97d6efcd @@ -0,0 +1,38 @@ +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 new file mode 100644 index 0000000..c6ab412 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/b021defa7c490011162d85db97d6efcd @@ -0,0 +1,43 @@ +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 new file mode 100644 index 0000000..36c029f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/37/d0436add7b490011162d85db97d6efcd @@ -0,0 +1,64 @@ +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/38/20e7f42068460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/20e7f42068460011162d85db97d6efcd new file mode 100644 index 0000000..6f196b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/20e7f42068460011162d85db97d6efcd @@ -0,0 +1,52 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd new file mode 100644 index 0000000..ea26961 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/40cae98f2d470011162d85db97d6efcd @@ -0,0 +1,111 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return (); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/7051dac9424600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/38/7051dac9424600111c1cd5b6f02d115c new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd new file mode 100644 index 0000000..626c413 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/707b9bcd2f470011162d85db97d6efcd @@ -0,0 +1,136 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c new file mode 100644 index 0000000..c8864f5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/38/d092a59f374600111c1cd5b6f02d115c @@ -0,0 +1,38 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/0087dfdbed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/0087dfdbed4a0011162d85db97d6efcd new file mode 100644 index 0000000..66ae807 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/0087dfdbed4a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * Ritorna il numero di elementi correnti nello stack. + * @return Numero di elementi nello stack. + */ + int size(); + + /** + * Ritorna se ci sono elementi nello stack o meno. + * @return + */ + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/308ae8dcfb4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/308ae8dcfb4a0011162d85db97d6efcd new file mode 100644 index 0000000..99ba406 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/308ae8dcfb4a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package jcf_set.example; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd new file mode 100644 index 0000000..3afa073 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/50ddc46130470011162d85db97d6efcd @@ -0,0 +1,142 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd new file mode 100644 index 0000000..7901f6f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6028b60036470011162d85db97d6efcd @@ -0,0 +1,229 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd new file mode 100644 index 0000000..e244808 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/6037c85e5a490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + + + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd new file mode 100644 index 0000000..34238dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/70135f1169460011162d85db97d6efcd @@ -0,0 +1,69 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0a33c93ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0a33c93ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..57ca59a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/a0a33c93ef4a0011162d85db97d6efcd @@ -0,0 +1,17 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character a = 'b'; + Character a = 'c'; + Character a = 'd'; + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd new file mode 100644 index 0000000..5ca3e69 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/b0015c7c4d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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 new file mode 100644 index 0000000..5c953ce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/39/e08f03e560490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..981e4cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/0045265d4b490011162d85db97d6efcd @@ -0,0 +1,18 @@ +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/005ff1f72c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/005ff1f72c470011162d85db97d6efcd new file mode 100644 index 0000000..7e80b87 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/005ff1f72c470011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd new file mode 100644 index 0000000..5876ae7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/00936cc876460011162d85db97d6efcd @@ -0,0 +1,146 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd new file mode 100644 index 0000000..933752d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/308204547c490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/3a/709a0c6a68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/709a0c6a68460011162d85db97d6efcd new file mode 100644 index 0000000..4f3ff55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/709a0c6a68460011162d85db97d6efcd @@ -0,0 +1,57 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + if (parent != null && parent.getLeft() == this) return true; + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3a/e055b16ef34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/e055b16ef34a0011162d85db97d6efcd new file mode 100644 index 0000000..6e86212 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3a/e055b16ef34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd new file mode 100644 index 0000000..299025a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/00529eea33490011162d85db97d6efcd @@ -0,0 +1,271 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd new file mode 100644 index 0000000..f6878cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/7080cb0d5e490011162d85db97d6efcd @@ -0,0 +1,483 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd new file mode 100644 index 0000000..2086e8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/b08174893e490011162d85db97d6efcd @@ -0,0 +1,300 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3b/f07ef850404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/3b/f07ef850404600111c1cd5b6f02d115c new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd new file mode 100644 index 0000000..663f5c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/a072006041490011162d85db97d6efcd @@ -0,0 +1,340 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..a18f6de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3c/f0e216ee3e4600111c1cd5b6f02d115c @@ -0,0 +1,13 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00c30ff0ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00c30ff0ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..b0070cd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/00c30ff0ef4a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = 0; i < stack.size(); i++) { + stack.pop(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd new file mode 100644 index 0000000..83a2cad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/308c490330470011162d85db97d6efcd @@ -0,0 +1,139 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd new file mode 100644 index 0000000..a997bec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/809402126b460011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + return oldParent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd new file mode 100644 index 0000000..d2979c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/80cf170976490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/3d/d0cc27995b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/d0cc27995b490011162d85db97d6efcd new file mode 100644 index 0000000..63421f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3d/d0cc27995b490011162d85db97d6efcd @@ -0,0 +1,446 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + root = new BinaryNode(); + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3e/f0c6375075490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3e/f0c6375075490011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd new file mode 100644 index 0000000..9627d1a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/4059500f3e490011162d85db97d6efcd @@ -0,0 +1,288 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd new file mode 100644 index 0000000..54f84c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/504df4ec2d470011162d85db97d6efcd @@ -0,0 +1,111 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd new file mode 100644 index 0000000..58fb333 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/50df0d247b490011162d85db97d6efcd @@ -0,0 +1,26 @@ +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/3f/f05f9e933f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/f05f9e933f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..114910c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/3f/f05f9e933f4600111c1cd5b6f02d115c @@ -0,0 +1,50 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd new file mode 100644 index 0000000..6afbb30 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/20401eed75490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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/30d728f0374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30d728f0374600111c1cd5b6f02d115c new file mode 100644 index 0000000..a341225 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30d728f0374600111c1cd5b6f02d115c @@ -0,0 +1,45 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd new file mode 100644 index 0000000..167ff74 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/30f44fa55e490011162d85db97d6efcd @@ -0,0 +1,491 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/60ac45bf31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/60ac45bf31470011162d85db97d6efcd new file mode 100644 index 0000000..ff09d3e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/60ac45bf31470011162d85db97d6efcd @@ -0,0 +1,185 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd new file mode 100644 index 0000000..e970026 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/b04e3f9d4c490011162d85db97d6efcd @@ -0,0 +1,31 @@ +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/4/f0ae342069460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4/f0ae342069460011162d85db97d6efcd new file mode 100644 index 0000000..4d29975 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4/f0ae342069460011162d85db97d6efcd @@ -0,0 +1,74 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..d0927ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/40/90e6be0e3c4600111c1cd5b6f02d115c @@ -0,0 +1,67 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if () + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/40/a044297a394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/40/a044297a394b0011162d85db97d6efcd new file mode 100644 index 0000000..62ba98d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/40/a044297a394b0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/40f42912ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/40f42912ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..8db202b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/40f42912ed4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package stack; + +public class MyStack { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd new file mode 100644 index 0000000..c556c40 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/903f77fb6a460011162d85db97d6efcd @@ -0,0 +1,101 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd new file mode 100644 index 0000000..7a52b11 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a01333795e490011162d85db97d6efcd @@ -0,0 +1,490 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); + + int odd = 1; + + return n + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c new file mode 100644 index 0000000..8e361f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/a0fae8d0304600111c1cd5b6f02d115c @@ -0,0 +1,15 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0d8226ef04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0d8226ef04a0011162d85db97d6efcd new file mode 100644 index 0000000..26baaf2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/41/e0d8226ef04a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package queue; + +public class MyQueue { + + boolean offer(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd new file mode 100644 index 0000000..485dfc6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/42/e021a6913e490011162d85db97d6efcd @@ -0,0 +1,300 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd new file mode 100644 index 0000000..b6c976c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/1001729a50490011162d85db97d6efcd @@ -0,0 +1,409 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + public LinkedBinaryTree(List ) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd new file mode 100644 index 0000000..39b579c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/c0ba9c305d490011162d85db97d6efcd @@ -0,0 +1,474 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0254a81f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0254a81f14a0011162d85db97d6efcd new file mode 100644 index 0000000..bd52fde --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0254a81f14a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd new file mode 100644 index 0000000..4fed0c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/43/d0bafae776460011162d85db97d6efcd @@ -0,0 +1,147 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd new file mode 100644 index 0000000..f75073a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/3010e77730470011162d85db97d6efcd @@ -0,0 +1,154 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd new file mode 100644 index 0000000..9fd9084 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/a0094e0c68460011162d85db97d6efcd @@ -0,0 +1,52 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent==null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd new file mode 100644 index 0000000..0d89af4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/d0674f197d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/e0acee9633490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/e0acee9633490011162d85db97d6efcd new file mode 100644 index 0000000..ceb0aeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/e0acee9633490011162d85db97d6efcd @@ -0,0 +1,257 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + if + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd new file mode 100644 index 0000000..b7437e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/44/f011dd6380490011162d85db97d6efcd @@ -0,0 +1,44 @@ +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/45/00a4208ff54a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/00a4208ff54a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd new file mode 100644 index 0000000..417e270 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/45/9074d68c42490011162d85db97d6efcd @@ -0,0 +1,361 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c new file mode 100644 index 0000000..6746395 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/45/d0987f61314600111c1cd5b6f02d115c @@ -0,0 +1,23 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd new file mode 100644 index 0000000..fcb3ac2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/45/f08884f241490011162d85db97d6efcd @@ -0,0 +1,344 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd new file mode 100644 index 0000000..8c0556a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/30e0646267460011162d85db97d6efcd @@ -0,0 +1,25 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd new file mode 100644 index 0000000..292612f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/9051198a5c490011162d85db97d6efcd @@ -0,0 +1,459 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd new file mode 100644 index 0000000..6290f1e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/90c9ca2380490011162d85db97d6efcd @@ -0,0 +1,35 @@ +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/46/c0b9549e404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/46/c0b9549e404600111c1cd5b6f02d115c new file mode 100644 index 0000000..1d72ec5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/c0b9549e404600111c1cd5b6f02d115c @@ -0,0 +1,19 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + System.out.println("\nTest VettoreIntero"); + Integer i1 = Integer.valueOf(3); + Integer i2 = Integer.valueOf(389); + Integer i3 = Integer.valueOf(15); + Integer i4 = Integer.valueOf(10); + + VettoriIntero VI = new VettoriIntero(4); + + VI.aggiungi(i1); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/46/d0099f1ff74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/46/d0099f1ff74a0011162d85db97d6efcd new file mode 100644 index 0000000..243c47e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/46/d0099f1ff74a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + TreeSet + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd new file mode 100644 index 0000000..b9a17dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/006e9fb77b490011162d85db97d6efcd @@ -0,0 +1,37 @@ +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 new file mode 100644 index 0000000..0981778 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/70cdcd744d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/47/e0aca0e534470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/47/e0aca0e534470011162d85db97d6efcd new file mode 100644 index 0000000..c97e1ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/47/e0aca0e534470011162d85db97d6efcd @@ -0,0 +1,222 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + queueOfNodes.add(node); + + while() { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/00636a36bd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/00636a36bd4b0011162d85db97d6efcd new file mode 100644 index 0000000..d3f6242 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/00636a36bd4b0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // + if (paziente == null) return false; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/200821d1f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/200821d1f24a0011162d85db97d6efcd new file mode 100644 index 0000000..0ac83b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/200821d1f24a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd new file mode 100644 index 0000000..77df6f7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/40bd1e004d490011162d85db97d6efcd @@ -0,0 +1,38 @@ +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/48/f0d96ef466460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/48/f0d96ef466460011162d85db97d6efcd new file mode 100644 index 0000000..3545952 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/48/f0d96ef466460011162d85db97d6efcd @@ -0,0 +1,10 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd new file mode 100644 index 0000000..5ecba6c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/401ca6de67460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd new file mode 100644 index 0000000..fb9303c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/40eab8a977490011162d85db97d6efcd @@ -0,0 +1,11 @@ +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 new file mode 100644 index 0000000..d2ddbf7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/70b7c91463490011162d85db97d6efcd @@ -0,0 +1,514 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..e7a2989 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/49/f0c531e67c490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/10b261debe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/10b261debe4b0011162d85db97d6efcd new file mode 100644 index 0000000..9ffe1b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/10b261debe4b0011162d85db97d6efcd @@ -0,0 +1,46 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator lit = pazienti.listIterator(); + while (lit.hasNext()) { + Paziente current = lit.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) { + lit.add(paziente); + return true; + } + } + + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd new file mode 100644 index 0000000..10b0213 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b00e859a62490011162d85db97d6efcd @@ -0,0 +1,512 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/4a/b061b799314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b061b799314600111c1cd5b6f02d115c new file mode 100644 index 0000000..aa514cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4a/b061b799314600111c1cd5b6f02d115c @@ -0,0 +1,26 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + + return true; + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd new file mode 100644 index 0000000..a183669 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30407c1938470011162d85db97d6efcd @@ -0,0 +1,235 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30d24ecd394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30d24ecd394b0011162d85db97d6efcd new file mode 100644 index 0000000..4e7c90c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/30d24ecd394b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd new file mode 100644 index 0000000..3bc988a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/40083a7a2d470011162d85db97d6efcd @@ -0,0 +1,109 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd new file mode 100644 index 0000000..a8da0b4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/5017771d63490011162d85db97d6efcd @@ -0,0 +1,516 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/4b/d032b0536a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/d032b0536a460011162d85db97d6efcd new file mode 100644 index 0000000..9ef29e3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4b/d032b0536a460011162d85db97d6efcd @@ -0,0 +1,80 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) return null; + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/00101cf57e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/00101cf57e490011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70465734394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70465734394b0011162d85db97d6efcd new file mode 100644 index 0000000..7d00cba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70465734394b0011162d85db97d6efcd @@ -0,0 +1,29 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd new file mode 100644 index 0000000..9e73805 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/7083ebe876460011162d85db97d6efcd @@ -0,0 +1,147 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + int hRight = (right == null) ? 0 : right.height(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70d8eb6df24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70d8eb6df24a0011162d85db97d6efcd new file mode 100644 index 0000000..e535ae7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/70d8eb6df24a0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.getSize(); + } + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd new file mode 100644 index 0000000..04941da --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4c/d09a61472f470011162d85db97d6efcd @@ -0,0 +1,127 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd new file mode 100644 index 0000000..6dcf239 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4d/10e536e03e490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight()) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd new file mode 100644 index 0000000..5a1e610 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/20e0276b76490011162d85db97d6efcd @@ -0,0 +1,59 @@ +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/4e/40f1c63c2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/40f1c63c2c470011162d85db97d6efcd new file mode 100644 index 0000000..9587897 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/40f1c63c2c470011162d85db97d6efcd @@ -0,0 +1,95 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd new file mode 100644 index 0000000..255d941 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/601cd7aa33490011162d85db97d6efcd @@ -0,0 +1,268 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + if + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4e/f0350baff04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/f0350baff04a0011162d85db97d6efcd new file mode 100644 index 0000000..f231854 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4e/f0350baff04a0011162d85db97d6efcd @@ -0,0 +1,11 @@ +package queue; + +public interface MyQueue { + + /** + * Aggiungi l'elemento specificato in fondo alla coda + * @param item + */ + boolean offer(T item); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/103a7ed4384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/103a7ed4384b0011162d85db97d6efcd new file mode 100644 index 0000000..96c390f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/103a7ed4384b0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50aef4f9384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50aef4f9384b0011162d85db97d6efcd new file mode 100644 index 0000000..e5ad047 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/50aef4f9384b0011162d85db97d6efcd @@ -0,0 +1,19 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd new file mode 100644 index 0000000..f1c2ec3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/609e16c965460011162d85db97d6efcd @@ -0,0 +1,19 @@ +package binary_tree; + +import java.util.Iterator; + +public interface BinaryTree { + + public boolean isEmpty(); + + public int size(); + + public void clear(); + + public E getRoot(); + + public boolean contains(E targetElement); + + public Iterator + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd new file mode 100644 index 0000000..39f7cfd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/a08872953d490011162d85db97d6efcd @@ -0,0 +1,282 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd new file mode 100644 index 0000000..f6fd5c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/4f/d047e3ca75490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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/5/0053573f43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/0053573f43490011162d85db97d6efcd new file mode 100644 index 0000000..ee8ab62 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/0053573f43490011162d85db97d6efcd @@ -0,0 +1,384 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/40524a0dbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/40524a0dbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..b880840 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/40524a0dbe4b0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/7096ec27f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/7096ec27f74a0011162d85db97d6efcd new file mode 100644 index 0000000..e20edd8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/7096ec27f74a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + TreeSet ts = new TreeSet(); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd new file mode 100644 index 0000000..45f4b3f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5/908c6e4e5d490011162d85db97d6efcd @@ -0,0 +1,481 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + + } + + public void numberLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd new file mode 100644 index 0000000..4f06d18 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/301fd41376460011162d85db97d6efcd @@ -0,0 +1,127 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/309094edf74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/309094edf74a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd new file mode 100644 index 0000000..a1e5ef6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/6014e5aa5a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd new file mode 100644 index 0000000..494173c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/7055109f5d490011162d85db97d6efcd @@ -0,0 +1,488 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd new file mode 100644 index 0000000..b51e27d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/50/d0ac635b2f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + temporaryList.add(node.getData()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd new file mode 100644 index 0000000..530be36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/004e91c067460011162d85db97d6efcd @@ -0,0 +1,34 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/0075c65cfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/0075c65cfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..a0c7b2e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/0075c65cfc4a0011162d85db97d6efcd @@ -0,0 +1,32 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd new file mode 100644 index 0000000..a6d574e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/80dfe9df6a460011162d85db97d6efcd @@ -0,0 +1,94 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd new file mode 100644 index 0000000..081dbbb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/9076b8502e470011162d85db97d6efcd @@ -0,0 +1,117 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORI + + /* + * Qui si utilizza il metodo del preorder + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd new file mode 100644 index 0000000..c2ca817 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/51/d037f2ec59490011162d85db97d6efcd @@ -0,0 +1,434 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + + + currentNode.setData(currentObject); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd new file mode 100644 index 0000000..dc2df44 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/90cb57c368460011162d85db97d6efcd @@ -0,0 +1,64 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd new file mode 100644 index 0000000..a07ccfe --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/a055924468460011162d85db97d6efcd @@ -0,0 +1,56 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + if (parent != null && parent.getLeft() == this) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd new file mode 100644 index 0000000..5b75c38 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/b08162f142490011162d85db97d6efcd @@ -0,0 +1,374 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + + } else { + // il nodo non è ancora da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd new file mode 100644 index 0000000..9b037a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/52/e05461a15e490011162d85db97d6efcd @@ -0,0 +1,491 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/20bbb9a5404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/53/20bbb9a5404600111c1cd5b6f02d115c new file mode 100644 index 0000000..be6de4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/53/20bbb9a5404600111c1cd5b6f02d115c @@ -0,0 +1,22 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + System.out.println("\nTest VettoreIntero"); + Integer i1 = Integer.valueOf(3); + Integer i2 = Integer.valueOf(389); + Integer i3 = Integer.valueOf(15); + Integer i4 = Integer.valueOf(10); + + VettoriIntero VI = new VettoriIntero(4); + + VI.aggiungi(i1); + VI.aggiungi(i2); + VI.aggiungi(i3); + VI.aggiungi(i4); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd new file mode 100644 index 0000000..86e3f74 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/53/50f0698377490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd new file mode 100644 index 0000000..7bd3163 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/53/c05369904c490011162d85db97d6efcd @@ -0,0 +1,31 @@ +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/53/f000f8983f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/53/f000f8983f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..73f9fc2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/53/f000f8983f4600111c1cd5b6f02d115c @@ -0,0 +1,50 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd new file mode 100644 index 0000000..8795bb3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/0076262b68460011162d85db97d6efcd @@ -0,0 +1,56 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd new file mode 100644 index 0000000..15b6bed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/2006ffc076460011162d85db97d6efcd @@ -0,0 +1,145 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd new file mode 100644 index 0000000..5a01ac7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/20cba89730470011162d85db97d6efcd @@ -0,0 +1,159 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd new file mode 100644 index 0000000..27fde3a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/909c1ce260490011162d85db97d6efcd @@ -0,0 +1,509 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/54/90cdcfa6f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/90cdcfa6f74a0011162d85db97d6efcd new file mode 100644 index 0000000..b9bae3e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/90cdcfa6f74a0011162d85db97d6efcd @@ -0,0 +1,36 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + if (ts.isEmpty()) + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/54/f0471982394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/54/f0471982394b0011162d85db97d6efcd new file mode 100644 index 0000000..6856950 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/54/f0471982394b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public in compareTo() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c new file mode 100644 index 0000000..32d6042 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/55/204b0787314600111c1cd5b6f02d115c @@ -0,0 +1,23 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd new file mode 100644 index 0000000..1678a21 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/55/300820856b460011162d85db97d6efcd @@ -0,0 +1,110 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left == null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/55/5092a369f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/55/5092a369f04a0011162d85db97d6efcd new file mode 100644 index 0000000..ba56c8a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/55/5092a369f04a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package queue; + +public class MyQueue { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd new file mode 100644 index 0000000..e35c0db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/3099355b85460011162d85db97d6efcd @@ -0,0 +1,72 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd new file mode 100644 index 0000000..90946ba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/8046d3d032470011162d85db97d6efcd @@ -0,0 +1,191 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd new file mode 100644 index 0000000..43763ed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/a019c8b976460011162d85db97d6efcd @@ -0,0 +1,144 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/56/f0b9e44efd4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/56/f0b9e44efd4a0011162d85db97d6efcd new file mode 100644 index 0000000..a4198cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/56/f0b9e44efd4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package jcf_set.example; + +public class TreeSetExample3 { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd new file mode 100644 index 0000000..551c4d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/10b14d217f490011162d85db97d6efcd @@ -0,0 +1,14 @@ +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/20e10b4768460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/20e10b4768460011162d85db97d6efcd new file mode 100644 index 0000000..0f53cce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/20e10b4768460011162d85db97d6efcd @@ -0,0 +1,56 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + if (parent != null && parent.getLeft() == this) return true; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd new file mode 100644 index 0000000..87ec9dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/30825eb267460011162d85db97d6efcd @@ -0,0 +1,33 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd new file mode 100644 index 0000000..81c71c2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/70f9172d60490011162d85db97d6efcd @@ -0,0 +1,501 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/57/903e9a17be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/903e9a17be4b0011162d85db97d6efcd new file mode 100644 index 0000000..eac4b72 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/903e9a17be4b0011162d85db97d6efcd @@ -0,0 +1,35 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/a08606c3ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/a08606c3ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..ad982eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/a08606c3ef4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/57/b0b4f215f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/57/b0b4f215f04a0011162d85db97d6efcd new file mode 100644 index 0000000..0ceaa72 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/57/b0b4f215f04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i > 0; i++) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd new file mode 100644 index 0000000..9e5fe25 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/58/00221bbc42490011162d85db97d6efcd @@ -0,0 +1,366 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd new file mode 100644 index 0000000..5302347 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/58/d0f2ac5984460011162d85db97d6efcd @@ -0,0 +1,64 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c new file mode 100644 index 0000000..3c233ea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/58/e0d0bd89374600111c1cd5b6f02d115c @@ -0,0 +1,36 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd new file mode 100644 index 0000000..cbc7431 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/20e49d4040490011162d85db97d6efcd @@ -0,0 +1,319 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd new file mode 100644 index 0000000..ccde0e1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/306a49074c490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000..55a14e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/80fb7fa577490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco2 implements Comparator { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/9026ae623b4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/9026ae623b4b0011162d85db97d6efcd new file mode 100644 index 0000000..332e953 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/9026ae623b4b0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package parziale.p251110; + +public class Clinica { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd new file mode 100644 index 0000000..0715ce0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/59/a02ffb9334470011162d85db97d6efcd @@ -0,0 +1,214 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00cef804be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00cef804be4b0011162d85db97d6efcd new file mode 100644 index 0000000..3221cf4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/00cef804be4b0011162d85db97d6efcd @@ -0,0 +1,32 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd new file mode 100644 index 0000000..559f1b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/201abb532c470011162d85db97d6efcd @@ -0,0 +1,96 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd new file mode 100644 index 0000000..085dd13 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/30fe91715f490011162d85db97d6efcd @@ -0,0 +1,493 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/40ecac465d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/40ecac465d490011162d85db97d6efcd new file mode 100644 index 0000000..4d4657b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/40ecac465d490011162d85db97d6efcd @@ -0,0 +1,481 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + + } + + public void printLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd new file mode 100644 index 0000000..5c4b7d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/c0c92f247c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/5a/d0656c8b59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d0656c8b59490011162d85db97d6efcd new file mode 100644 index 0000000..3df5e10 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5a/d0656c8b59490011162d85db97d6efcd @@ -0,0 +1,429 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + + while (iterator.hasNext()) { + E current = iterator.next(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd new file mode 100644 index 0000000..8df1313 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/20d12f495e490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd new file mode 100644 index 0000000..63cd6c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/306d34792f470011162d85db97d6efcd @@ -0,0 +1,131 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd new file mode 100644 index 0000000..e77e038 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/50b621fc5d490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(node.getRight(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd new file mode 100644 index 0000000..2a3278e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/a0652eed68460011162d85db97d6efcd @@ -0,0 +1,64 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd new file mode 100644 index 0000000..a5a1c7c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/b0eb910243490011162d85db97d6efcd @@ -0,0 +1,375 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + if () + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd new file mode 100644 index 0000000..d183c4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5b/e0db09db4a490011162d85db97d6efcd @@ -0,0 +1,14 @@ +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/00f6201ded4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/00f6201ded4a0011162d85db97d6efcd new file mode 100644 index 0000000..0abd015 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/00f6201ded4a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package stack; + +public interface MyStack { + + void push(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/1012ac97f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/1012ac97f74a0011162d85db97d6efcd new file mode 100644 index 0000000..478a035 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/1012ac97f74a0011162d85db97d6efcd @@ -0,0 +1,35 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..bb5b641 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/201b06733f4600111c1cd5b6f02d115c @@ -0,0 +1,40 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd new file mode 100644 index 0000000..084cb7d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/205b16fa4b490011162d85db97d6efcd @@ -0,0 +1,29 @@ +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/3017176541490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/3017176541490011162d85db97d6efcd new file mode 100644 index 0000000..3860907 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/3017176541490011162d85db97d6efcd @@ -0,0 +1,341 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd new file mode 100644 index 0000000..5e3f3b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/60546b564d490011162d85db97d6efcd @@ -0,0 +1,49 @@ +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/803c58916b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/803c58916b460011162d85db97d6efcd new file mode 100644 index 0000000..49cafe6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/803c58916b460011162d85db97d6efcd @@ -0,0 +1,114 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd new file mode 100644 index 0000000..841a595 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/b04b230342490011162d85db97d6efcd @@ -0,0 +1,344 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd new file mode 100644 index 0000000..49ea5c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/c05d13224d490011162d85db97d6efcd @@ -0,0 +1,43 @@ +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/5c/f099b30242490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/f099b30242490011162d85db97d6efcd new file mode 100644 index 0000000..fcb3ac2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5c/f099b30242490011162d85db97d6efcd @@ -0,0 +1,344 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd new file mode 100644 index 0000000..52f7d8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/20e4ce207f490011162d85db97d6efcd @@ -0,0 +1,9 @@ +package jcf_set.example; + +public class InteriRipetuti { + + public static void main(String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..16a8fbc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/40d1b9f83e4600111c1cd5b6f02d115c @@ -0,0 +1,22 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd new file mode 100644 index 0000000..400991e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/a00a1b492f470011162d85db97d6efcd @@ -0,0 +1,128 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd new file mode 100644 index 0000000..15b1c17 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5d/f0b3635e80490011162d85db97d6efcd @@ -0,0 +1,42 @@ +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 new file mode 100644 index 0000000..5b1ccd7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/30c39e5478490011162d85db97d6efcd @@ -0,0 +1,19 @@ +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/5e/60004df250490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/60004df250490011162d85db97d6efcd new file mode 100644 index 0000000..9fe5847 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/60004df250490011162d85db97d6efcd @@ -0,0 +1,412 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * + */ + public LinkedBinaryTree(List objectList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd new file mode 100644 index 0000000..e410e4d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/707621f285460011162d85db97d6efcd @@ -0,0 +1,82 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd new file mode 100644 index 0000000..98b82d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/70bf5dd042490011162d85db97d6efcd @@ -0,0 +1,371 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd new file mode 100644 index 0000000..6ee8cdc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/a0c16d3a32490011162d85db97d6efcd @@ -0,0 +1,245 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd new file mode 100644 index 0000000..8bdc749 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5e/d068cda06a460011162d85db97d6efcd @@ -0,0 +1,88 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd new file mode 100644 index 0000000..a6d6b6f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/6070b0f240490011162d85db97d6efcd @@ -0,0 +1,332 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd new file mode 100644 index 0000000..f6aadca --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/5f/80a5218542490011162d85db97d6efcd @@ -0,0 +1,360 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..d3155e5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/008f04793c4600111c1cd5b6f02d115c @@ -0,0 +1,77 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if () + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/5008b04dee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6/5008b04dee4a0011162d85db97d6efcd new file mode 100644 index 0000000..c029442 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/5008b04dee4a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.get(array.size() - 1); + } + + public int size() { + return array.size(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd new file mode 100644 index 0000000..3fa0ab6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/80cd5fac5a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode; = new BinaryNode(); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6/a0f29528f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6/a0f29528f34a0011162d85db97d6efcd new file mode 100644 index 0000000..6e86212 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6/a0f29528f34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd new file mode 100644 index 0000000..033b17a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/40bc6eca42490011162d85db97d6efcd @@ -0,0 +1,371 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd new file mode 100644 index 0000000..6ac7cbd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/50acadf659490011162d85db97d6efcd @@ -0,0 +1,437 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd new file mode 100644 index 0000000..dd43fe6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/8000b4c567460011162d85db97d6efcd @@ -0,0 +1,36 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd new file mode 100644 index 0000000..f57c5b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/e043dd365e490011162d85db97d6efcd @@ -0,0 +1,486 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.hasLeft() == null) ? 0 : + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd new file mode 100644 index 0000000..0f30ba5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/60/f0e5988d5d490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..ed43b66 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/307fe6873f4600111c1cd5b6f02d115c @@ -0,0 +1,46 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd new file mode 100644 index 0000000..534320c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/6043fa2a7d490011162d85db97d6efcd @@ -0,0 +1,54 @@ +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/61/a08bf1565e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/61/a08bf1565e490011162d85db97d6efcd new file mode 100644 index 0000000..e4dfa97 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/a08bf1565e490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.hasRight() == null) ? 0 : numberLeaf(node.getRight()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..4ab30b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/61/d03e4efa3c4600111c1cd5b6f02d115c @@ -0,0 +1,79 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + Object temp = vettore[minimo]; + vettore[minimo] = vettore[i]; + vettore[i] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd new file mode 100644 index 0000000..a4a31c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/109f384477460011162d85db97d6efcd @@ -0,0 +1,149 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + int hRight = (right == null) ? 0 : right.height(); + + return Math.max(hLeft, hRight) + 1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..6f877fb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/60a5473f3f4600111c1cd5b6f02d115c @@ -0,0 +1,31 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd new file mode 100644 index 0000000..a1c332d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70882dbe5c490011162d85db97d6efcd @@ -0,0 +1,465 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd new file mode 100644 index 0000000..7a2255e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/70d7a0222e470011162d85db97d6efcd @@ -0,0 +1,113 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORI + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd new file mode 100644 index 0000000..ec342bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/c015439530470011162d85db97d6efcd @@ -0,0 +1,156 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/62/e0856c60394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/62/e0856c60394b0011162d85db97d6efcd new file mode 100644 index 0000000..7474748 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/62/e0856c60394b0011162d85db97d6efcd @@ -0,0 +1,32 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd new file mode 100644 index 0000000..b536a2c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/c0ab895c6a460011162d85db97d6efcd @@ -0,0 +1,81 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) return null; + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd new file mode 100644 index 0000000..fa254aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/d094954042490011162d85db97d6efcd @@ -0,0 +1,354 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd new file mode 100644 index 0000000..21d53cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/63/e050cca14c490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/402663f7f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/402663f7f14a0011162d85db97d6efcd new file mode 100644 index 0000000..a014f00 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/402663f7f14a0011162d85db97d6efcd @@ -0,0 +1,15 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd new file mode 100644 index 0000000..4d671dc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/4061172868460011162d85db97d6efcd @@ -0,0 +1,56 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd new file mode 100644 index 0000000..a268874 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/64/a0def40a4f490011162d85db97d6efcd @@ -0,0 +1,62 @@ +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 new file mode 100644 index 0000000..ac499e3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/10c1a53477490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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/65/20e6a367be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/20e6a367be4b0011162d85db97d6efcd new file mode 100644 index 0000000..fc384eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/20e6a367be4b0011162d85db97d6efcd @@ -0,0 +1,40 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd new file mode 100644 index 0000000..058058f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/40b6f28330470011162d85db97d6efcd @@ -0,0 +1,154 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/7055b6f3f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/7055b6f3f24a0011162d85db97d6efcd new file mode 100644 index 0000000..e543084 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/7055b6f3f24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd new file mode 100644 index 0000000..403d4fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/c08427a130470011162d85db97d6efcd @@ -0,0 +1,159 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/e09e6b30ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/e09e6b30ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..8d79181 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/e09e6b30ed4a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package stack; + +public interface MyStack { + + void push(T item); + + T pop(); + + T peek(); + + int size(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd new file mode 100644 index 0000000..8c0d061 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/65/f0387a6b41490011162d85db97d6efcd @@ -0,0 +1,342 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/102741eefb4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/102741eefb4a0011162d85db97d6efcd new file mode 100644 index 0000000..4492b2c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/66/102741eefb4a0011162d85db97d6efcd @@ -0,0 +1,13 @@ +package jcf_set.example; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd new file mode 100644 index 0000000..0f91771 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/66/104863334d490011162d85db97d6efcd @@ -0,0 +1,46 @@ +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 new file mode 100644 index 0000000..39849e7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/66/30ab513061490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/66/5030cdf92e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/66/5030cdf92e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..4e0fed7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/66/5030cdf92e4600111c1cd5b6f02d115c @@ -0,0 +1,8 @@ +/** + * + */ +/** + * + */ +module asdl { +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd new file mode 100644 index 0000000..d0bcfcb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/305327802c470011162d85db97d6efcd @@ -0,0 +1,97 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd new file mode 100644 index 0000000..f0ab9d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/805ea56f61490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/67/9056832dee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/9056832dee4a0011162d85db97d6efcd new file mode 100644 index 0000000..ab9cd02 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/9056832dee4a0011162d85db97d6efcd @@ -0,0 +1,16 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/e040f67f394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/e040f67f394b0011162d85db97d6efcd new file mode 100644 index 0000000..bd398dc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/e040f67f394b0011162d85db97d6efcd @@ -0,0 +1,37 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd new file mode 100644 index 0000000..dcf2552 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0533aae67460011162d85db97d6efcd @@ -0,0 +1,33 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f07be87af74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f07be87af74a0011162d85db97d6efcd new file mode 100644 index 0000000..9167332 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f07be87af74a0011162d85db97d6efcd @@ -0,0 +1,33 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet"); + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd new file mode 100644 index 0000000..605e846 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/67/f0eadc1981460011162d85db97d6efcd @@ -0,0 +1,38 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd new file mode 100644 index 0000000..35e7c47 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/68/0045007978490011162d85db97d6efcd @@ -0,0 +1,20 @@ +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/68/607bdda481460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/607bdda481460011162d85db97d6efcd new file mode 100644 index 0000000..cecca25 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/68/607bdda481460011162d85db97d6efcd @@ -0,0 +1,41 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd new file mode 100644 index 0000000..69b01f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/68/80fddd7f42490011162d85db97d6efcd @@ -0,0 +1,360 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/68/d081e45bee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/68/d081e45bee4a0011162d85db97d6efcd new file mode 100644 index 0000000..ce19c9b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/68/d081e45bee4a0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } + + @Override + public int isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/80e1f2c0f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/80e1f2c0f24a0011162d85db97d6efcd new file mode 100644 index 0000000..3c9a3dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/80e1f2c0f24a0011162d85db97d6efcd @@ -0,0 +1,16 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/904f298def4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/904f298def4a0011162d85db97d6efcd new file mode 100644 index 0000000..ff61e24 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/904f298def4a0011162d85db97d6efcd @@ -0,0 +1,13 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = "a"; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd new file mode 100644 index 0000000..cd8cfd2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b034e4bb5c490011162d85db97d6efcd @@ -0,0 +1,465 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd new file mode 100644 index 0000000..e77e864 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/b0ff378178490011162d85db97d6efcd @@ -0,0 +1,21 @@ +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/69/c09f8a6b59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/69/c09f8a6b59490011162d85db97d6efcd new file mode 100644 index 0000000..04237c8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/69/c09f8a6b59490011162d85db97d6efcd @@ -0,0 +1,423 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerExeption(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd new file mode 100644 index 0000000..763cecd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/30b3802076490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/404291a93b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/404291a93b4600111c1cd5b6f02d115c new file mode 100644 index 0000000..6ad52cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/404291a93b4600111c1cd5b6f02d115c @@ -0,0 +1,62 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd new file mode 100644 index 0000000..aeee988 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/40fb16d460490011162d85db97d6efcd @@ -0,0 +1,506 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/6a/6017fcb975460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/6017fcb975460011162d85db97d6efcd new file mode 100644 index 0000000..fd4bcc7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/6017fcb975460011162d85db97d6efcd @@ -0,0 +1,125 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.equals) || hasLeft() != otherType.hasLeft() || harRight() != otherType.hasRight()) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd new file mode 100644 index 0000000..5edebd0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/70aa679e3e490011162d85db97d6efcd @@ -0,0 +1,303 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd new file mode 100644 index 0000000..9cfcf1e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/8006081a69460011162d85db97d6efcd @@ -0,0 +1,73 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd new file mode 100644 index 0000000..552c108 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6a/f0b866862f470011162d85db97d6efcd @@ -0,0 +1,131 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd new file mode 100644 index 0000000..c8a7436 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/202041e130470011162d85db97d6efcd @@ -0,0 +1,166 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/70787486fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/70787486fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..87d9a09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/70787486fc4a0011162d85db97d6efcd @@ -0,0 +1,37 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd new file mode 100644 index 0000000..0354789 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/e08ea0f94b490011162d85db97d6efcd @@ -0,0 +1,29 @@ +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/6b/f044e4ff4f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/f044e4ff4f490011162d85db97d6efcd new file mode 100644 index 0000000..1deb801 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6b/f044e4ff4f490011162d85db97d6efcd @@ -0,0 +1,407 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd new file mode 100644 index 0000000..f571c20 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6c/e08a4fe085460011162d85db97d6efcd @@ -0,0 +1,79 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd new file mode 100644 index 0000000..3c88f8c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/40d91aaa6a460011162d85db97d6efcd @@ -0,0 +1,90 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd new file mode 100644 index 0000000..fe5b568 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/d0764a0c3f490011162d85db97d6efcd @@ -0,0 +1,308 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd new file mode 100644 index 0000000..fab6862 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6e/f002016436470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd new file mode 100644 index 0000000..f257a41 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/60faf8b460490011162d85db97d6efcd @@ -0,0 +1,504 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..f0ab9d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/6f/a0a27c0761490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/7/a02a5e32ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7/a02a5e32ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..59015af --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7/a02a5e32ee4a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/108fa51b394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/108fa51b394b0011162d85db97d6efcd new file mode 100644 index 0000000..cbd29e5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/108fa51b394b0011162d85db97d6efcd @@ -0,0 +1,28 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd new file mode 100644 index 0000000..6b24d05 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/60aed3d277490011162d85db97d6efcd @@ -0,0 +1,16 @@ +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/70/c034a8002d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/70/c034a8002d470011162d85db97d6efcd new file mode 100644 index 0000000..9e6be2c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/70/c034a8002d470011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd new file mode 100644 index 0000000..a88b058 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/71/70864afd7f490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/70f75f0ff04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/70f75f0ff04a0011162d85db97d6efcd new file mode 100644 index 0000000..a3bc1de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/71/70f75f0ff04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = 0; i < stack.size(); i++) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/804bff5dee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/804bff5dee4a0011162d85db97d6efcd new file mode 100644 index 0000000..a192ff5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/71/804bff5dee4a0011162d85db97d6efcd @@ -0,0 +1,33 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } + + @Override + public int isEmpty() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd new file mode 100644 index 0000000..f80a5ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/71/e0a180047b490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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/30bc84b83f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30bc84b83f490011162d85db97d6efcd new file mode 100644 index 0000000..2c91499 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30bc84b83f490011162d85db97d6efcd @@ -0,0 +1,311 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd new file mode 100644 index 0000000..de704f0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/30e8bfad35470011162d85db97d6efcd @@ -0,0 +1,226 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd new file mode 100644 index 0000000..846e17f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/60234d1380490011162d85db97d6efcd @@ -0,0 +1,34 @@ +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/602cb7cf67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/602cb7cf67460011162d85db97d6efcd new file mode 100644 index 0000000..eea068f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/602cb7cf67460011162d85db97d6efcd @@ -0,0 +1,36 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd new file mode 100644 index 0000000..1af4861 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/70cc29e77a490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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/72/70e3f700f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/72/70e3f700f34a0011162d85db97d6efcd new file mode 100644 index 0000000..43dfa50 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/72/70e3f700f34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(queue.get(i)); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/73/b015ffb9384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/73/b015ffb9384b0011162d85db97d6efcd new file mode 100644 index 0000000..1a73fad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/73/b015ffb9384b0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package parziale.p251110; + +public class Paziente { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd new file mode 100644 index 0000000..d3c5815 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/304022b735470011162d85db97d6efcd @@ -0,0 +1,227 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd new file mode 100644 index 0000000..8f98dd2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/4011737859490011162d85db97d6efcd @@ -0,0 +1,423 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/600a9e613e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/74/600a9e613e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd new file mode 100644 index 0000000..9e3c2e8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/74/d03a010e7c490011162d85db97d6efcd @@ -0,0 +1,66 @@ +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/75/405922ed3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/405922ed3d490011162d85db97d6efcd new file mode 100644 index 0000000..4d7c175 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/405922ed3d490011162d85db97d6efcd @@ -0,0 +1,282 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/70205a12f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/70205a12f74a0011162d85db97d6efcd new file mode 100644 index 0000000..296b45b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/70205a12f74a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd new file mode 100644 index 0000000..4e200b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/75/b056aedb80460011162d85db97d6efcd @@ -0,0 +1,31 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/b027d1d0424600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/76/b027d1d0424600111c1cd5b6f02d115c new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd new file mode 100644 index 0000000..1c62be9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/76/f05ffd725d490011162d85db97d6efcd @@ -0,0 +1,482 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd new file mode 100644 index 0000000..b97d3e1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/003d6b9e77490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco2 { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd new file mode 100644 index 0000000..53063a9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/30ac586d5d490011162d85db97d6efcd @@ -0,0 +1,482 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd new file mode 100644 index 0000000..c4e46ba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/60f2f1c17b490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/77/8031d2d73f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/8031d2d73f490011162d85db97d6efcd new file mode 100644 index 0000000..5894709 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/8031d2d73f490011162d85db97d6efcd @@ -0,0 +1,314 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd new file mode 100644 index 0000000..614a2a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/a08d42473e490011162d85db97d6efcd @@ -0,0 +1,291 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd new file mode 100644 index 0000000..5b85e8d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/b07a2b7767460011162d85db97d6efcd @@ -0,0 +1,27 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + this.left = left; + this.right = right; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd new file mode 100644 index 0000000..e04fb83 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/77/d09dfc6776460011162d85db97d6efcd @@ -0,0 +1,137 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/30d103dd394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/30d103dd394b0011162d85db97d6efcd new file mode 100644 index 0000000..22f7c68 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/30d103dd394b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + return String.compare(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/78/b0df2fdd384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/78/b0df2fdd384b0011162d85db97d6efcd new file mode 100644 index 0000000..1769053 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/78/b0df2fdd384b0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente(String id, int annoNascita) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd new file mode 100644 index 0000000..eaf53b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/400d4ff333490011162d85db97d6efcd @@ -0,0 +1,271 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd new file mode 100644 index 0000000..40f0a7a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/406738f531470011162d85db97d6efcd @@ -0,0 +1,190 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd new file mode 100644 index 0000000..15376c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/8059808d6b460011162d85db97d6efcd @@ -0,0 +1,110 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd new file mode 100644 index 0000000..bf4695b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/d0de88a65d490011162d85db97d6efcd @@ -0,0 +1,488 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(node.getRight(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd new file mode 100644 index 0000000..897c3c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/79/e001ee783e490011162d85db97d6efcd @@ -0,0 +1,299 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd new file mode 100644 index 0000000..da62ad6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/306fed7160490011162d85db97d6efcd @@ -0,0 +1,503 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/7a/50622c69f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/50622c69f74a0011162d85db97d6efcd new file mode 100644 index 0000000..66412f0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/50622c69f74a0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd new file mode 100644 index 0000000..3dfcbcd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/a0edc6ee31470011162d85db97d6efcd @@ -0,0 +1,190 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, ); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7a/b0016969ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/b0016969ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..5e9b639 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7a/b0016969ed4a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param item + */ + void push(T item); + + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/00fa36de394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/00fa36de394b0011162d85db97d6efcd new file mode 100644 index 0000000..b48dbde --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/00fa36de394b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + return id.compareTo(p); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd new file mode 100644 index 0000000..bd4e8db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7b/a044075a80460011162d85db97d6efcd @@ -0,0 +1,26 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd new file mode 100644 index 0000000..a3d0ccf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7c/d002ffc682460011162d85db97d6efcd @@ -0,0 +1,58 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd new file mode 100644 index 0000000..c4a2ae7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/602356d640490011162d85db97d6efcd @@ -0,0 +1,328 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flag.push(); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd new file mode 100644 index 0000000..d683f58 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/d0187f7a33470011162d85db97d6efcd @@ -0,0 +1,212 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd new file mode 100644 index 0000000..d8d3586 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7d/f0675fe07b490011162d85db97d6efcd @@ -0,0 +1,66 @@ +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/7e/2030127d3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2030127d3e490011162d85db97d6efcd new file mode 100644 index 0000000..71d924c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/2030127d3e490011162d85db97d6efcd @@ -0,0 +1,299 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>();) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd new file mode 100644 index 0000000..a8a333c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/a093cda230470011162d85db97d6efcd @@ -0,0 +1,161 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd new file mode 100644 index 0000000..6b655af --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7e/b057d02285460011162d85db97d6efcd @@ -0,0 +1,69 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/00350724ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/00350724ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..97a71af --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/00350724ee4a0011162d85db97d6efcd @@ -0,0 +1,16 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd new file mode 100644 index 0000000..bd4d256 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/4031c61b69460011162d85db97d6efcd @@ -0,0 +1,73 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd new file mode 100644 index 0000000..8c4d4aa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/601ec91b51490011162d85db97d6efcd @@ -0,0 +1,414 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * + */ + public LinkedBinaryTree(List objectList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd new file mode 100644 index 0000000..a236240 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/80811bb576460011162d85db97d6efcd @@ -0,0 +1,144 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd new file mode 100644 index 0000000..88f4208 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/c02658a840490011162d85db97d6efcd @@ -0,0 +1,324 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(true); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd new file mode 100644 index 0000000..ed40103 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/7f/d00d2c7f2d470011162d85db97d6efcd @@ -0,0 +1,111 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd new file mode 100644 index 0000000..83a4933 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8/2044863a5d490011162d85db97d6efcd @@ -0,0 +1,477 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8/7067f024394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8/7067f024394b0011162d85db97d6efcd new file mode 100644 index 0000000..f9c90d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8/7067f024394b0011162d85db97d6efcd @@ -0,0 +1,28 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c new file mode 100644 index 0000000..ce4ff1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/b0078850314600111c1cd5b6f02d115c @@ -0,0 +1,18 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd new file mode 100644 index 0000000..733a2ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/c040020767460011162d85db97d6efcd @@ -0,0 +1,14 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd new file mode 100644 index 0000000..6f3c3f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/80/d0a8189e7a490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class Main { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd new file mode 100644 index 0000000..d5cb216 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/208f964238470011162d85db97d6efcd @@ -0,0 +1,239 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd new file mode 100644 index 0000000..edd12d6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/5038b84141490011162d85db97d6efcd @@ -0,0 +1,336 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd new file mode 100644 index 0000000..b2677cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/9093a27430470011162d85db97d6efcd @@ -0,0 +1,154 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd new file mode 100644 index 0000000..cd961fc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/a0de638e7f490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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/b06be446394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/b06be446394b0011162d85db97d6efcd new file mode 100644 index 0000000..0ad9597 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/b06be446394b0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c new file mode 100644 index 0000000..fc5f690 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/c0e49baf304600111c1cd5b6f02d115c @@ -0,0 +1,9 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/d0e1e622f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/d0e1e622f74a0011162d85db97d6efcd new file mode 100644 index 0000000..09ffdaf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/d0e1e622f74a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + TreeSet ts = new TreeSet(); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd new file mode 100644 index 0000000..00129cf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/81/f0a7db2863490011162d85db97d6efcd @@ -0,0 +1,516 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/82/20365eeb3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/82/20365eeb3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..13d3f37 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/20365eeb3f4600111c1cd5b6f02d115c @@ -0,0 +1,55 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + + if (i1 < i2) return -1; + if (i1 > i2) return 1; + return 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/d01ad87bf14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/d01ad87bf14a0011162d85db97d6efcd new file mode 100644 index 0000000..b0fa00d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/d01ad87bf14a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package queue; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd new file mode 100644 index 0000000..8795bb3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/82/f080e74168460011162d85db97d6efcd @@ -0,0 +1,56 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd new file mode 100644 index 0000000..c487f8b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/1003f3343d490011162d85db97d6efcd @@ -0,0 +1,274 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd new file mode 100644 index 0000000..2104af7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/102226a57f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/30b24fb66a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/30b24fb66a460011162d85db97d6efcd new file mode 100644 index 0000000..e09d78d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/30b24fb66a460011162d85db97d6efcd @@ -0,0 +1,92 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/506b04443b4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/506b04443b4b0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd new file mode 100644 index 0000000..ef0c3d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/509aac9078490011162d85db97d6efcd @@ -0,0 +1,21 @@ +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/604cd337be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/604cd337be4b0011162d85db97d6efcd new file mode 100644 index 0000000..fb0ecac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/604cd337be4b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd new file mode 100644 index 0000000..3c1c09f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/80f9dad840490011162d85db97d6efcd @@ -0,0 +1,328 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flag.push(true); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd new file mode 100644 index 0000000..bebc5a0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/83/b0282bd675490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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/00f6fe732f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/00f6fe732f470011162d85db97d6efcd new file mode 100644 index 0000000..9d5220b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/00f6fe732f470011162d85db97d6efcd @@ -0,0 +1,130 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd new file mode 100644 index 0000000..7b1b391 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/10240f5f5d490011162d85db97d6efcd @@ -0,0 +1,482 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root); + } + + public void numberLeaf(BinaryNode node) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/20b6b322f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/20b6b322f24a0011162d85db97d6efcd new file mode 100644 index 0000000..8ef2332 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/20b6b322f24a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd new file mode 100644 index 0000000..b438fcb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/401ed55643490011162d85db97d6efcd @@ -0,0 +1,390 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd new file mode 100644 index 0000000..77ea04d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/70e3fa6f5f490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..4bdb994 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/d01430a67b490011162d85db97d6efcd @@ -0,0 +1,35 @@ +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/84/d02d775afc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/84/d02d775afc4a0011162d85db97d6efcd new file mode 100644 index 0000000..d71dda8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/84/d02d775afc4a0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd new file mode 100644 index 0000000..299722c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/6055b2e57f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/703fd7f265460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/703fd7f265460011162d85db97d6efcd new file mode 100644 index 0000000..b0cf2dd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/703fd7f265460011162d85db97d6efcd @@ -0,0 +1,10 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd new file mode 100644 index 0000000..54e6973 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/80d69c7930470011162d85db97d6efcd @@ -0,0 +1,154 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd new file mode 100644 index 0000000..ffdeb86 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/904d3a6659490011162d85db97d6efcd @@ -0,0 +1,423 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd new file mode 100644 index 0000000..5bfdaf1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/85/b07fb9694d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/20d7ee5a41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/20d7ee5a41490011162d85db97d6efcd new file mode 100644 index 0000000..d8dcac9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/20d7ee5a41490011162d85db97d6efcd @@ -0,0 +1,340 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd new file mode 100644 index 0000000..d1f6390 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50917a4a84460011162d85db97d6efcd @@ -0,0 +1,60 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd new file mode 100644 index 0000000..870d842 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/50bf7b7e4b490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..af72712 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/7012d43578490011162d85db97d6efcd @@ -0,0 +1,18 @@ +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/86/80935480f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/80935480f74a0011162d85db97d6efcd new file mode 100644 index 0000000..4fd6d1e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/80935480f74a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd new file mode 100644 index 0000000..e90522d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/90b9fc6f2d470011162d85db97d6efcd @@ -0,0 +1,107 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd new file mode 100644 index 0000000..2dd43c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/86/c08d5f4281460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + public boolean isEmpty() { + return root == null; + } + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..01bc290 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/87/504f06ad3c4600111c1cd5b6f02d115c @@ -0,0 +1,79 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + int temp = vettore[minimo]; + vettore[minimo] = vettore[j]; + vettore[j] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd new file mode 100644 index 0000000..9f5e400 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/87/602625d530470011162d85db97d6efcd @@ -0,0 +1,164 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd new file mode 100644 index 0000000..4c0e00d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/87/904a8d643d490011162d85db97d6efcd @@ -0,0 +1,280 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator iteratorInOrder() { + + } + + // ITERATOR + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd new file mode 100644 index 0000000..af06c47 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/87/908ea3bd75490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..c8443bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/40313a667f490011162d85db97d6efcd @@ -0,0 +1,22 @@ +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/5045f61f38470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/5045f61f38470011162d85db97d6efcd new file mode 100644 index 0000000..b9b586f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/5045f61f38470011162d85db97d6efcd @@ -0,0 +1,239 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator(){ + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/603dce17ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/603dce17ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..1054005 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/603dce17ee4a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd new file mode 100644 index 0000000..572bd88 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/88/906b9d3b80490011162d85db97d6efcd @@ -0,0 +1,37 @@ +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/508509b03e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/508509b03e490011162d85db97d6efcd new file mode 100644 index 0000000..7251773 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/508509b03e490011162d85db97d6efcd @@ -0,0 +1,305 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd new file mode 100644 index 0000000..1abfd4e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/89/80797a724b490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..b0a9a13 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/20df8efa4e490011162d85db97d6efcd @@ -0,0 +1,61 @@ +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/309bb85a5d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/309bb85a5d490011162d85db97d6efcd new file mode 100644 index 0000000..0f14d71 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/309bb85a5d490011162d85db97d6efcd @@ -0,0 +1,481 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + numberLeaf(root); + } + + public void numberLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/50cd25024c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/50cd25024c490011162d85db97d6efcd new file mode 100644 index 0000000..122d4de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/50cd25024c490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd new file mode 100644 index 0000000..bdf02e8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8a/90ba854d5a490011162d85db97d6efcd @@ -0,0 +1,443 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd new file mode 100644 index 0000000..1910fbd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/8036a58375490011162d85db97d6efcd @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..a4b991d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/902b548560490011162d85db97d6efcd @@ -0,0 +1,504 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..006cd6f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/d08ae6af7c490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/f0036ed559490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0036ed559490011162d85db97d6efcd new file mode 100644 index 0000000..f45c81b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0036ed559490011162d85db97d6efcd @@ -0,0 +1,433 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + + currentNode.setData(currentObject); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd new file mode 100644 index 0000000..ad52513 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8b/f0077d9c7f490011162d85db97d6efcd @@ -0,0 +1,28 @@ +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 new file mode 100644 index 0000000..df67d42 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/30c326fe5f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/8c/60ea247043490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60ea247043490011162d85db97d6efcd new file mode 100644 index 0000000..51d96a6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/60ea247043490011162d85db97d6efcd @@ -0,0 +1,392 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/709ced35be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/709ced35be4b0011162d85db97d6efcd new file mode 100644 index 0000000..dd2507c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/709ced35be4b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/80c95c4bee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/80c95c4bee4a0011162d85db97d6efcd new file mode 100644 index 0000000..159e492 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/80c95c4bee4a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.get(array.size() - 1); + } + + public int size() { + return array.size(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd new file mode 100644 index 0000000..b80efcd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8c/a050684776460011162d85db97d6efcd @@ -0,0 +1,132 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd new file mode 100644 index 0000000..0010ebc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/204922d15c490011162d85db97d6efcd @@ -0,0 +1,466 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd new file mode 100644 index 0000000..050ee6c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6064b86d68460011162d85db97d6efcd @@ -0,0 +1,58 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + if (parent != null && parent.getLeft() == this) return true; + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd new file mode 100644 index 0000000..32ac800 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/6068a86a30470011162d85db97d6efcd @@ -0,0 +1,150 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd new file mode 100644 index 0000000..07345d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/8028541534490011162d85db97d6efcd @@ -0,0 +1,272 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/808d46a3fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/808d46a3fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..df892f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/808d46a3fc4a0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd new file mode 100644 index 0000000..5dd854a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c015358f68460011162d85db97d6efcd @@ -0,0 +1,58 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd new file mode 100644 index 0000000..811b6f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/c0b488d773460011162d85db97d6efcd @@ -0,0 +1,121 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd new file mode 100644 index 0000000..d2fdb70 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8d/e08c3c447b490011162d85db97d6efcd @@ -0,0 +1,32 @@ +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/8e/20287a7731470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/20287a7731470011162d85db97d6efcd new file mode 100644 index 0000000..87264f7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/20287a7731470011162d85db97d6efcd @@ -0,0 +1,176 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/30e78f1fbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/30e78f1fbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..4f39276 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/30e78f1fbe4b0011162d85db97d6efcd @@ -0,0 +1,35 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd new file mode 100644 index 0000000..1359e59 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/40dd24e33e490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd new file mode 100644 index 0000000..de34a65 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/a01c69eb31470011162d85db97d6efcd @@ -0,0 +1,189 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList arrayList = new ArrayList(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8e/f0fdb4653b4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/f0fdb4653b4b0011162d85db97d6efcd new file mode 100644 index 0000000..5710f5d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8e/f0fdb4653b4b0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package parziale.p251110; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd new file mode 100644 index 0000000..1cd7900 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/001666176a460011162d85db97d6efcd @@ -0,0 +1,78 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/2013c0aabe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/2013c0aabe4b0011162d85db97d6efcd new file mode 100644 index 0000000..d9801e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/2013c0aabe4b0011162d85db97d6efcd @@ -0,0 +1,43 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.listIterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) { + it.add(); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/209996d4ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/209996d4ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..337745f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/209996d4ed4a0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * Ritorna il numero di elementi correnti nello stack. + * @return Numero di elementi nello stack. + */ + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd new file mode 100644 index 0000000..dff7e76 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/30b7a4e87b490011162d85db97d6efcd @@ -0,0 +1,66 @@ +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/8f/e065521c41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/e065521c41490011162d85db97d6efcd new file mode 100644 index 0000000..470e278 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/8f/e065521c41490011162d85db97d6efcd @@ -0,0 +1,333 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd new file mode 100644 index 0000000..5d3a61b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/0074cee833490011162d85db97d6efcd @@ -0,0 +1,270 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/20a16061fd4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/20a16061fd4a0011162d85db97d6efcd new file mode 100644 index 0000000..a41815f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/20a16061fd4a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package jcf_set.example; + +public class TreeSetExample3 { + +} + +class Studente { + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd new file mode 100644 index 0000000..009f378 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/402bd10e4b490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/70d5f0b8f54a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/70d5f0b8f54a0011162d85db97d6efcd new file mode 100644 index 0000000..bb30c2b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/70d5f0b8f54a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare() { + + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd new file mode 100644 index 0000000..5fe09db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/80709bd965460011162d85db97d6efcd @@ -0,0 +1,5 @@ +package binary_tree; + +public class LinkedBinaryTree { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/a0efec74394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/a0efec74394b0011162d85db97d6efcd new file mode 100644 index 0000000..6280a6f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/a0efec74394b0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package parziale.p251110; + +public class Paziente implements { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd new file mode 100644 index 0000000..6537153 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9/e08523e55e490011162d85db97d6efcd @@ -0,0 +1,492 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/90/3065ab7235470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/3065ab7235470011162d85db97d6efcd new file mode 100644 index 0000000..2d33cc2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/3065ab7235470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) templist.add(current.getData); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd new file mode 100644 index 0000000..ff863b2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/506ea2b582460011162d85db97d6efcd @@ -0,0 +1,57 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : node.getLeft().getSize(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd new file mode 100644 index 0000000..ccaf664 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/9001896c67460011162d85db97d6efcd @@ -0,0 +1,26 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + this.left = left; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd new file mode 100644 index 0000000..7e80b87 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/b00a82fc2c470011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd new file mode 100644 index 0000000..976ecbc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/90/e0e5fd013f490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) queueOfNodes.push(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd new file mode 100644 index 0000000..e2b2777 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/1052254876490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/109e105cf24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/109e105cf24a0011162d85db97d6efcd new file mode 100644 index 0000000..44a15fe --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/109e105cf24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd new file mode 100644 index 0000000..4b7e1ed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/30cef6bd81460011162d85db97d6efcd @@ -0,0 +1,45 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd new file mode 100644 index 0000000..7083081 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/401d4a2434490011162d85db97d6efcd @@ -0,0 +1,273 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/602bc4adbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/602bc4adbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..88cca2d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/602bc4adbe4b0011162d85db97d6efcd @@ -0,0 +1,43 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.listIterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) { + it.add(paziente); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd new file mode 100644 index 0000000..19087d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/91/603a0fd07b490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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 new file mode 100644 index 0000000..6359ed8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/92/007aafd960490011162d85db97d6efcd @@ -0,0 +1,508 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/00a5a3503c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/92/00a5a3503c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..de1e3e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/92/00a5a3503c4600111c1cd5b6f02d115c @@ -0,0 +1,77 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if () + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd new file mode 100644 index 0000000..2fbb42c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/92/309fa0967b490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/92/60a3241932490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/92/60a3241932490011162d85db97d6efcd new file mode 100644 index 0000000..4b10be1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/92/60a3241932490011162d85db97d6efcd @@ -0,0 +1,242 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c new file mode 100644 index 0000000..1da0e36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/105b76ae314600111c1cd5b6f02d115c @@ -0,0 +1,27 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + + dimensioneCorrente++; + return true; + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd new file mode 100644 index 0000000..4d70205 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30a18ad73f490011162d85db97d6efcd @@ -0,0 +1,314 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd new file mode 100644 index 0000000..b8f22bf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/30d507c175460011162d85db97d6efcd @@ -0,0 +1,126 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || harRight() != otherType.hasRight()) return false; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f5ed02ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f5ed02ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..6e157e0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/93/d0f5ed02ed4a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package stack; + +public class ArrayListStack { + + private ArrayList array = new ArrayList(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd new file mode 100644 index 0000000..6cdd316 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/10f11e9b6a460011162d85db97d6efcd @@ -0,0 +1,88 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..c3c45d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/b00dea3c3c4600111c1cd5b6f02d115c @@ -0,0 +1,71 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if () + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c new file mode 100644 index 0000000..c08d0a3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/94/f0f2ddce314600111c1cd5b6f02d115c @@ -0,0 +1,32 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd new file mode 100644 index 0000000..9ec57df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/10935ed373460011162d85db97d6efcd @@ -0,0 +1,120 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd new file mode 100644 index 0000000..7a38370 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/50e19e643e490011162d85db97d6efcd @@ -0,0 +1,299 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryLisr) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd new file mode 100644 index 0000000..338599e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/601a39ea2c470011162d85db97d6efcd @@ -0,0 +1,102 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd new file mode 100644 index 0000000..872a265 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/c00ab97d7b490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/95/d0aa950c35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0aa950c35470011162d85db97d6efcd new file mode 100644 index 0000000..3a8a43b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/95/d0aa950c35470011162d85db97d6efcd @@ -0,0 +1,222 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + queueOfNodes.add(node); + + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..8d721a2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/6004b92d3f4600111c1cd5b6f02d115c @@ -0,0 +1,31 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd new file mode 100644 index 0000000..85777ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/70bb02e532490011162d85db97d6efcd @@ -0,0 +1,251 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + if + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c new file mode 100644 index 0000000..50da30d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/e00f21c1304600111c1cd5b6f02d115c @@ -0,0 +1,10 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd new file mode 100644 index 0000000..a184175 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/96/f0bcb9b259490011162d85db97d6efcd @@ -0,0 +1,430 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + + while (iterator.hasNext()) { + E current = iterator.next(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd new file mode 100644 index 0000000..3b5bb3e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/004de8e976490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco1 { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/404b7abff24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/404b7abff24a0011162d85db97d6efcd new file mode 100644 index 0000000..540f1bb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/404b7abff24a0011162d85db97d6efcd @@ -0,0 +1,11 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd new file mode 100644 index 0000000..d1b36c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/a0779db233490011162d85db97d6efcd @@ -0,0 +1,270 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd new file mode 100644 index 0000000..4844aaa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/b01cdcc881460011162d85db97d6efcd @@ -0,0 +1,46 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd new file mode 100644 index 0000000..3c3e5c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c14f7b4d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/97/d0c607e4394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c607e4394b0011162d85db97d6efcd new file mode 100644 index 0000000..dfab0b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/d0c607e4394b0011162d85db97d6efcd @@ -0,0 +1,43 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + return id.compareTo(p.id); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd new file mode 100644 index 0000000..1c4f7b2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/97/f050f7e65d490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(node.getRight(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd new file mode 100644 index 0000000..7a7fbe9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/204d69a530470011162d85db97d6efcd @@ -0,0 +1,162 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + + return + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd new file mode 100644 index 0000000..eece3e5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/40f6441b7d490011162d85db97d6efcd @@ -0,0 +1,54 @@ +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/98/70a425c7f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/70a425c7f24a0011162d85db97d6efcd new file mode 100644 index 0000000..507eb30 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/70a425c7f24a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd new file mode 100644 index 0000000..9a48820 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/98/906ebb2130470011162d85db97d6efcd @@ -0,0 +1,140 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/5000d856f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/5000d856f74a0011162d85db97d6efcd new file mode 100644 index 0000000..ffe0700 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/5000d856f74a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd new file mode 100644 index 0000000..2ab2156 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/50a39d0276490011162d85db97d6efcd @@ -0,0 +1,31 @@ +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/99/a048f929f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/a048f929f24a0011162d85db97d6efcd new file mode 100644 index 0000000..9160de5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/a048f929f24a0011162d85db97d6efcd @@ -0,0 +1,23 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd new file mode 100644 index 0000000..ba3c5bc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/99/b02246cc81460011162d85db97d6efcd @@ -0,0 +1,50 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd new file mode 100644 index 0000000..28e65df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/201388db77490011162d85db97d6efcd @@ -0,0 +1,16 @@ +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/9a/3059e0133f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/3059e0133f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..9797ba0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/3059e0133f4600111c1cd5b6f02d115c @@ -0,0 +1,28 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo intero. + */ + public + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/50386da7f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/50386da7f24a0011162d85db97d6efcd new file mode 100644 index 0000000..4f7c620 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/50386da7f24a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c new file mode 100644 index 0000000..834bd98 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/80cb6e5d314600111c1cd5b6f02d115c @@ -0,0 +1,23 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object oggetto) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0fdda1aed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0fdda1aed4a0011162d85db97d6efcd new file mode 100644 index 0000000..61c581f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/a0fdda1aed4a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package stack; + +public interface MyStack { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9a/d02dbcabf74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/d02dbcabf74a0011162d85db97d6efcd new file mode 100644 index 0000000..976d55d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9a/d02dbcabf74a0011162d85db97d6efcd @@ -0,0 +1,36 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + if (ts.isEmpty()) System.out.println(); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd new file mode 100644 index 0000000..fdb2a7f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/109551477b490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/9b/8024daf35a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/8024daf35a490011162d85db97d6efcd new file mode 100644 index 0000000..21f7152 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9b/8024daf35a490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + newNode.setParent(currentNode); + + if (wing == 0) { + // Left Wing + currentNode. + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0b6057aef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0b6057aef4a0011162d85db97d6efcd new file mode 100644 index 0000000..d83d907 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0b6057aef4a0011162d85db97d6efcd @@ -0,0 +1,11 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new MyStack(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd new file mode 100644 index 0000000..7e80b87 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/b0e585eb2c470011162d85db97d6efcd @@ -0,0 +1,103 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9c/e0161cfef64a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/e0161cfef64a0011162d85db97d6efcd new file mode 100644 index 0000000..135f982 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9c/e0161cfef64a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..0b8301f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/004b92023c4600111c1cd5b6f02d115c @@ -0,0 +1,69 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if () + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2) { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd new file mode 100644 index 0000000..19886a4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/00a9a52a33470011162d85db97d6efcd @@ -0,0 +1,205 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9d/10ef3bca66460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9d/10ef3bca66460011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd new file mode 100644 index 0000000..1a4e161 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/106bfbff7e490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package jcf_set.example; + +public class InteriRipetuti { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9e/8046aad2be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/8046aad2be4b0011162d85db97d6efcd new file mode 100644 index 0000000..b89cdab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9e/8046aad2be4b0011162d85db97d6efcd @@ -0,0 +1,46 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.listIterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) { + it.add(paziente); + return true; + } + } + + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd new file mode 100644 index 0000000..e20c637 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9f/a03d9a6e41490011162d85db97d6efcd @@ -0,0 +1,342 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/9f/b09ac0e6384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/9f/b09ac0e6384b0011162d85db97d6efcd new file mode 100644 index 0000000..a2b519b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/9f/b09ac0e6384b0011162d85db97d6efcd @@ -0,0 +1,14 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd new file mode 100644 index 0000000..a01789c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/405cad2f5e490011162d85db97d6efcd @@ -0,0 +1,486 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/70095722ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/70095722ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..f57c780 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/70095722ed4a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package stack; + +public interface MyStack { + + void push(T item); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/809065c6394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/809065c6394b0011162d85db97d6efcd new file mode 100644 index 0000000..8a4d40d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/809065c6394b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd new file mode 100644 index 0000000..3c5aaf4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/b0fa8b836b460011162d85db97d6efcd @@ -0,0 +1,110 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd new file mode 100644 index 0000000..d22ccff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a/e01254345c490011162d85db97d6efcd @@ -0,0 +1,450 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd new file mode 100644 index 0000000..0edd29d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/20039e585e490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.hasRight() == null) ? 0 : numberLeaf(node.getRight()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd new file mode 100644 index 0000000..8f6d995 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/80cda19b5b490011162d85db97d6efcd @@ -0,0 +1,446 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd new file mode 100644 index 0000000..b3de34a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a0/a06c94983e490011162d85db97d6efcd @@ -0,0 +1,303 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(current); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd new file mode 100644 index 0000000..a301c8c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/0005d07276490011162d85db97d6efcd @@ -0,0 +1,61 @@ +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/40a27dfd3b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/40a27dfd3b4600111c1cd5b6f02d115c new file mode 100644 index 0000000..8ee0b4b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/40a27dfd3b4600111c1cd5b6f02d115c @@ -0,0 +1,65 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if (vettore[minimo] > vettore[j]) minimo = j; + } + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd new file mode 100644 index 0000000..941e8d7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/6023ce1c32490011162d85db97d6efcd @@ -0,0 +1,245 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void preorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/d0cd0482f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/d0cd0482f24a0011162d85db97d6efcd new file mode 100644 index 0000000..6e8c4bb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/d0cd0482f24a0011162d85db97d6efcd @@ -0,0 +1,32 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.size(); + } + + @Override + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd new file mode 100644 index 0000000..bb5a4ba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f01e02445a490011162d85db97d6efcd @@ -0,0 +1,441 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + E currentObject + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd new file mode 100644 index 0000000..9179555 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a1/f0cafd0f78490011162d85db97d6efcd @@ -0,0 +1,12 @@ +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/a2/0022814ff24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/0022814ff24a0011162d85db97d6efcd new file mode 100644 index 0000000..05833c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/0022814ff24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd new file mode 100644 index 0000000..b5a7a0c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/60b567d142490011162d85db97d6efcd @@ -0,0 +1,373 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + } else { + // il nodo non è ancora da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9076c359ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9076c359ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..ffee6ba --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9076c359ee4a0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } + + @Override +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd new file mode 100644 index 0000000..8c662cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a2/9085d8fa3e490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) queueOfNodes.add(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd new file mode 100644 index 0000000..5d785c6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/20c867805c490011162d85db97d6efcd @@ -0,0 +1,457 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd new file mode 100644 index 0000000..caf134a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/40499cd540490011162d85db97d6efcd @@ -0,0 +1,327 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd new file mode 100644 index 0000000..e1a40b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/6073d5943f490011162d85db97d6efcd @@ -0,0 +1,310 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd new file mode 100644 index 0000000..f0ab9d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/9056884262490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/a3/c04b9a8ded4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/c04b9a8ded4a0011162d85db97d6efcd new file mode 100644 index 0000000..e606b2d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/c04b9a8ded4a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd new file mode 100644 index 0000000..d9b9577 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a3/d08010f173460011162d85db97d6efcd @@ -0,0 +1,121 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/000f1b05ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/000f1b05ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..2b5f9f3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/000f1b05ed4a0011162d85db97d6efcd @@ -0,0 +1,8 @@ +package stack; + +public class ArrayListStack { + + private ArrayList array = new ArrayList(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd new file mode 100644 index 0000000..35d2910 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/501420ef59490011162d85db97d6efcd @@ -0,0 +1,434 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd new file mode 100644 index 0000000..5979f1a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/a01aada535470011162d85db97d6efcd @@ -0,0 +1,225 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd new file mode 100644 index 0000000..737a562 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a4/d0ee7f743d490011162d85db97d6efcd @@ -0,0 +1,280 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + } + + // ITERATOR + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd new file mode 100644 index 0000000..708efbf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/102120144f490011162d85db97d6efcd @@ -0,0 +1,398 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd new file mode 100644 index 0000000..b8e170c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/504ccd2a43490011162d85db97d6efcd @@ -0,0 +1,383 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd new file mode 100644 index 0000000..3810cd0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/70f40a5180490011162d85db97d6efcd @@ -0,0 +1,41 @@ +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/a5/d0540f3b5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/d0540f3b5e490011162d85db97d6efcd new file mode 100644 index 0000000..61bcc33 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/d0540f3b5e490011162d85db97d6efcd @@ -0,0 +1,486 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(node.getLeft()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd new file mode 100644 index 0000000..890a21f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a5/f0c825f459490011162d85db97d6efcd @@ -0,0 +1,435 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd new file mode 100644 index 0000000..764dd69 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/205543404d490011162d85db97d6efcd @@ -0,0 +1,48 @@ +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/a6/2096ffbf374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/2096ffbf374600111c1cd5b6f02d115c new file mode 100644 index 0000000..c99e1a0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/2096ffbf374600111c1cd5b6f02d115c @@ -0,0 +1,40 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[dimensioneCorrente]; + } else return null; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd new file mode 100644 index 0000000..95fd9b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/e0c3cd033f490011162d85db97d6efcd @@ -0,0 +1,307 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd new file mode 100644 index 0000000..904412b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a6/f0821ecc30470011162d85db97d6efcd @@ -0,0 +1,164 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd new file mode 100644 index 0000000..0072c98 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/70133e1277490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco1 implements Comparator { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd new file mode 100644 index 0000000..84b3e4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a7/c0e72fcb31470011162d85db97d6efcd @@ -0,0 +1,189 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd new file mode 100644 index 0000000..395fc2e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/3021b38c5a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd new file mode 100644 index 0000000..f63453b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/306c514e2f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd new file mode 100644 index 0000000..a1e5ef6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/505d9cba5a490011162d85db97d6efcd @@ -0,0 +1,444 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd new file mode 100644 index 0000000..cf4a1d0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a8/d02469f259490011162d85db97d6efcd @@ -0,0 +1,435 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/004f7a1eee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/004f7a1eee4a0011162d85db97d6efcd new file mode 100644 index 0000000..b25e3ca --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/004f7a1eee4a0011162d85db97d6efcd @@ -0,0 +1,16 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd new file mode 100644 index 0000000..50b7705 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/a9/f0f8b5b76a460011162d85db97d6efcd @@ -0,0 +1,94 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldrRight; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/10ee591df24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/10ee591df24a0011162d85db97d6efcd new file mode 100644 index 0000000..809457a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/10ee591df24a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd new file mode 100644 index 0000000..10de7fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/40d151256b460011162d85db97d6efcd @@ -0,0 +1,105 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + + + return oldParent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/508bd546fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/508bd546fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..1b2e115 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/508bd546fc4a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd new file mode 100644 index 0000000..0f703d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/a0238d6080490011162d85db97d6efcd @@ -0,0 +1,44 @@ +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/aa/b0ba55f56a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/b0ba55f56a460011162d85db97d6efcd new file mode 100644 index 0000000..5ebc451 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/b0ba55f56a460011162d85db97d6efcd @@ -0,0 +1,98 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd new file mode 100644 index 0000000..77581a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/c0f74d4032490011162d85db97d6efcd @@ -0,0 +1,245 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder() { + Queue> queueOfNodes = new LinkedList>(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd new file mode 100644 index 0000000..efec2cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/aa/e0b98c806a460011162d85db97d6efcd @@ -0,0 +1,82 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + return oldLeft; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..2e264ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/4061129f3c4600111c1cd5b6f02d115c @@ -0,0 +1,79 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (int j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + int temp = vettore[minimo]; + vettore[minimo] = vettore[j]; + vett + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/60ee3bc64a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/60ee3bc64a490011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd new file mode 100644 index 0000000..907b057 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/9040ba755e490011162d85db97d6efcd @@ -0,0 +1,490 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); + + if () + + return n + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/c043d3aef54a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/c043d3aef54a0011162d85db97d6efcd new file mode 100644 index 0000000..bc5aa65 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/c043d3aef54a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package jcf_set.example; + +public class TreeSetExample1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/e0e3ffedf14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/e0e3ffedf14a0011162d85db97d6efcd new file mode 100644 index 0000000..21af1b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/e0e3ffedf14a0011162d85db97d6efcd @@ -0,0 +1,15 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd new file mode 100644 index 0000000..79573ad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ab/f09ec7bf73460011162d85db97d6efcd @@ -0,0 +1,119 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd new file mode 100644 index 0000000..1a71674 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/10ec5f3331470011162d85db97d6efcd @@ -0,0 +1,168 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/20bce5d5f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/20bce5d5f24a0011162d85db97d6efcd new file mode 100644 index 0000000..adbb243 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/20bce5d5f24a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd new file mode 100644 index 0000000..d16933d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/701902fc75460011162d85db97d6efcd @@ -0,0 +1,126 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd new file mode 100644 index 0000000..d3c1ca3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/c0b8e39f62490011162d85db97d6efcd @@ -0,0 +1,513 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/ac/d011a72a314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d011a72a314600111c1cd5b6f02d115c new file mode 100644 index 0000000..0c1be37 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ac/d011a72a314600111c1cd5b6f02d115c @@ -0,0 +1,15 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..1c371ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/10570af73f4600111c1cd5b6f02d115c @@ -0,0 +1,81 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + this.dimensioneCorrente = 0; + this.vettore = new Object[dimensioneMassima]; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + Object temp = vettore[minimo]; + vettore[minimo] = vettore[i]; + vettore[i] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd new file mode 100644 index 0000000..8655999 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/20b1b4175d490011162d85db97d6efcd @@ -0,0 +1,469 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd new file mode 100644 index 0000000..7bf5745 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/90c3fd0136470011162d85db97d6efcd @@ -0,0 +1,231 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder(){ + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd new file mode 100644 index 0000000..f8c0212 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/a0a1b8d12b470011162d85db97d6efcd @@ -0,0 +1,92 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e30a8e8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ad/d065d9013d4600111c1cd5b6f02d115c @@ -0,0 +1,80 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + this.vettore = new Object[dimensioneMassima]; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + Object temp = vettore[minimo]; + vettore[minimo] = vettore[i]; + vettore[i] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd new file mode 100644 index 0000000..69ace53 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/3062d10a7d490011162d85db97d6efcd @@ -0,0 +1,44 @@ +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/ae/9078c40968460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/9078c40968460011162d85db97d6efcd new file mode 100644 index 0000000..e82c75b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/9078c40968460011162d85db97d6efcd @@ -0,0 +1,48 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd new file mode 100644 index 0000000..8a7ca24 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ae/e0541f4d6b460011162d85db97d6efcd @@ -0,0 +1,105 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if () + + return oldParent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd new file mode 100644 index 0000000..a43f8d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/20c1761533490011162d85db97d6efcd @@ -0,0 +1,252 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + if + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/a005db6fef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/a005db6fef4a0011162d85db97d6efcd new file mode 100644 index 0000000..e51641e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/a005db6fef4a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0bdb053bd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0bdb053bd4b0011162d85db97d6efcd new file mode 100644 index 0000000..59d17a6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0bdb053bd4b0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..38237d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/b0c0e5362f4600111c1cd5b6f02d115c @@ -0,0 +1,7 @@ +package test; + +public class Main { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/c0c1045cf34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/c0c1045cf34a0011162d85db97d6efcd new file mode 100644 index 0000000..6e86212 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/c0c1045cf34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0d725b2be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0d725b2be4b0011162d85db97d6efcd new file mode 100644 index 0000000..3360865 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0d725b2be4b0011162d85db97d6efcd @@ -0,0 +1,44 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.listIterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) { + it.add(paziente); + return true; + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd new file mode 100644 index 0000000..858ec19 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/d0f560f45c490011162d85db97d6efcd @@ -0,0 +1,469 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd new file mode 100644 index 0000000..91ee426 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/af/f08afe2a42490011162d85db97d6efcd @@ -0,0 +1,352 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/309678f1bd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/309678f1bd4b0011162d85db97d6efcd new file mode 100644 index 0000000..86d9b5c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/309678f1bd4b0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/40010629ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/40010629ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..dc40467 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/40010629ed4a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package stack; + +public interface MyStack { + + void push(T item); + + T pop(); + + T peek(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd new file mode 100644 index 0000000..2420342 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/4057991677490011162d85db97d6efcd @@ -0,0 +1,13 @@ +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/b/50b5671567460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/50b5671567460011162d85db97d6efcd new file mode 100644 index 0000000..7ffc9d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/50b5671567460011162d85db97d6efcd @@ -0,0 +1,20 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORE + + public BinaryNode(E data) { + + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/60339e89f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/60339e89f74a0011162d85db97d6efcd new file mode 100644 index 0000000..a67cae8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/60339e89f74a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/60f2489bbc4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/60f2489bbc4b0011162d85db97d6efcd new file mode 100644 index 0000000..4f77049 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/60f2489bbc4b0011162d85db97d6efcd @@ -0,0 +1,23 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + if (paziente == null) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd new file mode 100644 index 0000000..ffd7ea0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/90ce6b235d490011162d85db97d6efcd @@ -0,0 +1,473 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd new file mode 100644 index 0000000..079aab5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b/a0df51aa5b490011162d85db97d6efcd @@ -0,0 +1,446 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd new file mode 100644 index 0000000..5a45c57 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/00e2d1c242490011162d85db97d6efcd @@ -0,0 +1,369 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd new file mode 100644 index 0000000..d7f9b1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/60ab100b4c490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/b0/e0b08c9931470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0b08c9931470011162d85db97d6efcd new file mode 100644 index 0000000..ac5aaf7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b0/e0b08c9931470011162d85db97d6efcd @@ -0,0 +1,180 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd new file mode 100644 index 0000000..8a69756 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/1030922b2c470011162d85db97d6efcd @@ -0,0 +1,94 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd new file mode 100644 index 0000000..d1b9629 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/30f7f1cf82460011162d85db97d6efcd @@ -0,0 +1,60 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd new file mode 100644 index 0000000..1ddf445 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/708b2b285b490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + //newNode.setParent(currentNode); + + if (wing == 0) { + // Left Wing + newNode.setParentAsLeftChild(); + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/a01e84e1384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/a01e84e1384b0011162d85db97d6efcd new file mode 100644 index 0000000..57a90ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/a01e84e1384b0011162d85db97d6efcd @@ -0,0 +1,14 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd new file mode 100644 index 0000000..0626b36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c09bfc7c7f490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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/b1/c0ae0fee35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c0ae0fee35470011162d85db97d6efcd new file mode 100644 index 0000000..083d5d2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/c0ae0fee35470011162d85db97d6efcd @@ -0,0 +1,227 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c new file mode 100644 index 0000000..d4af5c7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b1/f00cf77d404600111c1cd5b6f02d115c @@ -0,0 +1,9 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd new file mode 100644 index 0000000..983f259 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20d7be8d31470011162d85db97d6efcd @@ -0,0 +1,180 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd new file mode 100644 index 0000000..26c1513 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/20ffaf3f76460011162d85db97d6efcd @@ -0,0 +1,132 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd new file mode 100644 index 0000000..fa16ccc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/306384d376460011162d85db97d6efcd @@ -0,0 +1,146 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd new file mode 100644 index 0000000..0236371 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b2/e02b9c497c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/b3/2011fa8959490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/2011fa8959490011162d85db97d6efcd new file mode 100644 index 0000000..4a0ac48 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/2011fa8959490011162d85db97d6efcd @@ -0,0 +1,429 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + + while (iterator.hasNext()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd new file mode 100644 index 0000000..bd40471 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/907284b773460011162d85db97d6efcd @@ -0,0 +1,116 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b3/f0e8b183ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/f0e8b183ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..08cc47f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b3/f0e8b183ef4a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd new file mode 100644 index 0000000..2d9f19c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/10e34d1f77490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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/b4/40c6b52d43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/40c6b52d43490011162d85db97d6efcd new file mode 100644 index 0000000..22d1ec7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/40c6b52d43490011162d85db97d6efcd @@ -0,0 +1,383 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd new file mode 100644 index 0000000..44b2203 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b4/70331b5f67460011162d85db97d6efcd @@ -0,0 +1,21 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORE + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/804f8ed2bd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/804f8ed2bd4b0011162d85db97d6efcd new file mode 100644 index 0000000..159448d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/804f8ed2bd4b0011162d85db97d6efcd @@ -0,0 +1,31 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd new file mode 100644 index 0000000..ef9d421 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/b0f2484943490011162d85db97d6efcd @@ -0,0 +1,389 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd new file mode 100644 index 0000000..9325297 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b5/c006797e78490011162d85db97d6efcd @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..10fb185 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/108d87427f490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/20ba920e34490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20ba920e34490011162d85db97d6efcd new file mode 100644 index 0000000..528b5eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/20ba920e34490011162d85db97d6efcd @@ -0,0 +1,272 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/306f7df9fb4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/306f7df9fb4a0011162d85db97d6efcd new file mode 100644 index 0000000..3a3896b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/306f7df9fb4a0011162d85db97d6efcd @@ -0,0 +1,16 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd new file mode 100644 index 0000000..6306a24 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/4045d59a67460011162d85db97d6efcd @@ -0,0 +1,28 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + this.left = left; + this.right = right; + this.parent = null; + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd new file mode 100644 index 0000000..393050e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/60f2e7655a490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/b002c04bef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/b002c04bef4a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd new file mode 100644 index 0000000..9413b66 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b6/e04f37e74c490011162d85db97d6efcd @@ -0,0 +1,35 @@ +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/b7/00e150ce59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/00e150ce59490011162d85db97d6efcd new file mode 100644 index 0000000..db43d65 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/00e150ce59490011162d85db97d6efcd @@ -0,0 +1,431 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/1099081efc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/1099081efc4a0011162d85db97d6efcd new file mode 100644 index 0000000..703ed6c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/1099081efc4a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + + tree1.add(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30738755f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30738755f14a0011162d85db97d6efcd new file mode 100644 index 0000000..e5d191e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/30738755f14a0011162d85db97d6efcd @@ -0,0 +1,29 @@ +package queue; + +public interface MyQueue { + + /** + * Aggiungi l'elemento specificato in fondo alla coda + * @param item + */ + boolean offer(T item); + + /** + * Rimuove l'elemento in testa alla coda e lo restituisce + * @return + */ + T remove(); + + /** + * Restituisce l'elemento in cima alla coda senza rimuoverlo. + * @return + */ + T peek(); + + /** + * Restituisce l'elemento il numero di elementi correnti nella coda. + * @return + */ + int size(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd new file mode 100644 index 0000000..2bceb3d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b7/4054c4472f470011162d85db97d6efcd @@ -0,0 +1,127 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd new file mode 100644 index 0000000..a575884 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/2033ec633e490011162d85db97d6efcd @@ -0,0 +1,299 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd new file mode 100644 index 0000000..3de8154 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/40bd229f63490011162d85db97d6efcd @@ -0,0 +1,517 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/806987ccf74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/806987ccf74a0011162d85db97d6efcd new file mode 100644 index 0000000..ac287fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/806987ccf74a0011162d85db97d6efcd @@ -0,0 +1,36 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + if (ts.isEmpty()) System.out.println(""); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd new file mode 100644 index 0000000..2b835ed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/900dc62440490011162d85db97d6efcd @@ -0,0 +1,319 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if () + } else { + // il nodo non è da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd new file mode 100644 index 0000000..4cdb06e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b8/b004ba1580490011162d85db97d6efcd @@ -0,0 +1,35 @@ +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 new file mode 100644 index 0000000..7ba57b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/0031abfa5f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/b9/301798c52f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/301798c52f470011162d85db97d6efcd new file mode 100644 index 0000000..766e667 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/b9/301798c52f470011162d85db97d6efcd @@ -0,0 +1,136 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd new file mode 100644 index 0000000..4a0ac48 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/00b03b8359490011162d85db97d6efcd @@ -0,0 +1,429 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + + while (iterator.hasNext()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd new file mode 100644 index 0000000..11c1e45 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/305cdf5b42490011162d85db97d6efcd @@ -0,0 +1,354 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd new file mode 100644 index 0000000..b81e724 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/503dcaeb7f490011162d85db97d6efcd @@ -0,0 +1,33 @@ +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/ba/e05cec0669460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e05cec0669460011162d85db97d6efcd new file mode 100644 index 0000000..70c5127 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e05cec0669460011162d85db97d6efcd @@ -0,0 +1,69 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e0b884aaef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e0b884aaef4a0011162d85db97d6efcd new file mode 100644 index 0000000..a88e872 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ba/e0b884aaef4a0011162d85db97d6efcd @@ -0,0 +1,17 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character a = 'b'; + Character a = 'c'; + Character a = 'd'; + + stack.push(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/0073a49377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/0073a49377490011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd new file mode 100644 index 0000000..8e4d3ec --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/40f291a467460011162d85db97d6efcd @@ -0,0 +1,33 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left == null) + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd new file mode 100644 index 0000000..c1c20de --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/80b4cbd167460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..3186d0e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/90fe1ea53c4600111c1cd5b6f02d115c @@ -0,0 +1,79 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + int temp = vettore[minimo]; + vettore[minimo] = vettore[j]; + vett + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd new file mode 100644 index 0000000..b4899d4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0302eb131470011162d85db97d6efcd @@ -0,0 +1,184 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd new file mode 100644 index 0000000..d539bfd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/c0ab2dd92f470011162d85db97d6efcd @@ -0,0 +1,139 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd new file mode 100644 index 0000000..6495c29 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/d063a1ea76490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco1 implements Comparator{ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e024ca63ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e024ca63ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..cbf522a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e024ca63ee4a0011162d85db97d6efcd @@ -0,0 +1,33 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } + + @Override + public boolean isEmpty() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd new file mode 100644 index 0000000..1092ac3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bb/e0a49df05f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd new file mode 100644 index 0000000..bd32729 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/109a57395b490011162d85db97d6efcd @@ -0,0 +1,446 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + //newNode.setParent(currentNode); + + if (wing == 0) { + // Left Wing + newNode.setParentAsLeftChild(currentNode); + } else { + // Right Wing + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bc/408905da60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/408905da60490011162d85db97d6efcd new file mode 100644 index 0000000..4e5c324 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/408905da60490011162d85db97d6efcd @@ -0,0 +1,508 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/bc/700f177e5e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/700f177e5e490011162d85db97d6efcd new file mode 100644 index 0000000..54291ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bc/700f177e5e490011162d85db97d6efcd @@ -0,0 +1,492 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); + + int odd; + + + + return n + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd new file mode 100644 index 0000000..dd18429 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/00578b275f490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/bd/40743bd32f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/40743bd32f470011162d85db97d6efcd new file mode 100644 index 0000000..3622f9a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/40743bd32f470011162d85db97d6efcd @@ -0,0 +1,138 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd new file mode 100644 index 0000000..9864268 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/a01434bd59490011162d85db97d6efcd @@ -0,0 +1,430 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c new file mode 100644 index 0000000..83574da --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/c09bfca4374600111c1cd5b6f02d115c @@ -0,0 +1,38 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[dimensioneCorrente]; + } else return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd new file mode 100644 index 0000000..b632132 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f018ccf668460011162d85db97d6efcd @@ -0,0 +1,68 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd new file mode 100644 index 0000000..e6a69c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bd/f0e1f12f82460011162d85db97d6efcd @@ -0,0 +1,53 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd new file mode 100644 index 0000000..50091ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/be/805c67fa36470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd new file mode 100644 index 0000000..ef39891 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/be/9067867476490011162d85db97d6efcd @@ -0,0 +1,62 @@ +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/be/b04ebe5ff04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/be/b04ebe5ff04a0011162d85db97d6efcd new file mode 100644 index 0000000..6f877c9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/be/b04ebe5ff04a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package queue; + +public class MyQueue { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd new file mode 100644 index 0000000..6291744 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/10c8f1c068460011162d85db97d6efcd @@ -0,0 +1,64 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E elem) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/80f11f062f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/80f11f062f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd new file mode 100644 index 0000000..aa9a2db --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d07e3d442c470011162d85db97d6efcd @@ -0,0 +1,96 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd new file mode 100644 index 0000000..a47cdee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/d093a61e38470011162d85db97d6efcd @@ -0,0 +1,237 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd new file mode 100644 index 0000000..575fd54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/e040508c7f490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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 new file mode 100644 index 0000000..832f2fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/bf/f0b022e65f490011162d85db97d6efcd @@ -0,0 +1,500 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/30cb312643490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/30cb312643490011162d85db97d6efcd new file mode 100644 index 0000000..61fd767 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/30cb312643490011162d85db97d6efcd @@ -0,0 +1,380 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd new file mode 100644 index 0000000..abb0216 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c/507a02e25f490011162d85db97d6efcd @@ -0,0 +1,496 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/c0/2039934e3d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/2039934e3d490011162d85db97d6efcd new file mode 100644 index 0000000..a21165f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/2039934e3d490011162d85db97d6efcd @@ -0,0 +1,276 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/3084d806f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/3084d806f24a0011162d85db97d6efcd new file mode 100644 index 0000000..88b72fa --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/3084d806f24a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd new file mode 100644 index 0000000..56b153d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c0/90795f935d490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd new file mode 100644 index 0000000..229767d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/30f532133f490011162d85db97d6efcd @@ -0,0 +1,309 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd new file mode 100644 index 0000000..0c7e93d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/70c507362f470011162d85db97d6efcd @@ -0,0 +1,123 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/b0609970fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/b0609970fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..147123b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/b0609970fc4a0011162d85db97d6efcd @@ -0,0 +1,45 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c1/f014a506ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/f014a506ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..c8d59b9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c1/f014a506ee4a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/2069019aba4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/2069019aba4b0011162d85db97d6efcd new file mode 100644 index 0000000..f012e3f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/2069019aba4b0011162d85db97d6efcd @@ -0,0 +1,17 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/40d6c986be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/40d6c986be4b0011162d85db97d6efcd new file mode 100644 index 0000000..4270bd7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/40d6c986be4b0011162d85db97d6efcd @@ -0,0 +1,40 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd new file mode 100644 index 0000000..d5a1115 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/604e84eb84460011162d85db97d6efcd @@ -0,0 +1,67 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd new file mode 100644 index 0000000..977e3b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/80f86a3a2f470011162d85db97d6efcd @@ -0,0 +1,125 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd new file mode 100644 index 0000000..f291081 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/90e1ca0b76490011162d85db97d6efcd @@ -0,0 +1,37 @@ +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/c2/c01cbcea3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c01cbcea3f490011162d85db97d6efcd new file mode 100644 index 0000000..3bd9963 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c01cbcea3f490011162d85db97d6efcd @@ -0,0 +1,315 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c0fcc85ef14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/c0fcc85ef14a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c2/f0006eeded4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/f0006eeded4a0011162d85db97d6efcd new file mode 100644 index 0000000..c377dde --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c2/f0006eeded4a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack { + + private ArrayList array = new ArrayList(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/b06e7607ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/b06e7607ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..db7d999 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/b06e7607ee4a0011162d85db97d6efcd @@ -0,0 +1,12 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c0107502f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c0107502f74a0011162d85db97d6efcd new file mode 100644 index 0000000..05d1ac9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c0107502f74a0011162d85db97d6efcd @@ -0,0 +1,21 @@ +package jcf_set.example; + +import java.util.Comparator; + +public class TreeSetExample1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd new file mode 100644 index 0000000..86a7a58 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c3/c04425fc68460011162d85db97d6efcd @@ -0,0 +1,68 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd new file mode 100644 index 0000000..17761ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/103da84d62490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/50badc144f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/50badc144f490011162d85db97d6efcd new file mode 100644 index 0000000..45898f5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/50badc144f490011162d85db97d6efcd @@ -0,0 +1,408 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd new file mode 100644 index 0000000..2067b6a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/60ac830650490011162d85db97d6efcd @@ -0,0 +1,409 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + public LinkedBinaryTree() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd new file mode 100644 index 0000000..51014e3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c4/c0caa0cc7f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/c5/301f79bd30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/301f79bd30470011162d85db97d6efcd new file mode 100644 index 0000000..f342625 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c5/301f79bd30470011162d85db97d6efcd @@ -0,0 +1,160 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd new file mode 100644 index 0000000..0afbd8f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c6/b05e6a5f84460011162d85db97d6efcd @@ -0,0 +1,65 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd new file mode 100644 index 0000000..43c013b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/1091642840490011162d85db97d6efcd @@ -0,0 +1,319 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(); + } else { + // il nodo non è da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd new file mode 100644 index 0000000..bb05eff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/50a913d13f490011162d85db97d6efcd @@ -0,0 +1,313 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd new file mode 100644 index 0000000..f947e25 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0a3ec2576490011162d85db97d6efcd @@ -0,0 +1,45 @@ +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 new file mode 100644 index 0000000..57b218d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/b0d47f644b490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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/d086203880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d086203880460011162d85db97d6efcd new file mode 100644 index 0000000..2759159 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d086203880460011162d85db97d6efcd @@ -0,0 +1,23 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd new file mode 100644 index 0000000..dcb628c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c7/d0877e594d490011162d85db97d6efcd @@ -0,0 +1,49 @@ +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/005aed14ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/005aed14ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..0d73e54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/005aed14ed4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package stack; + +public class MyStack { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd new file mode 100644 index 0000000..6893ab8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/10d8ddcf34470011162d85db97d6efcd @@ -0,0 +1,219 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + queueOfNodes(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd new file mode 100644 index 0000000..b17e595 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/20f9929f40490011162d85db97d6efcd @@ -0,0 +1,321 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd new file mode 100644 index 0000000..8d3da70 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/40f828dd7f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/c8/a01cada6f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a01cada6f24a0011162d85db97d6efcd new file mode 100644 index 0000000..73bad01 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a01cada6f24a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package queue; + +public class QueueExample { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd new file mode 100644 index 0000000..a4ead15 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c8/a0c8bb7042490011162d85db97d6efcd @@ -0,0 +1,357 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd new file mode 100644 index 0000000..e65d4d3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/0002c31c41490011162d85db97d6efcd @@ -0,0 +1,336 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd new file mode 100644 index 0000000..ecc412e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/00275a1d43490011162d85db97d6efcd @@ -0,0 +1,378 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + queueOfNodes.push(current); + flags.push(true); + + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c new file mode 100644 index 0000000..9ab69fb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/606c6432314600111c1cd5b6f02d115c @@ -0,0 +1,15 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd new file mode 100644 index 0000000..f910670 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/d0b245d160490011162d85db97d6efcd @@ -0,0 +1,504 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/c9/e0010085f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/e0010085f14a0011162d85db97d6efcd new file mode 100644 index 0000000..557fc44 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/c9/e0010085f14a0011162d85db97d6efcd @@ -0,0 +1,13 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/201b479df24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/201b479df24a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd new file mode 100644 index 0000000..7f3eba9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/20a2e12f2e470011162d85db97d6efcd @@ -0,0 +1,114 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORI + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd new file mode 100644 index 0000000..296cd4b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/501dff395e490011162d85db97d6efcd @@ -0,0 +1,486 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.hasLeft() == null) ? 0 : numberLeaf(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd new file mode 100644 index 0000000..a2e6510 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ca/d06b013d74460011162d85db97d6efcd @@ -0,0 +1,125 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + return this.left.equals() + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..cc4eaa5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/20589ef92e4600111c1cd5b6f02d115c @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd new file mode 100644 index 0000000..b90beb9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cb/c08a279a75490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/107e382585460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/107e382585460011162d85db97d6efcd new file mode 100644 index 0000000..c78ff0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/107e382585460011162d85db97d6efcd @@ -0,0 +1,71 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + + return leftTree; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd new file mode 100644 index 0000000..424f445 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/10f9397d62490011162d85db97d6efcd @@ -0,0 +1,511 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..6798eef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/2063cc0578490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco3 implements Comparator { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd new file mode 100644 index 0000000..91cf0cc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/70abe1d485460011162d85db97d6efcd @@ -0,0 +1,78 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd new file mode 100644 index 0000000..56495be --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/80cdc9637f490011162d85db97d6efcd @@ -0,0 +1,21 @@ +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/c053a3aeed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/c053a3aeed4a0011162d85db97d6efcd new file mode 100644 index 0000000..8c1eb61 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/c053a3aeed4a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return + */ + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd new file mode 100644 index 0000000..e810abb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cc/d0a9170e4c490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/cd/003f4abb35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/003f4abb35470011162d85db97d6efcd new file mode 100644 index 0000000..d317517 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/003f4abb35470011162d85db97d6efcd @@ -0,0 +1,227 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd new file mode 100644 index 0000000..0922e3f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30a9784665460011162d85db97d6efcd @@ -0,0 +1,7 @@ +package binary_tree; + +public class BinaryTree { + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd new file mode 100644 index 0000000..60e4f0a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/30bb0fbd42490011162d85db97d6efcd @@ -0,0 +1,367 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd new file mode 100644 index 0000000..89e74bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/400a286a2f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd new file mode 100644 index 0000000..9e8f141 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f09a3bba81460011162d85db97d6efcd @@ -0,0 +1,43 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0ce62d0f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0ce62d0f74a0011162d85db97d6efcd new file mode 100644 index 0000000..e1ec9be --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0ce62d0f74a0011162d85db97d6efcd @@ -0,0 +1,37 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + if (ts.isEmpty()) System.out.println("TreeSet è vuoto"); + else System.out.println("TreeSet ha [" + ts.size() + "] elementi");ß + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0dd056aed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0dd056aed4a0011162d85db97d6efcd new file mode 100644 index 0000000..be6dd13 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cd/f0dd056aed4a0011162d85db97d6efcd @@ -0,0 +1,18 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack + */ + void push(T item); + + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/1091182cf04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/1091182cf04a0011162d85db97d6efcd new file mode 100644 index 0000000..be6ee45 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/1091182cf04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i > 0; i--) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/304a3c0fed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/304a3c0fed4a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/305b6a10f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/305b6a10f34a0011162d85db97d6efcd new file mode 100644 index 0000000..e4b15d5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/305b6a10f34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 1; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd new file mode 100644 index 0000000..62b6fd0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/801d225243490011162d85db97d6efcd @@ -0,0 +1,389 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getRight()); + } + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/b03af87dfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/b03af87dfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..3f7ed4d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/b03af87dfc4a0011162d85db97d6efcd @@ -0,0 +1,37 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()) + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c new file mode 100644 index 0000000..98771fb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f01c00dd314600111c1cd5b6f02d115c @@ -0,0 +1,33 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f0f69448ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f0f69448ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..75489c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ce/f0f69448ee4a0011162d85db97d6efcd @@ -0,0 +1,24 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.get(array.size() - 1); + } + + public int size() { + return array.size(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd new file mode 100644 index 0000000..a552cd5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/0023d91a36470011162d85db97d6efcd @@ -0,0 +1,231 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd new file mode 100644 index 0000000..3f1ba9f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/301a058880460011162d85db97d6efcd @@ -0,0 +1,27 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd new file mode 100644 index 0000000..eb7e519 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/307e539c43490011162d85db97d6efcd @@ -0,0 +1,396 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c new file mode 100644 index 0000000..485f82b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/cf/40ca51a23b4600111c1cd5b6f02d115c @@ -0,0 +1,60 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd new file mode 100644 index 0000000..c64616d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d/301721ca2e470011162d85db97d6efcd @@ -0,0 +1,121 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORI + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd new file mode 100644 index 0000000..b6184e9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d/50af1af25c490011162d85db97d6efcd @@ -0,0 +1,469 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd new file mode 100644 index 0000000..348b6e7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d/90afdba635470011162d85db97d6efcd @@ -0,0 +1,225 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd new file mode 100644 index 0000000..57b218d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d/a072985e4b490011162d85db97d6efcd @@ -0,0 +1,24 @@ +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 new file mode 100644 index 0000000..219ed21 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d/c0a00c864d490011162d85db97d6efcd @@ -0,0 +1,52 @@ +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/30bfbc8cfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/30bfbc8cfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..079f4d1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/30bfbc8cfc4a0011162d85db97d6efcd @@ -0,0 +1,38 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd new file mode 100644 index 0000000..4aaf1ff --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/40962e082c470011162d85db97d6efcd @@ -0,0 +1,93 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c new file mode 100644 index 0000000..ddd90e0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/608f4b8c314600111c1cd5b6f02d115c @@ -0,0 +1,25 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + + } else return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd new file mode 100644 index 0000000..8db3513 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/70e1a6a181460011162d85db97d6efcd @@ -0,0 +1,41 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd new file mode 100644 index 0000000..a31c38c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/803500214c490011162d85db97d6efcd @@ -0,0 +1,31 @@ +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/d0/f0c814b1ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/f0c814b1ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..66c3c4c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d0/f0c814b1ed4a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd new file mode 100644 index 0000000..6fd5be6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/003c72e95d490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + else { + numberLeaf(node.getLeft(), number); + numberLeaf(node.getRight(), number); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/00fca95ff34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/00fca95ff34a0011162d85db97d6efcd new file mode 100644 index 0000000..1247643 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/00fca95ff34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = 0; i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd new file mode 100644 index 0000000..fbd4536 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/50ee9fac5b490011162d85db97d6efcd @@ -0,0 +1,448 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd new file mode 100644 index 0000000..6781d78 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/604687a231470011162d85db97d6efcd @@ -0,0 +1,182 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/60f7f535ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/60f7f535ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..c3ad37a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/60f7f535ee4a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.peek(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/70dee12af34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/70dee12af34a0011162d85db97d6efcd new file mode 100644 index 0000000..7148ad3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/70dee12af34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size() -1; i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd new file mode 100644 index 0000000..0995790 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/80b24cb377490011162d85db97d6efcd @@ -0,0 +1,13 @@ +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/d1/a0ed897835470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/a0ed897835470011162d85db97d6efcd new file mode 100644 index 0000000..773cf41 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/a0ed897835470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) templist.add(current.getData()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..9dd8e48 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0008dce3e4600111c1cd5b6f02d115c @@ -0,0 +1,13 @@ +package vettore_ordinabile; + +public class VettoriIntero { + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..62243ce --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/e0a8cfe23f4600111c1cd5b6f02d115c @@ -0,0 +1,54 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + + if (i1 < i2) return -1; + if (i1 > i2) return 1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd new file mode 100644 index 0000000..d15cdcb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d1/f0cf075733470011162d85db97d6efcd @@ -0,0 +1,210 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c new file mode 100644 index 0000000..7d04d7f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/3017d76a384600111c1cd5b6f02d115c @@ -0,0 +1,49 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd new file mode 100644 index 0000000..25ba616 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/5034f47c2f470011162d85db97d6efcd @@ -0,0 +1,131 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd new file mode 100644 index 0000000..b25e943 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/90ac7c6137470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd new file mode 100644 index 0000000..5432b30 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0cbf25378490011162d85db97d6efcd @@ -0,0 +1,18 @@ +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/d2/c0d09a6076460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0d09a6076460011162d85db97d6efcd new file mode 100644 index 0000000..ea08abb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d2/c0d09a6076460011162d85db97d6efcd @@ -0,0 +1,135 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd new file mode 100644 index 0000000..be5ace7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/90b2d2595e490011162d85db97d6efcd @@ -0,0 +1,487 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public int numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + return number; + } + + public int numberLeaf(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : numberLeaf(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : numberLeaf(node.getRight()); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd new file mode 100644 index 0000000..980b62c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d3/e092bfd74a490011162d85db97d6efcd @@ -0,0 +1,5 @@ +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 new file mode 100644 index 0000000..12847ab --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/00a4846175490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class Videogioco { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd new file mode 100644 index 0000000..415b187 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c048208931470011162d85db97d6efcd @@ -0,0 +1,180 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c new file mode 100644 index 0000000..44949b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/c0b387c6314600111c1cd5b6f02d115c @@ -0,0 +1,31 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd new file mode 100644 index 0000000..1929532 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d4/e01abfb742490011162d85db97d6efcd @@ -0,0 +1,366 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd new file mode 100644 index 0000000..5486b71 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/00090f0577460011162d85db97d6efcd @@ -0,0 +1,149 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + int hRight = (right == null) ? 0 : right.height(); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd new file mode 100644 index 0000000..9b8c053 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d5/805269c682460011162d85db97d6efcd @@ -0,0 +1,57 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd new file mode 100644 index 0000000..fa7440e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/0019fb894d490011162d85db97d6efcd @@ -0,0 +1,52 @@ +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/d6/9004a26f6b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/9004a26f6b460011162d85db97d6efcd new file mode 100644 index 0000000..90cea23 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/9004a26f6b460011162d85db97d6efcd @@ -0,0 +1,106 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d6/d018f950ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/d018f950ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..f7e8576 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d6/d018f950ee4a0011162d85db97d6efcd @@ -0,0 +1,27 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + public int size() { + return array.size(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd new file mode 100644 index 0000000..adc729a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/306e90e27a490011162d85db97d6efcd @@ -0,0 +1,17 @@ +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/d7/e05d7f2dbd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/e05d7f2dbd4b0011162d85db97d6efcd new file mode 100644 index 0000000..c4b176d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d7/e05d7f2dbd4b0011162d85db97d6efcd @@ -0,0 +1,24 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + if (paziente == null) return false; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd new file mode 100644 index 0000000..98764b5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/609026e733490011162d85db97d6efcd @@ -0,0 +1,270 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi il primo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d8/702c060ef34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/702c060ef34a0011162d85db97d6efcd new file mode 100644 index 0000000..6e86212 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d8/702c060ef34a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd new file mode 100644 index 0000000..50e6917 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/002585297c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/10f6bd5932490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/10f6bd5932490011162d85db97d6efcd new file mode 100644 index 0000000..96a17f9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/10f6bd5932490011162d85db97d6efcd @@ -0,0 +1,246 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder() { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd new file mode 100644 index 0000000..2ee1db4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/90bfa8e175490011162d85db97d6efcd @@ -0,0 +1,26 @@ +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 new file mode 100644 index 0000000..8cddde5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/c0c28e6e7f490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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/d9/e02ca87a3c4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e02ca87a3c4600111c1cd5b6f02d115c new file mode 100644 index 0000000..e5bda90 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/d9/e02ca87a3c4600111c1cd5b6f02d115c @@ -0,0 +1,77 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + if (vettore[minimo] > vettore[j]) minimo = j; + } + + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd new file mode 100644 index 0000000..24c48ad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/50a010315f490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..7c5decc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/9033323278490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..6bad699 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/da/e036aa2f7d490011162d85db97d6efcd @@ -0,0 +1,55 @@ +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/30aede39394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/30aede39394b0011162d85db97d6efcd new file mode 100644 index 0000000..252ad8e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/db/30aede39394b0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd new file mode 100644 index 0000000..f82d42a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/db/70e01b3038470011162d85db97d6efcd @@ -0,0 +1,239 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/9060375ef04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/9060375ef04a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd new file mode 100644 index 0000000..d6cfe42 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/db/a0d06ad730470011162d85db97d6efcd @@ -0,0 +1,165 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd new file mode 100644 index 0000000..245cdee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/db/b0e0579275490011162d85db97d6efcd @@ -0,0 +1,12 @@ +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/0096812236470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/0096812236470011162d85db97d6efcd new file mode 100644 index 0000000..90c2a41 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/0096812236470011162d85db97d6efcd @@ -0,0 +1,231 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd new file mode 100644 index 0000000..6b02ec6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/206722a634470011162d85db97d6efcd @@ -0,0 +1,214 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/2081945abd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/2081945abd4b0011162d85db97d6efcd new file mode 100644 index 0000000..41c2e6a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/2081945abd4b0011162d85db97d6efcd @@ -0,0 +1,27 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd new file mode 100644 index 0000000..f081272 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/3055a4825d490011162d85db97d6efcd @@ -0,0 +1,484 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd new file mode 100644 index 0000000..b4bfecf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/30d8b4457f490011162d85db97d6efcd @@ -0,0 +1,20 @@ +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 new file mode 100644 index 0000000..a6be25b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/503bd80960490011162d85db97d6efcd @@ -0,0 +1,501 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/dc/6071be1435470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/6071be1435470011162d85db97d6efcd new file mode 100644 index 0000000..817d951 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/6071be1435470011162d85db97d6efcd @@ -0,0 +1,223 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dc/70126600ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/70126600ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..6e0c0d8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dc/70126600ed4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package stack; + +public class ArrayListStack { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd new file mode 100644 index 0000000..9a2c881 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/b0ef216d37470011162d85db97d6efcd @@ -0,0 +1,233 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd new file mode 100644 index 0000000..f948d36 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/c0c4e1405d490011162d85db97d6efcd @@ -0,0 +1,481 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + + } + + public void printLeaf() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0a0a238ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0a0a238ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..5233cc3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/d0a0a238ee4a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.get(array.size() - 1); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd new file mode 100644 index 0000000..f596f82 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/e050b6832d470011162d85db97d6efcd @@ -0,0 +1,111 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd new file mode 100644 index 0000000..90c01bd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/dd/f09e80bc60490011162d85db97d6efcd @@ -0,0 +1,504 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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 new file mode 100644 index 0000000..0dd1807 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/de/50724e1476490011162d85db97d6efcd @@ -0,0 +1,37 @@ +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/50ba347e42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/50ba347e42490011162d85db97d6efcd new file mode 100644 index 0000000..6c59f84 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/de/50ba347e42490011162d85db97d6efcd @@ -0,0 +1,358 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd new file mode 100644 index 0000000..ff63d54 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/de/606c53175f490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/df/109d6d1b81460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/df/109d6d1b81460011162d85db97d6efcd new file mode 100644 index 0000000..06777b3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/109d6d1b81460011162d85db97d6efcd @@ -0,0 +1,40 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + public boolean isEmpty() { + return (size == 0); + } + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..8485ba1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/10a1dbe03f4600111c1cd5b6f02d115c @@ -0,0 +1,53 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + public int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + + if (i1 < i2) return -1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/df/30eb905ff24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/df/30eb905ff24a0011162d85db97d6efcd new file mode 100644 index 0000000..f2544e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/df/30eb905ff24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd new file mode 100644 index 0000000..2a2b13a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/301fc98031470011162d85db97d6efcd @@ -0,0 +1,178 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd new file mode 100644 index 0000000..1a85fb7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e/90d154294d490011162d85db97d6efcd @@ -0,0 +1,46 @@ +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/e0/609ef625ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/609ef625ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..55a9238 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/609ef625ed4a0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package stack; + +public interface MyStack { + + void push(T item); + + T pop(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd new file mode 100644 index 0000000..0397ece --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/7041a0625a490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c new file mode 100644 index 0000000..a010378 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/9034c3df314600111c1cd5b6f02d115c @@ -0,0 +1,36 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/905091f8f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/905091f8f24a0011162d85db97d6efcd new file mode 100644 index 0000000..b14ece7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/905091f8f24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i++) { + System.out.println(); + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd new file mode 100644 index 0000000..7ddc2bf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/908afa513e490011162d85db97d6efcd @@ -0,0 +1,296 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd new file mode 100644 index 0000000..56dfe93 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e0/c0b110f442490011162d85db97d6efcd @@ -0,0 +1,374 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(); + } else { + // il nodo non è ancora da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/200d6249fd4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/200d6249fd4a0011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd new file mode 100644 index 0000000..f22aaef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/50f446624e490011162d85db97d6efcd @@ -0,0 +1,55 @@ +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/e1/605e520b43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/605e520b43490011162d85db97d6efcd new file mode 100644 index 0000000..2afbc9f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/605e520b43490011162d85db97d6efcd @@ -0,0 +1,375 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + queueOfNodes.push(current); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd new file mode 100644 index 0000000..db38dcc --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e1/80b123895d490011162d85db97d6efcd @@ -0,0 +1,484 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node, Integer number) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) number++; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd new file mode 100644 index 0000000..234004d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/1080eb132c470011162d85db97d6efcd @@ -0,0 +1,93 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/40ef6061fd4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/40ef6061fd4a0011162d85db97d6efcd new file mode 100644 index 0000000..82565d3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/40ef6061fd4a0011162d85db97d6efcd @@ -0,0 +1,9 @@ +package jcf_set.example; + +public class TreeSetExample3 { + +} + +class Student { + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd new file mode 100644 index 0000000..2ffc5a7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/608fe2f580460011162d85db97d6efcd @@ -0,0 +1,32 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1; + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd new file mode 100644 index 0000000..9608d85 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/703bdb1243490011162d85db97d6efcd @@ -0,0 +1,376 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + queueOfNodes.push(current); + flags.push(true); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd new file mode 100644 index 0000000..9b84665 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80c9d09775490011162d85db97d6efcd @@ -0,0 +1,13 @@ +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/e2/80ed7ac8ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80ed7ac8ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..61d714c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e2/80ed7ac8ed4a0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * Ritorna il numero di elementi correnti nello stack. + * @return + */ + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd new file mode 100644 index 0000000..bd28c99 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/4041b3c82c470011162d85db97d6efcd @@ -0,0 +1,101 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/804575eef24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/804575eef24a0011162d85db97d6efcd new file mode 100644 index 0000000..9ea2c09 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/804575eef24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for () { + + } + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd new file mode 100644 index 0000000..feedbf9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/900ac8d334470011162d85db97d6efcd @@ -0,0 +1,220 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + queueOfNodes.add(node); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd new file mode 100644 index 0000000..d33d8ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e3/e06c01635d490011162d85db97d6efcd @@ -0,0 +1,482 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + printLeaf(root); + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + + if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString()); + else { + printLeaf(node.getLeft()); + printLeaf(node.getRight()); + } + } + + /* + * Si aggiunga alla classe LinkedBinaryTree un metodo che conta il numero + * di foglie dell'albero corrente + */ + public void numberLeaf() { + Integer number = 0; + numberLeaf(root, number); + } + + public void numberLeaf(BinaryNode node) { + if () + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd new file mode 100644 index 0000000..78e253f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/10a985f37f460011162d85db97d6efcd @@ -0,0 +1,16 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + + + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd new file mode 100644 index 0000000..d4f9594 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/4087a6a067460011162d85db97d6efcd @@ -0,0 +1,33 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd new file mode 100644 index 0000000..79c83c5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e4/60912d3b41490011162d85db97d6efcd @@ -0,0 +1,336 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/003d920e394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/003d920e394b0011162d85db97d6efcd new file mode 100644 index 0000000..c947b60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/003d920e394b0011162d85db97d6efcd @@ -0,0 +1,23 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/009fa737ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/009fa737ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..dccfba5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/009fa737ee4a0011162d85db97d6efcd @@ -0,0 +1,20 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + public void push(T item) { + array.add(item); + } + + public T pop() { + return array.remove(array.size() - 1); + } + + public T peek() { + return array.peek(array.size() - 1); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/4016112dbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/4016112dbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..c6c4bd9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/4016112dbe4b0011162d85db97d6efcd @@ -0,0 +1,38 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/7013bcf1ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/7013bcf1ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..f60a8a8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/7013bcf1ef4a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = 0; i < stack.size(); i++) { + Character current = stack.pop(); + System.out.println(); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/b06ebe2d394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/b06ebe2d394b0011162d85db97d6efcd new file mode 100644 index 0000000..4300a7e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/b06ebe2d394b0011162d85db97d6efcd @@ -0,0 +1,28 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd new file mode 100644 index 0000000..b155bf7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e5/c05a5ccf2e470011162d85db97d6efcd @@ -0,0 +1,123 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORI + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..8c8ee80 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/40a645103f4600111c1cd5b6f02d115c @@ -0,0 +1,23 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd new file mode 100644 index 0000000..587a26e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/501f93412c470011162d85db97d6efcd @@ -0,0 +1,95 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/5052c2c4ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/5052c2c4ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..24009b6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/5052c2c4ed4a0011162d85db97d6efcd @@ -0,0 +1,30 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * + * @return + */ + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd new file mode 100644 index 0000000..1fd5fe0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e6/c06eabe84b490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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/2056ddcdb34b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/2056ddcdb34b0011162d85db97d6efcd new file mode 100644 index 0000000..3b3ae60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/2056ddcdb34b0011162d85db97d6efcd @@ -0,0 +1,10 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd new file mode 100644 index 0000000..3c4cd92 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/808a52ce60490011162d85db97d6efcd @@ -0,0 +1,504 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/e7/80bc1cef36470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/80bc1cef36470011162d85db97d6efcd new file mode 100644 index 0000000..8611438 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/80bc1cef36470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd new file mode 100644 index 0000000..d494fea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/a03b88fb40490011162d85db97d6efcd @@ -0,0 +1,332 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(); + } + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd new file mode 100644 index 0000000..6f44d5c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e7/f00b77c673460011162d85db97d6efcd @@ -0,0 +1,119 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd new file mode 100644 index 0000000..70386b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/5007538483460011162d85db97d6efcd @@ -0,0 +1,60 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd new file mode 100644 index 0000000..cc741fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/7048924d80490011162d85db97d6efcd @@ -0,0 +1,38 @@ +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/8076a4e36a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8076a4e36a460011162d85db97d6efcd new file mode 100644 index 0000000..608b332 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8076a4e36a460011162d85db97d6efcd @@ -0,0 +1,98 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public setAsRoot() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8092f3d2424600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8092f3d2424600111c1cd5b6f02d115c new file mode 100644 index 0000000..918fa03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/8092f3d2424600111c1cd5b6f02d115c @@ -0,0 +1,5 @@ +package binary_tree; + +public class BinaryTree { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd new file mode 100644 index 0000000..0b249b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e8/e0f0342976490011162d85db97d6efcd @@ -0,0 +1,49 @@ +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/0020d68f42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/0020d68f42490011162d85db97d6efcd new file mode 100644 index 0000000..f8d9960 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/0020d68f42490011162d85db97d6efcd @@ -0,0 +1,363 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd new file mode 100644 index 0000000..3e27413 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/307aa9f542490011162d85db97d6efcd @@ -0,0 +1,374 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd new file mode 100644 index 0000000..55e2fe0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/5026957837470011162d85db97d6efcd @@ -0,0 +1,235 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd new file mode 100644 index 0000000..c0126fe --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/50ef8c9b42490011162d85db97d6efcd @@ -0,0 +1,363 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd new file mode 100644 index 0000000..5b8855b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c00253277c490011162d85db97d6efcd @@ -0,0 +1,39 @@ +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/e9/c0bc73d63b4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c0bc73d63b4600111c1cd5b6f02d115c new file mode 100644 index 0000000..063eff5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/e9/c0bc73d63b4600111c1cd5b6f02d115c @@ -0,0 +1,65 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + for (int j = i+1; j < dimensioneCorrente; j++) { + + } + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd new file mode 100644 index 0000000..e15e97c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/008f350674460011162d85db97d6efcd @@ -0,0 +1,124 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd new file mode 100644 index 0000000..f9e315a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00c278e877490011162d85db97d6efcd @@ -0,0 +1,16 @@ +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/00d8648259490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00d8648259490011162d85db97d6efcd new file mode 100644 index 0000000..c037bb4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/00d8648259490011162d85db97d6efcd @@ -0,0 +1,425 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd new file mode 100644 index 0000000..76730c3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/10cbff122d470011162d85db97d6efcd @@ -0,0 +1,106 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + } + return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd new file mode 100644 index 0000000..860ddb6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80c5163b77490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..cdd536f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/80cdfcd74b490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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 new file mode 100644 index 0000000..2aa7685 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/b027310b78490011162d85db97d6efcd @@ -0,0 +1,12 @@ +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/ea/c07bc3dc76460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/c07bc3dc76460011162d85db97d6efcd new file mode 100644 index 0000000..060b8a1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ea/c07bc3dc76460011162d85db97d6efcd @@ -0,0 +1,146 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/10ba3b9cfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/10ba3b9cfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..d6f9a99 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/10ba3b9cfc4a0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd new file mode 100644 index 0000000..d417c4f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/60f3acd676460011162d85db97d6efcd @@ -0,0 +1,146 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : height(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd new file mode 100644 index 0000000..e1d1dbd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c00f466276460011162d85db97d6efcd @@ -0,0 +1,137 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd new file mode 100644 index 0000000..febee55 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/eb/c0b4ba7e35470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c new file mode 100644 index 0000000..fc3085c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/30c7589d384600111c1cd5b6f02d115c @@ -0,0 +1,58 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + + } + + // FUNZIONI ASTRATTE + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd new file mode 100644 index 0000000..91bb173 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/40e60d222e470011162d85db97d6efcd @@ -0,0 +1,113 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd new file mode 100644 index 0000000..0e087ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60662fc75b490011162d85db97d6efcd @@ -0,0 +1,449 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60810e6cfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60810e6cfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..caf2265 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/60810e6cfc4a0011162d85db97d6efcd @@ -0,0 +1,35 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree3 + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd new file mode 100644 index 0000000..d8dcac9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/70c3454f41490011162d85db97d6efcd @@ -0,0 +1,340 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/8067a830f24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/8067a830f24a0011162d85db97d6efcd new file mode 100644 index 0000000..cad4969 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/8067a830f24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d03681aa394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d03681aa394b0011162d85db97d6efcd new file mode 100644 index 0000000..5572ce5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ec/d03681aa394b0011162d85db97d6efcd @@ -0,0 +1,39 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd new file mode 100644 index 0000000..75eba6f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/008786e85e490011162d85db97d6efcd @@ -0,0 +1,489 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/40f7512a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/40f7512a42490011162d85db97d6efcd new file mode 100644 index 0000000..3a76c06 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/40f7512a42490011162d85db97d6efcd @@ -0,0 +1,349 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..a39d1e2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/609b87fe3f4600111c1cd5b6f02d115c @@ -0,0 +1,55 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + protected int ordina(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + + if (i1 < i2) return -1; + if (i1 > i2) return 1; + return 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd new file mode 100644 index 0000000..169d26f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/60a079f47c490011162d85db97d6efcd @@ -0,0 +1,42 @@ +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/90a8ecf642490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/90a8ecf642490011162d85db97d6efcd new file mode 100644 index 0000000..560dac0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/90a8ecf642490011162d85db97d6efcd @@ -0,0 +1,375 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd new file mode 100644 index 0000000..f3ae19a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/a067004e40490011162d85db97d6efcd @@ -0,0 +1,321 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if () + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd new file mode 100644 index 0000000..3841e57 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ed/f047b1f57a490011162d85db97d6efcd @@ -0,0 +1,25 @@ +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/ee/40954b8842490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40954b8842490011162d85db97d6efcd new file mode 100644 index 0000000..f107ac4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40954b8842490011162d85db97d6efcd @@ -0,0 +1,360 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40ed944ef24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40ed944ef24a0011162d85db97d6efcd new file mode 100644 index 0000000..b74ca03 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/40ed944ef24a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getLast(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd new file mode 100644 index 0000000..5cae35e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/7082ca335a490011162d85db97d6efcd @@ -0,0 +1,440 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + BinaryNode currentNode = root; + + while (iterator.hasNext()) { + E currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + currentNode.setData(currentObject); + + if (wing == 0) { + // Left Wing + + } else { + // Right Wing + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd new file mode 100644 index 0000000..1a9f452 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/70cea9da32490011162d85db97d6efcd @@ -0,0 +1,251 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + protected void itpreorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while () { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ee/9030a96af24a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/9030a96af24a0011162d85db97d6efcd new file mode 100644 index 0000000..aeaf504 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ee/9030a96af24a0011162d85db97d6efcd @@ -0,0 +1,29 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.removeFirst(); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/2059016d394b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/2059016d394b0011162d85db97d6efcd new file mode 100644 index 0000000..8ed2f59 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/2059016d394b0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd new file mode 100644 index 0000000..43cbd92 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/20dc66fd3f490011162d85db97d6efcd @@ -0,0 +1,313 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd new file mode 100644 index 0000000..fdb9206 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/80e1af142c470011162d85db97d6efcd @@ -0,0 +1,93 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd new file mode 100644 index 0000000..1e5a494 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/905fbcfe77490011162d85db97d6efcd @@ -0,0 +1,5 @@ +package comparatori.videogioco; + +public class ComparatorVideogioco3 { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd new file mode 100644 index 0000000..aa4ec5e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/e0a02e6d5b490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd new file mode 100644 index 0000000..8981a79 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ef/f0027c137d490011162d85db97d6efcd @@ -0,0 +1,49 @@ +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/f/00b0ae8ff14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/00b0ae8ff14a0011162d85db97d6efcd new file mode 100644 index 0000000..808216b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/00b0ae8ff14a0011162d85db97d6efcd @@ -0,0 +1,15 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/307a46acfc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/307a46acfc4a0011162d85db97d6efcd new file mode 100644 index 0000000..32837df --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/307a46acfc4a0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet2: "); + System.out.println(tree2); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + System.out.println("Stampa del TreeSet3: "); + System.out.println(tree3); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/70ceeef2ec4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/70ceeef2ec4a0011162d85db97d6efcd new file mode 100644 index 0000000..547d0d0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/70ceeef2ec4a0011162d85db97d6efcd @@ -0,0 +1,5 @@ +package stack; + +public class ArrayListStack { + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/8068d796ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/8068d796ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..c1f9468 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/8068d796ed4a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd new file mode 100644 index 0000000..e0c59ef --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/909f1d4441490011162d85db97d6efcd @@ -0,0 +1,338 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c new file mode 100644 index 0000000..7c4b712 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d051d3bf3e4600111c1cd5b6f02d115c @@ -0,0 +1,11 @@ +package vettore_ordinabile; + +public class VettoriIntero { + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd new file mode 100644 index 0000000..63ee187 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/d0bbb26982460011162d85db97d6efcd @@ -0,0 +1,55 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd new file mode 100644 index 0000000..2f62929 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f/e03feef76a460011162d85db97d6efcd @@ -0,0 +1,100 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f0/e0705445bd4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f0/e0705445bd4b0011162d85db97d6efcd new file mode 100644 index 0000000..42de2ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f0/e0705445bd4b0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f1/a087a099be4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/a087a099be4b0011162d85db97d6efcd new file mode 100644 index 0000000..d42c79f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f1/a087a099be4b0011162d85db97d6efcd @@ -0,0 +1,41 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + int cmp = current.compareTo(paziente); + if (cmp > 1) + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd new file mode 100644 index 0000000..cdd536f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/304b6bdc4b490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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/40dc2e995c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/40dc2e995c490011162d85db97d6efcd new file mode 100644 index 0000000..93a7b2c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/40dc2e995c490011162d85db97d6efcd @@ -0,0 +1,463 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0ee9dde384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0ee9dde384b0011162d85db97d6efcd new file mode 100644 index 0000000..b60c037 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/a0ee9dde384b0011162d85db97d6efcd @@ -0,0 +1,13 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + + public Paziente(String id, + int annoNascita) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd new file mode 100644 index 0000000..24abebd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/b0235aab62490011162d85db97d6efcd @@ -0,0 +1,514 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/f2/c0e6b7e86a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/c0e6b7e86a460011162d85db97d6efcd new file mode 100644 index 0000000..16124c0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f2/c0e6b7e86a460011162d85db97d6efcd @@ -0,0 +1,98 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd new file mode 100644 index 0000000..b70d6fd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/402066662f470011162d85db97d6efcd @@ -0,0 +1,129 @@ +package binary_tree; + +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/70631b44f34a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/70631b44f34a0011162d85db97d6efcd new file mode 100644 index 0000000..4273783 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/70631b44f34a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.remove(0); + } + + @Override + public T peek() { + return queue.getFirst(); + } + + @Override + public int size() { + return queue.size(); + } + + @Override + public boolean isEmpty() { + return queue.isEmpty(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd new file mode 100644 index 0000000..d3a1e4e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/9087e70981460011162d85db97d6efcd @@ -0,0 +1,36 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd new file mode 100644 index 0000000..8d88711 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f3/b0e1dac24d490011162d85db97d6efcd @@ -0,0 +1,53 @@ +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/f4/009bcd6637470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/009bcd6637470011162d85db97d6efcd new file mode 100644 index 0000000..2aa55ee --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/009bcd6637470011162d85db97d6efcd @@ -0,0 +1,232 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/a0760bd0384b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/a0760bd0384b0011162d85db97d6efcd new file mode 100644 index 0000000..1f3a4c1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/a0760bd0384b0011162d85db97d6efcd @@ -0,0 +1,8 @@ +package parziale.p251110; + +public class Paziente { + + private String id; + private int annoNascita; + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f4/d036b2d2ef4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/d036b2d2ef4a0011162d85db97d6efcd new file mode 100644 index 0000000..11cd086 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f4/d036b2d2ef4a0011162d85db97d6efcd @@ -0,0 +1,25 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = 0; i < stack.size(); i++) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c new file mode 100644 index 0000000..17a4a40 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/30a05556314600111c1cd5b6f02d115c @@ -0,0 +1,23 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + } + + /** + * Funzione aggiungi + */ + public boolean aggiungi() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd new file mode 100644 index 0000000..7bc0706 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/605de02776490011162d85db97d6efcd @@ -0,0 +1,49 @@ +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/f5/70bf8cfc34470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/70bf8cfc34470011162d85db97d6efcd new file mode 100644 index 0000000..23cf796 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/70bf8cfc34470011162d85db97d6efcd @@ -0,0 +1,222 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + + queueOfNodes.add(node); + + while(!queueOfNodes.isEmpty()) { + + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd new file mode 100644 index 0000000..9a75290 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f5/c0b3b4df30470011162d85db97d6efcd @@ -0,0 +1,166 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd new file mode 100644 index 0000000..c06a1a5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/001eb8c27a490011162d85db97d6efcd @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..ed9f584 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/00f8c70b4d490011162d85db97d6efcd @@ -0,0 +1,42 @@ +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/f6/2036eb1143490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/2036eb1143490011162d85db97d6efcd new file mode 100644 index 0000000..689de91 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/2036eb1143490011162d85db97d6efcd @@ -0,0 +1,376 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + queueOfNodes.push(current); + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd new file mode 100644 index 0000000..96e26b1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f6/3077d2e665460011162d85db97d6efcd @@ -0,0 +1,7 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd new file mode 100644 index 0000000..b0c36eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/30a87d1440490011162d85db97d6efcd @@ -0,0 +1,319 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + + } else { + // il nodo non è da visitare + + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd new file mode 100644 index 0000000..9ede96f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/501f784e35470011162d85db97d6efcd @@ -0,0 +1,224 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if () + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd new file mode 100644 index 0000000..c6494ea --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/60d0454131470011162d85db97d6efcd @@ -0,0 +1,168 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a009ef81f74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a009ef81f74a0011162d85db97d6efcd new file mode 100644 index 0000000..f225d51 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a009ef81f74a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(); + + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a0fef4a4ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a0fef4a4ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..c68e3c4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/a0fef4a4ed4a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * + * @return + */ + T peek(); + + int size(); + + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd new file mode 100644 index 0000000..ada265b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/c096101a42490011162d85db97d6efcd @@ -0,0 +1,345 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f7/f07d6f43f14a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/f07d6f43f14a0011162d85db97d6efcd new file mode 100644 index 0000000..701121b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f7/f07d6f43f14a0011162d85db97d6efcd @@ -0,0 +1,17 @@ +package queue; + +public interface MyQueue { + + /** + * Aggiungi l'elemento specificato in fondo alla coda + * @param item + */ + boolean offer(T item); + + /** + * Rimuove l'elemento in testa alla coda e lo restituisce + * @return + */ + T remove(); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/50ffb5eeb34b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/50ffb5eeb34b0011162d85db97d6efcd new file mode 100644 index 0000000..11d3768 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/50ffb5eeb34b0011162d85db97d6efcd @@ -0,0 +1,14 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd new file mode 100644 index 0000000..d361cae --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/7084a7d650490011162d85db97d6efcd @@ -0,0 +1,409 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + public LinkedBinaryTree(List objectList) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/a0f7ec1af04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/a0f7ec1af04a0011162d85db97d6efcd new file mode 100644 index 0000000..be6ee45 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/a0f7ec1af04a0011162d85db97d6efcd @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i > 0; i--) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d073bb72f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d073bb72f04a0011162d85db97d6efcd new file mode 100644 index 0000000..65eb17d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d073bb72f04a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package queue; + +public class MyQueue { + + boolean offer(T item); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd new file mode 100644 index 0000000..aa4ec5e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f8/d07b20925b490011162d85db97d6efcd @@ -0,0 +1,445 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + BinaryNode currentNode = root; + E currentObject = iterator.next(); + currentNode.setData(currentObject); + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd new file mode 100644 index 0000000..1c0a9ac --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/109ef43d77460011162d85db97d6efcd @@ -0,0 +1,149 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + int hRight = (right == null) ? 0 : right.height(); + + return Math.max(hLeft + hRight) + 1; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd new file mode 100644 index 0000000..cf858cf --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/5019a0a163490011162d85db97d6efcd @@ -0,0 +1,519 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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/6073767d42490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/6073767d42490011162d85db97d6efcd new file mode 100644 index 0000000..d48fe2c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/6073767d42490011162d85db97d6efcd @@ -0,0 +1,357 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>();) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd new file mode 100644 index 0000000..34cbefd --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/608405b275460011162d85db97d6efcd @@ -0,0 +1,125 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.equals) || ) + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a023e70cbe4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a023e70cbe4b0011162d85db97d6efcd new file mode 100644 index 0000000..68bdcf9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a023e70cbe4b0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a08eab53ee4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a08eab53ee4a0011162d85db97d6efcd new file mode 100644 index 0000000..93234b0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/a08eab53ee4a0011162d85db97d6efcd @@ -0,0 +1,28 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd new file mode 100644 index 0000000..991815b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/d0fa5a8c75490011162d85db97d6efcd @@ -0,0 +1,12 @@ +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/f9/f0c05c943f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f0c05c943f490011162d85db97d6efcd new file mode 100644 index 0000000..c9d5e1b --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/f9/f0c05c943f490011162d85db97d6efcd @@ -0,0 +1,309 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra, il primo nodo da inserire è quello di + * destra + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + // Aggiungiamo il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Aggiungiamo + } + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd new file mode 100644 index 0000000..c50e715 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/000757b35c490011162d85db97d6efcd @@ -0,0 +1,463 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + + /* + * II parziale 2022/2023 + * Realizzare un metodo costruttore della classe LinkedBinaryTree che + * prende in input una lista di oggetti di tipo E e costruisce una catena + * casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo + * della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro. + */ + /* + * Spiegazione intuitiva (visto che la melideo intende complicare la consegna): + * Data ad esempio una lista del genere: + * [A, B, C, D] + * + * Si ottiene quindi una catena del genere: + * A + * \ + * B + * / + * C + * \ + * D + */ + public LinkedBinaryTree(List objectList) { + if (objectList == null) throw new NullPointerException(); + + Iterator iterator = objectList.iterator(); + if (!iterator.hasNext()) return; + + E currentObject = iterator.next(); + root = new BinaryNode(currentObject); + BinaryNode currentNode = root; + currentNode.setData(currentObject); + size = 1; + + while (iterator.hasNext()) { + currentObject = iterator.next(); + int wing = (int) (Math.random() * 2); + + BinaryNode newNode = new BinaryNode(currentObject); + + if (wing == 0) { + // Left + newNode.setParentAsLeftChild(currentNode); + } else { + // Right + newNode.setParentAsRightChild(currentNode); + } + + currentNode = newNode; + size++; + } + } + + /* + * II parziale 2022 + * Si aggiunga alla classe LinkedBinaryTree un metodo + * che stampa le foglie dall'albero corrente (da sinistra verso destra) + */ + public void printLeaf() { + + } + + public void printLeaf(BinaryNode node) { + if (node == null) return; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/00959a3bf74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/00959a3bf74a0011162d85db97d6efcd new file mode 100644 index 0000000..ad91899 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/00959a3bf74a0011162d85db97d6efcd @@ -0,0 +1,22 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + TreeSet ts = new TreeSet(new Decreasing()); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd new file mode 100644 index 0000000..6023964 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/7036d1c67f490011162d85db97d6efcd @@ -0,0 +1,30 @@ +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/fa/80c0277137470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/80c0277137470011162d85db97d6efcd new file mode 100644 index 0000000..5c86188 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/80c0277137470011162d85db97d6efcd @@ -0,0 +1,233 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd new file mode 100644 index 0000000..05478d9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/9019649f43490011162d85db97d6efcd @@ -0,0 +1,397 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + if (current.getRight() != null) queueOfNodes.push(current.getRight()); + // Infine aggiungiamo il nodo di sinistra + if (current.getLeft() != null) queueOfNodes.push(current.getLeft()); + } + } + + public Iterator ititeratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + itpreorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR INORDER ITERATIVO (NON RICORSIVO) + + /* + * Questo metodo riguarda lo stesso di quello superiore. + * Il metodo inorder richiede che si visiti prima sinistra, + * poi il nodo stesso, infine il nodo di destra. + * + * Possiamo quindi sempre adoperare uno Stack per siumlare la + * ricorsione. + * + * Come detto prima, se il primo nodo ad essere prelevato è + * l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare + * sinistra -> centro -> destra. + * + * Nel metodo sono state inserite delle check con uno stack di + * flag che permette di capire quando il nodo centrale è visitare + * oppure no. + */ + private void itinorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.add(node); + flags.push(false); + + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è da visitare + + // prima si aggiunge il nodo di destra + if (current.getRight() != null) { + queueOfNodes.add(current.getRight()); + flags.add(false); + } + + queueOfNodes.push(current); + flags.push(true); + + if (current.getLeft() != null) { + queueOfNodes.add(current.getLeft()); + flags.push(false); + } + + } + } + } + + public Iterator ititeratorInOrder() { + ArrayList temporaryList = new ArrayList(); + itinorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATOR POST ORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso la logica è la seguente. si ha la + * necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE + * + * Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario, + * dunque NODO CORRENTE -> DESTRA -> SINISTRA + * + * Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra. + */ + protected void itpostorder(BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + Stack flags = new Stack(); + + BinaryNode current; + Boolean flag; + + queueOfNodes.push(node); + flags.push(false); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + flag = flags.pop(); + + if (flag) { + // il nodo è da visitare + if (current.getData() != null) temporaryList.add(current.getData()); + } else { + // il nodo non è ancora da visitare + + // Visitiamo il nodo corrente + queueOfNodes.push(current); + flags.push(true); + + // visitiamo il nodo di destra + if (current.getRight() != null) { + queueOfNodes.push(current.getRight()); + flags.push(false); + } + + // visitiamo il nodo di sinistra infine + if (current.getLeft() != null) { + queueOfNodes.push(current.getLeft()); + flags.push(false); + } + } + } + } + + public Iterator ititeratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + itpostorder(root, temporaryList); + return temporaryList.iterator(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd new file mode 100644 index 0000000..32e8b05 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/a062590781460011162d85db97d6efcd @@ -0,0 +1,32 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd new file mode 100644 index 0000000..da64fad --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fa/d00ff3c52c470011162d85db97d6efcd @@ -0,0 +1,101 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10214a827a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10214a827a490011162d85db97d6efcd new file mode 100644 index 0000000..e69de29 diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd new file mode 100644 index 0000000..ca3b4b2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/10aedfc37a490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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/506acab431470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/506acab431470011162d85db97d6efcd new file mode 100644 index 0000000..f88e94e --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/506acab431470011162d85db97d6efcd @@ -0,0 +1,185 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(); + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd new file mode 100644 index 0000000..0981778 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/a08ffa6e4d490011162d85db97d6efcd @@ -0,0 +1,51 @@ +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/fb/b0c21486f04a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/b0c21486f04a0011162d85db97d6efcd new file mode 100644 index 0000000..e85d8f1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/b0c21486f04a0011162d85db97d6efcd @@ -0,0 +1,7 @@ +package queue; + +public interface MyQueue { + + boolean offer(T item); + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/d047fad5ed4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/d047fad5ed4a0011162d85db97d6efcd new file mode 100644 index 0000000..3450a60 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/d047fad5ed4a0011162d85db97d6efcd @@ -0,0 +1,34 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * Ritorna il numero di elementi correnti nello stack. + * @return Numero di elementi nello stack. + */ + int size(); + + /** + * Ritorna se ci sono elementi nello stack o meno + * @return + */ + boolean isEmpty(); +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd new file mode 100644 index 0000000..b7a6ad8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/e08b4a326a460011162d85db97d6efcd @@ -0,0 +1,78 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (this.parent == null) return null; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd new file mode 100644 index 0000000..ceb4477 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fb/f04a91032d470011162d85db97d6efcd @@ -0,0 +1,105 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + + } return false; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c new file mode 100644 index 0000000..d365567 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/0096e9523f4600111c1cd5b6f02d115c @@ -0,0 +1,37 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/a0ee3f1dbc4b0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/a0ee3f1dbc4b0011162d85db97d6efcd new file mode 100644 index 0000000..1b302f4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/a0ee3f1dbc4b0011162d85db97d6efcd @@ -0,0 +1,23 @@ +package parziale.p251110; + +import java.util.ArrayList; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b0056b54fc4a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b0056b54fc4a0011162d85db97d6efcd new file mode 100644 index 0000000..ee7c8b2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b0056b54fc4a0011162d85db97d6efcd @@ -0,0 +1,28 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + + } + + public void run() { + + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + TreeSet tree2 = new TreeSet(tree1); + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b093699ff74a0011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b093699ff74a0011162d85db97d6efcd new file mode 100644 index 0000000..4f9da0f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/b093699ff74a0011162d85db97d6efcd @@ -0,0 +1,35 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd new file mode 100644 index 0000000..4322605 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fc/e0c897f14a490011162d85db97d6efcd @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..f79a397 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/204b1a5976490011162d85db97d6efcd @@ -0,0 +1,57 @@ +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 new file mode 100644 index 0000000..b395512 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/b0b1616180490011162d85db97d6efcd @@ -0,0 +1,44 @@ +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/fd/d0051c1d80460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d0051c1d80460011162d85db97d6efcd new file mode 100644 index 0000000..8dc273a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d0051c1d80460011162d85db97d6efcd @@ -0,0 +1,17 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + + // METODI + + @Override + public boolean isEmpty() { + + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd new file mode 100644 index 0000000..a528534 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/d05b94546b460011162d85db97d6efcd @@ -0,0 +1,105 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + + return oldParent; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd new file mode 100644 index 0000000..d0c2e74 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fd/e06265aa82460011162d85db97d6efcd @@ -0,0 +1,55 @@ +package binary_tree; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node.getLeft() == null && node.getRight() == null) return 0; + } + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd new file mode 100644 index 0000000..cf4812d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/60c3bc217b490011162d85db97d6efcd @@ -0,0 +1,27 @@ +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 new file mode 100644 index 0000000..a9f4894 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/fe/c02b7e3d7b490011162d85db97d6efcd @@ -0,0 +1,32 @@ +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/.history/ff/209a7ab667460011162d85db97d6efcd b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/209a7ab667460011162d85db97d6efcd new file mode 100644 index 0000000..2238ce8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.history/ff/209a7ab667460011162d85db97d6efcd @@ -0,0 +1,34 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) + } + + // METODI + + + +} diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.location new file mode 100644 index 0000000..51c5d9d Binary files /dev/null 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 new file mode 100644 index 0000000..87fa9a4 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap new file mode 100644 index 0000000..87fa9a4 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/.org.eclipse.egit.core.cmp/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index new file mode 100644 index 0000000..e04a951 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/af/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/11/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/11/history.index new file mode 100644 index 0000000..5734c07 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/11/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index new file mode 100644 index 0000000..9657852 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/79/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/88/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/88/history.index new file mode 100644 index 0000000..cde8139 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/88/history.index 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 new file mode 100644 index 0000000..3a50960 Binary files /dev/null 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/92/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/92/history.index new file mode 100644 index 0000000..bd28b16 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/92/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 new file mode 100644 index 0000000..4ad7dbb Binary files /dev/null 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 new file mode 100644 index 0000000..adffa50 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/e4/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index new file mode 100644 index 0000000..8733198 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.indexes/e4/history.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.location b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.location new file mode 100644 index 0000000..7eac6bd Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.location differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers new file mode 100644 index 0000000..92e43e6 Binary files /dev/null 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 new file mode 100644 index 0000000..7b125bc Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap new file mode 100644 index 0000000..87fa9a4 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.projects/asdl/.syncinfo.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version new file mode 100644 index 0000000..25cb955 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/history.version @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index new file mode 100644 index 0000000..a0af7b2 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.index differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version new file mode 100644 index 0000000..6b2aaa7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.resources/.root/.indexes/properties.version @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap new file mode 100644 index 0000000..1a6e81b Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.root/.markers.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree b/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree new file mode 100644 index 0000000..4149426 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.root/1.tree differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources new file mode 100644 index 0000000..390607d Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/.safetable/org.eclipse.core.resources differ diff --git a/.metadata/.plugins/org.eclipse.core.resources/1.snap b/.metadata/.plugins/org.eclipse.core.resources/1.snap new file mode 100644 index 0000000..8fba0d7 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.core.resources/1.snap differ diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ant.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ant.ui.prefs new file mode 100644 index 0000000..565f933 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ant.ui.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +useAnnotationsPrefPage=true +useQuickDiffPrefPage=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..30841eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding=UTF-8 +version=1 diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.variables.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.variables.prefs new file mode 100644 index 0000000..0cc508a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.variables.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.core.variables.valueVariables=\n\n 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 new file mode 100644 index 0000000..4b54bf5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.debug.ui.prefs @@ -0,0 +1,20 @@ +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=\n\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, +preferredTargets=default\:default| diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..7de1e05 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.classpathVariable.JRE_LIB=/Users/eslusarz/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.macosx.aarch64_21.0.10.v20260205-0638/jre/lib/jrt-fs.jar +org.eclipse.jdt.core.classpathVariable.JRE_SRC=/Users/eslusarz/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.macosx.aarch64_21.0.10.v20260205-0638/jre/lib/src.zip +org.eclipse.jdt.core.classpathVariable.JRE_SRCROOT= +org.eclipse.jdt.core.codeComplete.visibilityCheck=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.junit.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.junit.prefs new file mode 100644 index 0000000..31df02c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.junit.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.junit.content_assist_favorite_static_members_migrated=true 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 new file mode 100644 index 0000000..0dafcd4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.launching.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +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 new file mode 100644 index 0000000..a815d4c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs @@ -0,0 +1,170 @@ +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 +spelling_ignore_java_strings=true +spelling_ignore_mixed=true +spelling_ignore_non_letters=true +spelling_ignore_sentence=true +spelling_ignore_single_letters=true +spelling_ignore_upper=true +spelling_ignore_urls=true +spelling_locale_initialized=true +spelling_user_dictionary_encoding= +typefilter_migrated_2=true +useAnnotationsPrefPage=true +useQuickDiffPrefPage=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs new file mode 100644 index 0000000..43e97e4 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.context.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +mylyn.attention.migrated=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs new file mode 100644 index 0000000..8d462a6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.monitor.ui.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.mylyn.monitor.activity.tracking.enabled.checked=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.tasks.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.tasks.ui.prefs new file mode 100644 index 0000000..5330e43 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.mylyn.tasks.ui.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +migrated.task.repositories.secure.store=true +org.eclipse.mylyn.tasks.ui.filters.nonmatching=true +org.eclipse.mylyn.tasks.ui.filters.nonmatching.encouraged=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.browser.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.browser.prefs new file mode 100644 index 0000000..a394269 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.browser.prefs @@ -0,0 +1,2 @@ +browsers=\n\n\n\n\n +eclipse.preferences.version=1 diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs new file mode 100644 index 0000000..aecbeeb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.editors.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +spellingEnabled=false 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 new file mode 100644 index 0000000..489e2c7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.ide.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +platformState=943974497357990 +quickStart=false +tipsAndTricks=true diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs new file mode 100644 index 0000000..08076f2 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +showIntro=false diff --git a/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs new file mode 100644 index 0000000..4178616 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.ui.workbench.prefs @@ -0,0 +1,17 @@ +//org.eclipse.ui.commands/state/org.eclipse.ui.navigator.resources.nested.changeProjectPresentation/org.eclipse.ui.commands.radioState=false +ColorsAndFontsPreferencePage.expandedCategories=Torg.eclipse.jdt.ui.presentation +ColorsAndFontsPreferencePage.selectedElement=Forg.eclipse.jdt.ui.editors.textfont +PLUGINS_NOT_ACTIVATED_ON_STARTUP=;org.eclipse.m2e.discovery; +eclipse.preferences.version=1 +org.eclipse.jdt.internal.ui.compare.JavaMergeViewer=1|Menlo|18.0|0|COCOA|1|Menlo-Regular; +org.eclipse.jdt.ui.ColoredLabels.match_highlight=206,92,0 +org.eclipse.jdt.ui.editors.textfont=1|Menlo|18.0|0|COCOA|1|Menlo-Regular; +org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_BG_END=41,41,41 +org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_BG_START=43,44,45 +org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_TEXT_COLOR=255,255,255 +org.eclipse.ui.workbench.ACTIVE_TAB_BG_END=72,72,76 +org.eclipse.ui.workbench.ACTIVE_TAB_BG_START=72,72,76 +org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR=187,187,187 +org.eclipse.ui.workbench.INACTIVE_TAB_BG_END=49,53,56 +org.eclipse.ui.workbench.INACTIVE_TAB_BG_START=59,64,66 +org.eclipse.ui.workbench.INACTIVE_TAB_TEXT_COLOR=187,187,187 diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/InteriRipetuti.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/InteriRipetuti.launch new file mode 100644 index 0000000..c0ce2b8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/InteriRipetuti.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (1).launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (1).launch new file mode 100644 index 0000000..38e25c3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (1).launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (2).launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (2).launch new file mode 100644 index 0000000..51ed868 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main (2).launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/Main.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main.launch new file mode 100644 index 0000000..8387071 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/Main.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/QueueExample.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/QueueExample.launch new file mode 100644 index 0000000..9d65db3 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/QueueExample.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/StackExample.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/StackExample.launch new file mode 100644 index 0000000..009eda1 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/StackExample.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample1.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample1.launch new file mode 100644 index 0000000..1f55cd6 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample1.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample2.launch b/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample2.launch new file mode 100644 index 0000000..e757aed --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.core/.launches/TreeSetExample2.launch @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml new file mode 100644 index 0000000..462dd28 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.debug.ui/launchConfigurationHistory.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi new file mode 100644 index 0000000..bca9641 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi @@ -0,0 +1,2724 @@ + + + + activeSchemeId:org.eclipse.ui.defaultAcceleratorConfiguration + + + + + + + + topLevel + + + + + persp.actionSet:org.eclipse.mylyn.tasks.ui.navigation + persp.actionSet:org.eclipse.ui.cheatsheets.actionSet + persp.actionSet:org.eclipse.search.searchActionSet + persp.actionSet:org.eclipse.text.quicksearch.actionSet + persp.actionSet:org.eclipse.ui.edit.text.actionSet.annotationNavigation + persp.actionSet:org.eclipse.ui.edit.text.actionSet.navigation + persp.actionSet:org.eclipse.ui.edit.text.actionSet.convertLineDelimitersTo + persp.actionSet:org.eclipse.ui.externaltools.ExternalToolsSet + persp.actionSet:org.eclipse.ui.actionSet.keyBindings + persp.actionSet:org.eclipse.ui.actionSet.openFiles + persp.actionSet:org.eclipse.debug.ui.launchActionSet + persp.actionSet:org.eclipse.jdt.ui.JavaActionSet + persp.actionSet:org.eclipse.jdt.ui.JavaElementCreationActionSet + persp.actionSet:org.eclipse.ui.NavigateActionSet + persp.viewSC:org.eclipse.jdt.ui.PackageExplorer + persp.viewSC:org.eclipse.jdt.ui.TypeHierarchy + persp.viewSC:org.eclipse.jdt.ui.SourceView + persp.viewSC:org.eclipse.jdt.ui.JavadocView + persp.viewSC:org.eclipse.search.ui.views.SearchView + persp.viewSC:org.eclipse.ui.console.ConsoleView + persp.viewSC:org.eclipse.ui.views.ContentOutline + persp.viewSC:org.eclipse.ui.views.ProblemView + persp.viewSC:org.eclipse.ui.views.TaskList + persp.viewSC:org.eclipse.ui.views.ProgressView + persp.viewSC:org.eclipse.ui.navigator.ProjectExplorer + persp.viewSC:org.eclipse.ui.texteditor.TemplatesView + persp.viewSC:org.eclipse.pde.runtime.LogView + persp.newWizSC:org.eclipse.jdt.ui.wizards.JavaProjectWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewPackageCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewClassCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewInterfaceCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewEnumCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewRecordCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewAnnotationCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewSourceFolderCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewSnippetFileCreationWizard + persp.newWizSC:org.eclipse.jdt.ui.wizards.NewJavaWorkingSetWizard + persp.newWizSC:org.eclipse.ui.wizards.new.folder + persp.newWizSC:org.eclipse.ui.wizards.new.file + persp.newWizSC:org.eclipse.ui.editors.wizards.UntitledTextFileWizard + persp.perspSC:org.eclipse.jdt.ui.JavaBrowsingPerspective + persp.perspSC:org.eclipse.debug.ui.DebugPerspective + persp.showIn:org.eclipse.jdt.ui.PackageExplorer + persp.showIn:org.eclipse.team.ui.GenericHistoryView + persp.showIn:org.eclipse.ui.navigator.ProjectExplorer + persp.viewSC:org.eclipse.mylyn.tasks.ui.views.tasks + persp.newWizSC:org.eclipse.mylyn.tasks.ui.wizards.new.repository.task + persp.viewSC:org.eclipse.terminal.view.ui.TerminalsView + persp.showIn:org.eclipse.terminal.view.ui.TerminalsView + persp.actionSet:org.eclipse.debug.ui.breakpointActionSet + persp.actionSet:org.eclipse.jdt.debug.ui.JDTDebugActionSet + persp.newWizSC:org.eclipse.m2e.core.wizards.Maven2ProjectWizard + persp.actionSet:org.eclipse.eclemma.ui.CoverageActionSet + persp.showIn:org.eclipse.eclemma.ui.CoverageView + persp.viewSC:org.eclipse.jdt.bcoview.views.BytecodeOutlineView + persp.showIn:org.eclipse.egit.ui.RepositoriesView + persp.newWizSC:org.eclipse.jdt.junit.wizards.NewTestCaseCreationWizard + persp.actionSet:org.eclipse.jdt.junit.JUnitActionSet + 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$$$⌘3 + persp.editorOnboardingCommand:Show Key Assist$$$⇧⌘L + persp.editorOnboardingCommand:New$$$⌘N + persp.editorOnboardingCommand:Open Type$$$⇧⌘T + + + + org.eclipse.e4.primaryNavigationStack + + View + categoryTag:Java + + + View + categoryTag:Java + + + View + categoryTag:General + + + View + categoryTag:Java + + + + + View + categoryTag:Git + + + + + + + + + + View + categoryTag:General + + + + + org.eclipse.e4.secondaryNavigationStack + Minimized + + View + categoryTag:General + + + View + categoryTag:General + + + View + categoryTag:General + + + View + categoryTag:Mylyn + + + View + categoryTag:Java + + + View + categoryTag:Ant + + + + + org.eclipse.e4.secondaryDataStack + Oomph + Minimized + + View + categoryTag:General + + + View + categoryTag:Java + + + View + categoryTag:Java + + + View + categoryTag:General + + + View + categoryTag:General + + + View + categoryTag:General + + + View + categoryTag:Terminal + + + View + categoryTag:Oomph + NoRestore + + + View + categoryTag:Oomph + NoRestore + + + + + + + + + View + categoryTag:Help + + + View + categoryTag:General + + + View + categoryTag:Help + + + + + + + View + categoryTag:Help + + + + + + View + categoryTag:General + + ViewMenu + menuContribution:menu + + + + + + + View + categoryTag:Help + + + + EditorStack + org.eclipse.e4.primaryDataStack + active + noFocus + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + + + + Editor + removeOnHide + org.eclipse.jdt.ui.CompilationUnitEditor + active + + + + + + + + View + categoryTag:Java + + ViewMenu + menuContribution:menu + + + + + + + View + categoryTag:Java + + + + + View + categoryTag:General + + + + + + View + categoryTag:General + + ViewMenu + menuContribution:menu + + + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java + + + + + View + categoryTag:General + + + + + + View + categoryTag:General + + ViewMenu + menuContribution:menu + + + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + + View + categoryTag:General + + ViewMenu + menuContribution:menu + + + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Terminal + + + + + View + categoryTag:Java + + + + + View + categoryTag:Git + + + + + View + categoryTag:Java + + + + + View + categoryTag:Ant + + + + + + View + categoryTag:Oomph + NoRestore + + ViewMenu + menuContribution:menu + + + + + + + + View + categoryTag:Oomph + NoRestore + + ViewMenu + menuContribution:menu + + + + + + toolbarSeparator + + + + Draggable + + + + toolbarSeparator + + + + Draggable + + + + + toolbarSeparator + + + + Draggable + + + Draggable + + + Draggable + + + Draggable + + + toolbarSeparator + + + + Draggable + + + + Draggable + + + toolbarSeparator + + + + toolbarSeparator + + + + Draggable + + + stretch + SHOW_RESTORE_MENU + + + Draggable + HIDEABLE + SHOW_RESTORE_MENU + + + + + stretch + + + Draggable + + + Draggable + + + + + TrimStack + Draggable + + + TrimStack + Draggable + + + + + TrimStack + Draggable + + + TrimStack + Draggable + + + TrimStack + Draggable + + + + + + + + + + + + platform:cocoa + + + platform:cocoa + + + + + + platform: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 + + + + 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 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Editor + removeOnHide + + + + + View + categoryTag:Ant + + + + + View + categoryTag:Gradle + + + + + View + categoryTag:Gradle + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Debug + + + View + categoryTag:Debug + + + + + View + categoryTag:Java + + + + + View + categoryTag:Git + + + + + View + categoryTag:Git + + + + + View + categoryTag:Git + + + + + View + categoryTag:Git + NoRestore + + + + + View + categoryTag:Git + + + + + View + categoryTag:Help + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java + + + + + View + categoryTag:Debug + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java Browsing + + + + + View + categoryTag:Java Browsing + + + + + View + categoryTag:Java Browsing + + + + + View + categoryTag:Java Browsing + + + + + View + categoryTag:Java + + + + + View + categoryTag:General + + + + + View + categoryTag:Java + + + + + View + categoryTag:Java + + + + + View + categoryTag:Language Servers + + + + + View + categoryTag:Language Servers + + + + + View + categoryTag:Language Servers + + + + + View + categoryTag:Maven + + + + + View + categoryTag:Maven + + + + + View + categoryTag:Maven + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Mylyn + + + + + View + categoryTag:Oomph + + + + + View + categoryTag:Oomph + NoRestore + + + + + View + categoryTag:Plug-in Development + + + + + View + categoryTag:General + + + + + View + categoryTag:Version Control (Team) + + + + + View + categoryTag:Version Control (Team) + + + + + View + categoryTag:Terminal + + + View + categoryTag:Help + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:Help + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + + View + categoryTag:General + + + + glue + move_after:PerspectiveSpacer + SHOW_RESTORE_MENU + + + move_after:Spacer Glue + HIDEABLE + SHOW_RESTORE_MENU + + + glue + move_after:SearchField + SHOW_RESTORE_MENU + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.project b/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.project new file mode 100644 index 0000000..3c10856 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.project @@ -0,0 +1,11 @@ + + + .org.eclipse.egit.core.cmp + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.settings/org.eclipse.core.resources.prefs b/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.egit.core/.org.eclipse.egit.core.cmp/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index b/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index new file mode 100644 index 0000000..a400303 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/1865797976.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/353077879.index b/.metadata/.plugins/org.eclipse.jdt.core/353077879.index new file mode 100644 index 0000000..507393c Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/353077879.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/937937267.index b/.metadata/.plugins/org.eclipse.jdt.core/937937267.index new file mode 100644 index 0000000..7bbd0cb Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/937937267.index differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/assumedExternalFilesCache b/.metadata/.plugins/org.eclipse.jdt.core/assumedExternalFilesCache new file mode 100644 index 0000000..593f470 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/assumedExternalFilesCache differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache b/.metadata/.plugins/org.eclipse.jdt.core/externalFilesCache new file mode 100644 index 0000000..5119c62 Binary files /dev/null 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 new file mode 100644 index 0000000..a6ddeb1 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/externalLibsTimeStamps differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/javaLikeNames.txt b/.metadata/.plugins/org.eclipse.jdt.core/javaLikeNames.txt new file mode 100644 index 0000000..8586397 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.core/javaLikeNames.txt @@ -0,0 +1 @@ +java \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache b/.metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache new file mode 100644 index 0000000..9b11e0f Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/nonChainingJarsCache differ diff --git a/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt new file mode 100644 index 0000000..973bebb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt @@ -0,0 +1,4 @@ +INDEX VERSION 1.134+/Users/eslusarz/eclipse-workspace/.metadata/.plugins/org.eclipse.jdt.core +353077879.index +1865797976.index +937937267.index diff --git a/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat b/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat new file mode 100644 index 0000000..806a42d Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.core/variablesAndContainers.dat differ diff --git a/.metadata/.plugins/org.eclipse.jdt.launching/.install.xml b/.metadata/.plugins/org.eclipse.jdt.launching/.install.xml new file mode 100644 index 0000000..c3f1fa8 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.launching/.install.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml b/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml new file mode 100644 index 0000000..efac03c --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml b/.metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml new file mode 100644 index 0000000..a4ee3cb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.ui/OpenTypeHistory.xml @@ -0,0 +1,2 @@ + + diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml b/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml new file mode 100644 index 0000000..9e390f5 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.ui/QualifiedTypeNameHistory.xml @@ -0,0 +1,2 @@ + + diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml new file mode 100644 index 0000000..07d982d --- /dev/null +++ b/.metadata/.plugins/org.eclipse.jdt.ui/dialog_settings.xml @@ -0,0 +1,14 @@ + +
+
+ + + + + +
+
+
+
+
+
diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png new file mode 100644 index 0000000..dfbdbb0 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/0.png 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 new file mode 100644 index 0000000..f5c6e73 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/1.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png new file mode 100644 index 0000000..f019b3d Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/10.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/11.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/11.png new file mode 100644 index 0000000..4eee966 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/11.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/12.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/12.png new file mode 100644 index 0000000..05b280b Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/12.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png new file mode 100644 index 0000000..469abaa Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/2.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png new file mode 100644 index 0000000..6630b18 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/3.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png new file mode 100644 index 0000000..4210c2a Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/4.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png new file mode 100644 index 0000000..e5624e3 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/5.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png new file mode 100644 index 0000000..4f58ae8 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/6.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png new file mode 100644 index 0000000..a4446f4 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/7.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png new file mode 100644 index 0000000..d30e25e Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/8.png differ diff --git a/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png new file mode 100644 index 0000000..0302b8c Binary files /dev/null and b/.metadata/.plugins/org.eclipse.jdt.ui/jdt-images/9.png differ diff --git a/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.history b/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.history new file mode 100644 index 0000000..688a5f9 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.history @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.index b/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.index new file mode 100644 index 0000000..d72de4a --- /dev/null +++ b/.metadata/.plugins/org.eclipse.ltk.core.refactoring/.refactorings/asdl/2026/5/19/refactorings.index @@ -0,0 +1 @@ +1778258754614 Delete element diff --git a/.metadata/.plugins/org.eclipse.m2e.core/workspaceState.ser b/.metadata/.plugins/org.eclipse.m2e.core/workspaceState.ser new file mode 100644 index 0000000..abbf8e5 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.m2e.core/workspaceState.ser differ diff --git a/.metadata/.plugins/org.eclipse.m2e.logback/0.log b/.metadata/.plugins/org.eclipse.m2e.logback/0.log new file mode 100644 index 0000000..1a0b5eb --- /dev/null +++ b/.metadata/.plugins/org.eclipse.m2e.logback/0.log @@ -0,0 +1,3 @@ +2026-05-02 15:49:24,070 [Worker-2: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is not available. Remote download required. +2026-05-02 19:00:17,148 [Worker-7: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. +2026-05-02 22:23:21,340 [Worker-2: Loading available Gradle versions] INFO o.e.b.c.i.u.g.PublishedGradleVersions - Gradle version information cache is up-to-date. Trying to read. diff --git a/.metadata/.plugins/org.eclipse.m2e.logback/logback.2.7.101.20251017-1242.xml b/.metadata/.plugins/org.eclipse.m2e.logback/logback.2.7.101.20251017-1242.xml new file mode 100644 index 0000000..9effde7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.m2e.logback/logback.2.7.101.20251017-1242.xml @@ -0,0 +1,41 @@ + + + + %date [%thread] %-5level %logger{35} - %msg%n + + + ${org.eclipse.m2e.log.console.threshold:-OFF} + + + + + ${org.eclipse.m2e.log.dir}/0.log + + ${org.eclipse.m2e.log.dir}/%i.log + 1 + 10 + + + 10MB + + + %date [%thread] %-5level %logger{35} - %msg%n + + + + + + WARN + + + + + + + + + + + + + diff --git a/.metadata/.plugins/org.eclipse.mylyn.github.ui/avatars.ser b/.metadata/.plugins/org.eclipse.mylyn.github.ui/avatars.ser new file mode 100644 index 0000000..1e9a069 Binary files /dev/null and b/.metadata/.plugins/org.eclipse.mylyn.github.ui/avatars.ser differ diff --git a/.metadata/.plugins/org.eclipse.oomph.setup/workspace.setup b/.metadata/.plugins/org.eclipse.oomph.setup/workspace.setup new file mode 100644 index 0000000..1f73e14 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.oomph.setup/workspace.setup @@ -0,0 +1,6 @@ + + diff --git a/.metadata/.plugins/org.eclipse.tips.ide/dialog_settings.xml b/.metadata/.plugins/org.eclipse.tips.ide/dialog_settings.xml new file mode 100644 index 0000000..5ca0b77 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.tips.ide/dialog_settings.xml @@ -0,0 +1,3 @@ + +
+
diff --git a/.metadata/.plugins/org.eclipse.ui.intro/introstate b/.metadata/.plugins/org.eclipse.ui.intro/introstate new file mode 100644 index 0000000..02f134f --- /dev/null +++ b/.metadata/.plugins/org.eclipse.ui.intro/introstate @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml b/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml new file mode 100644 index 0000000..8adebd7 --- /dev/null +++ b/.metadata/.plugins/org.eclipse.ui.workbench/workingsets.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.metadata/version.ini b/.metadata/version.ini new file mode 100644 index 0000000..3f4a97f --- /dev/null +++ b/.metadata/version.ini @@ -0,0 +1,3 @@ +#Sat May 02 22:23:18 CEST 2026 +org.eclipse.core.runtime=2 +org.eclipse.platform=4.39.0.v20260226-0420 diff --git a/asdl/.classpath b/asdl/.classpath new file mode 100644 index 0000000..463d5f9 --- /dev/null +++ b/asdl/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/asdl/.project b/asdl/.project new file mode 100644 index 0000000..b86718b --- /dev/null +++ b/asdl/.project @@ -0,0 +1,17 @@ + + + asdl + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/asdl/.settings/org.eclipse.core.resources.prefs b/asdl/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/asdl/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/asdl/.settings/org.eclipse.jdt.core.prefs b/asdl/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..9a7984b --- /dev/null +++ b/asdl/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/asdl/bin/binary_tree/BinaryNode.class b/asdl/bin/binary_tree/BinaryNode.class new file mode 100644 index 0000000..ccbee92 Binary files /dev/null and b/asdl/bin/binary_tree/BinaryNode.class differ diff --git a/asdl/bin/binary_tree/BinaryTree.class b/asdl/bin/binary_tree/BinaryTree.class new file mode 100644 index 0000000..2cf0e0c Binary files /dev/null and b/asdl/bin/binary_tree/BinaryTree.class differ diff --git a/asdl/bin/binary_tree/ExerciseBinaryTree.class b/asdl/bin/binary_tree/ExerciseBinaryTree.class new file mode 100644 index 0000000..dd07f93 Binary files /dev/null and b/asdl/bin/binary_tree/ExerciseBinaryTree.class differ diff --git a/asdl/bin/binary_tree/LinkedBinaryTree.class b/asdl/bin/binary_tree/LinkedBinaryTree.class new file mode 100644 index 0000000..b113d29 Binary files /dev/null and b/asdl/bin/binary_tree/LinkedBinaryTree.class differ diff --git a/asdl/bin/comparatori/videogioco/ComparatorVideogioco1.class b/asdl/bin/comparatori/videogioco/ComparatorVideogioco1.class new file mode 100644 index 0000000..2eb1b89 Binary files /dev/null and b/asdl/bin/comparatori/videogioco/ComparatorVideogioco1.class differ diff --git a/asdl/bin/comparatori/videogioco/ComparatorVideogioco2.class b/asdl/bin/comparatori/videogioco/ComparatorVideogioco2.class new file mode 100644 index 0000000..f7724ab Binary files /dev/null and b/asdl/bin/comparatori/videogioco/ComparatorVideogioco2.class differ diff --git a/asdl/bin/comparatori/videogioco/ComparatorVideogioco3.class b/asdl/bin/comparatori/videogioco/ComparatorVideogioco3.class new file mode 100644 index 0000000..691eff1 Binary files /dev/null and b/asdl/bin/comparatori/videogioco/ComparatorVideogioco3.class differ diff --git a/asdl/bin/comparatori/videogioco/Main.class b/asdl/bin/comparatori/videogioco/Main.class new file mode 100644 index 0000000..4bc603a Binary files /dev/null and b/asdl/bin/comparatori/videogioco/Main.class differ diff --git a/asdl/bin/comparatori/videogioco/Videogioco.class b/asdl/bin/comparatori/videogioco/Videogioco.class new file mode 100644 index 0000000..f7200ad Binary files /dev/null and b/asdl/bin/comparatori/videogioco/Videogioco.class differ diff --git a/asdl/bin/jcf_set/example/Decreasing.class b/asdl/bin/jcf_set/example/Decreasing.class new file mode 100644 index 0000000..f3a11c2 Binary files /dev/null and b/asdl/bin/jcf_set/example/Decreasing.class differ diff --git a/asdl/bin/jcf_set/example/InteriRipetuti.class b/asdl/bin/jcf_set/example/InteriRipetuti.class new file mode 100644 index 0000000..41f30b4 Binary files /dev/null and b/asdl/bin/jcf_set/example/InteriRipetuti.class differ diff --git a/asdl/bin/jcf_set/example/TreeSetExample1.class b/asdl/bin/jcf_set/example/TreeSetExample1.class new file mode 100644 index 0000000..ed46266 Binary files /dev/null and b/asdl/bin/jcf_set/example/TreeSetExample1.class differ diff --git a/asdl/bin/jcf_set/example/TreeSetExample2.class b/asdl/bin/jcf_set/example/TreeSetExample2.class new file mode 100644 index 0000000..e98f5b9 Binary files /dev/null and b/asdl/bin/jcf_set/example/TreeSetExample2.class differ diff --git a/asdl/bin/module-info.class b/asdl/bin/module-info.class new file mode 100644 index 0000000..8d44288 Binary files /dev/null and b/asdl/bin/module-info.class differ diff --git a/asdl/bin/parziale/p251110/Clinica.class b/asdl/bin/parziale/p251110/Clinica.class new file mode 100644 index 0000000..d466549 Binary files /dev/null and b/asdl/bin/parziale/p251110/Clinica.class differ diff --git a/asdl/bin/parziale/p251110/Paziente.class b/asdl/bin/parziale/p251110/Paziente.class new file mode 100644 index 0000000..43c2b3b Binary files /dev/null and b/asdl/bin/parziale/p251110/Paziente.class differ diff --git a/asdl/bin/queue/ArrayListQueue.class b/asdl/bin/queue/ArrayListQueue.class new file mode 100644 index 0000000..7fffd91 Binary files /dev/null and b/asdl/bin/queue/ArrayListQueue.class differ diff --git a/asdl/bin/queue/MyQueue.class b/asdl/bin/queue/MyQueue.class new file mode 100644 index 0000000..87e7efb Binary files /dev/null and b/asdl/bin/queue/MyQueue.class differ diff --git a/asdl/bin/queue/QueueExample.class b/asdl/bin/queue/QueueExample.class new file mode 100644 index 0000000..e327d0d Binary files /dev/null and b/asdl/bin/queue/QueueExample.class differ diff --git a/asdl/bin/stack/ArrayListStack.class b/asdl/bin/stack/ArrayListStack.class new file mode 100644 index 0000000..819174a Binary files /dev/null and b/asdl/bin/stack/ArrayListStack.class differ diff --git a/asdl/bin/stack/MyStack.class b/asdl/bin/stack/MyStack.class new file mode 100644 index 0000000..0ef4d7d Binary files /dev/null and b/asdl/bin/stack/MyStack.class differ diff --git a/asdl/bin/stack/StackExample.class b/asdl/bin/stack/StackExample.class new file mode 100644 index 0000000..468872b Binary files /dev/null and b/asdl/bin/stack/StackExample.class differ diff --git a/asdl/bin/test/Main.class b/asdl/bin/test/Main.class new file mode 100644 index 0000000..3b26159 Binary files /dev/null and b/asdl/bin/test/Main.class differ diff --git a/asdl/bin/vettore_ordinabile/Main.class b/asdl/bin/vettore_ordinabile/Main.class new file mode 100644 index 0000000..524a38f Binary files /dev/null and b/asdl/bin/vettore_ordinabile/Main.class differ diff --git a/asdl/bin/vettore_ordinabile/VettoreOrdinabile.class b/asdl/bin/vettore_ordinabile/VettoreOrdinabile.class new file mode 100644 index 0000000..60e052d Binary files /dev/null and b/asdl/bin/vettore_ordinabile/VettoreOrdinabile.class differ diff --git a/asdl/bin/vettore_ordinabile/VettoriIntero.class b/asdl/bin/vettore_ordinabile/VettoriIntero.class new file mode 100644 index 0000000..255bf43 Binary files /dev/null and b/asdl/bin/vettore_ordinabile/VettoriIntero.class differ diff --git a/asdl/src/binary_tree/BinaryNode.java b/asdl/src/binary_tree/BinaryNode.java new file mode 100644 index 0000000..fe6b59d --- /dev/null +++ b/asdl/src/binary_tree/BinaryNode.java @@ -0,0 +1,148 @@ +package binary_tree; + +public class BinaryNode { + + // VARIABILI + + private BinaryNode left, right, parent; + private E data; + + // COSTRUTTORI + + public BinaryNode(E data) { + this.data = data; + + left = right = parent = null; + } + + public BinaryNode(BinaryNode left, E data, BinaryNode right) { + this.data = data; + + this.left = left; + this.right = right; + + this.parent = null; + + if (this.left != null) this.left.parent = this; + if (this.right != null) this.right.parent = this; + } + + // METODI + + public E getData() { + return data; + } + + public BinaryNode getLeft() { + return left; + } + + public BinaryNode getRight() { + return right; + } + + public BinaryNode getParent() { + return parent; + } + + public boolean isRoot() { + return parent == null; + } + + public boolean isLeftChild() { + return parent != null && parent.getLeft() == this; + } + + public boolean isRightChild() { + return parent != null && parent.getRight() == this; + } + + public void setData(E data) { + this.data = data; + } + + public void setLeft(BinaryNode left) { + this.left = left; + if (this.left != null) this.left.parent = this; + } + + public void setRight(BinaryNode right) { + this.right = right; + if (this.right != null) this.right.parent = this; + } + + public BinaryNode setParentAsLeftChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldLeft = this.parent.left; + this.parent.left = this; + + return oldLeft; + } + + public BinaryNode setParentAsRightChild(BinaryNode parent) { + if (parent == null) return null; + + this.parent = parent; + BinaryNode oldRight = this.parent.right; + this.parent.right = this; + + return oldRight; + } + + public BinaryNode setAsRoot() { + if (parent == null) return null; + + BinaryNode oldParent = parent; + parent = null; + + if (oldParent.left == this) oldParent.left = null; + else oldParent.right = null; + + return oldParent; + } + + public boolean hasLeft() { + return left != null; + } + + public boolean hasRight() { + return right != null; + } + + @Override + public boolean equals(Object other) { + if (other == null) return false; + if (other == this) return true; + if (!(other instanceof BinaryNode)) return false; + + BinaryNode otherType = (BinaryNode)other; + + if (!data.equals(otherType.data) || hasLeft() != otherType.hasLeft() || hasRight() != otherType.hasRight()) return false; + if (this.left != null && !left.equals(otherType.left)) return false; + if (this.right != null && !right.equals(otherType.right)) return false; + return true; + } + + public int levelOf() { + int d = 0; + BinaryNode cur = this; + + while (cur.parent != null){ + d++; + cur = cur.parent; + } + + return d; + } + + public int height() { + if (left == null && right == null) return 0; + + int hLeft = (left == null) ? 0 : left.height(); + int hRight = (right == null) ? 0 : right.height(); + + return Math.max(hLeft, hRight) + 1; + } +} diff --git a/asdl/src/binary_tree/BinaryTree.java b/asdl/src/binary_tree/BinaryTree.java new file mode 100644 index 0000000..7b9e8d2 --- /dev/null +++ b/asdl/src/binary_tree/BinaryTree.java @@ -0,0 +1,27 @@ +package binary_tree; + +import java.util.Iterator; + +public interface BinaryTree { + + public boolean isEmpty(); + + public int size(); + + public void clear(); + + public E getRoot(); + + public boolean contains(E targetElement); + + public Iterator iteratorInOrder(); + + public Iterator iteratorPreOrder(); + + public Iterator iteratorPostOrder(); + + public Iterator iteratorLevelOrder(); + + public String toString(); + +} diff --git a/asdl/src/binary_tree/ExerciseBinaryTree.java b/asdl/src/binary_tree/ExerciseBinaryTree.java new file mode 100644 index 0000000..f22aaef --- /dev/null +++ b/asdl/src/binary_tree/ExerciseBinaryTree.java @@ -0,0 +1,55 @@ +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/asdl/src/binary_tree/LinkedBinaryTree.java b/asdl/src/binary_tree/LinkedBinaryTree.java new file mode 100644 index 0000000..d1bc93c --- /dev/null +++ b/asdl/src/binary_tree/LinkedBinaryTree.java @@ -0,0 +1,520 @@ +package binary_tree; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.Stack; + +public class LinkedBinaryTree implements BinaryTree{ + + // VARIABILI D'INSTANZA + private BinaryNode root; + private int size; + + // Metodi costruttori + public LinkedBinaryTree() { + root = null; + size = 0; + } + + public LinkedBinaryTree(E data) { + root = new BinaryNode(data); + size = 1; + } + + public LinkedBinaryTree(LinkedBinaryTree left, E data, LinkedBinaryTree right) { + root = new BinaryNode(left.root, data, right.root); + size = 1 + left.size + right.size; + } + + // METODI + + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return root == null; + } + + public E getRoot() { + if (isEmpty()) return null; + return root.getData(); + } + + public BinaryNode getRootNode() { + if (isEmpty()) return null; + return root; + } + + public void clear() { + root = null; + size = 0; + } + + private int getSize(BinaryNode node) { + if (node == null) return 0; + + int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft()); + int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight()); + + return 1 + nLeft + nRight; + } + + public LinkedBinaryTree removeLeft() { + LinkedBinaryTree leftTree = null; + if (root.getLeft() == null) return leftTree; + + leftTree = new LinkedBinaryTree(); + leftTree.root = root.getLeft(); + leftTree.size = getSize(root.getLeft()); + leftTree.root.setAsRoot(); + + size = size - leftTree.size; + + return leftTree; + } + + public LinkedBinaryTree removeRight() { + LinkedBinaryTree rightTree = null; + if (root.getRight() == null) return rightTree; + + rightTree = new LinkedBinaryTree(); + rightTree.root = root.getRight(); + rightTree.size = getSize(root.getRight()); + rightTree.root.setAsRoot(); + + size = size - rightTree.size; + + return rightTree; + } + + protected BinaryNode find(E targetElement, BinaryNode root) { + if (root == null) return null; + if (root.getData().equals(targetElement)) return root; + BinaryNode resNode; + resNode = find(targetElement, root.getLeft()); + if (resNode == null) resNode = find(targetElement, root.getRight()); + return resNode; + } + + public boolean remove(E targetElement) { + if (targetElement == null) return false; + BinaryNode temp = find(targetElement, root); + if (temp != null) { + temp.setData(null); + return true; + } + return false; + } + + public boolean contains(E targetElement) { + return find(targetElement, root) != null; + } + + // ITERATORE PREORDER + + /* + * Qui si utilizza il metodo del preorder. + * Le regole per il preorder sono queste: + * - Visita prima se stesso + * - Poi visita tutto ciò che si trova sinistra + * - Infine visita tutto ciò che si trova a destra + */ + + protected void preorder (BinaryNode node, List temporaryList) { + if (node == null) return; + + if (node.getData() != null) temporaryList.add(node.getData()); + + preorder(node.getLeft(), temporaryList); + preorder(node.getRight(), temporaryList); + } + + public Iterator iteratorPreOrder() { + ArrayList temporaryList = new ArrayList(); + preorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE INORDER + + /* + * Qui si utilizza il metodo del inorder. + * Le regole per l'inorder sono queste: + * - Visita prima tutto ciò che si trova a sinistra + * - Poi visita se stesso + * - Infine visita tutto ciò che si trova a destra + */ + + protected void inorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + inorder(node.getLeft(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + + inorder(node.getRight(), temporaryList); + } + + public Iterator iteratorInOrder() { + ArrayList temporaryList = new ArrayList(); + inorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE POSTORDER + + /* + * Qui si utilizza il metodo postorder. + * Le regole per il postorder sono queste: + * - Visita prima di tutto ciò che si trova a sinistra + * - Poi visita tutto ciò che si trova a destra + * - Infine visita il sestesso + */ + + protected void postorder(BinaryNode node, List temporaryList) { + if (node == null) return; + + postorder(node.getLeft(), temporaryList); + postorder(node.getRight(), temporaryList); + + if (node.getData() != null) temporaryList.add(node.getData()); + } + + public Iterator iteratorPostOrder() { + ArrayList temporaryList = new ArrayList(); + postorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE LEVEL ORDER + + /* + * Qui si usa il metodo levelorder. + * Si analizza tutto il livello dell'albero. + * Per chiarire di seguito un esempio: + * + * A + * / \ + * B C + * / \ \ + * D E F + * + * La lettura avviene quindi in questo modo: + * A - B - C - D - E - F + * + * Si adopera una coda (Queue) di tipo FI-FO (First In - First Out) + */ + + protected void levelorder(BinaryNode node, List temporaryList) { + Queue> queueOfNodes = new LinkedList>(); + BinaryNode current; + + queueOfNodes.add(node); + while(!queueOfNodes.isEmpty()) { + current = queueOfNodes.remove(); + + if (current.getData() != null) temporaryList.add(current.getData()); + + if (current.getLeft() != null) queueOfNodes.add(current.getLeft()); + if (current.getRight() != null) queueOfNodes.add(current.getRight()); + } + } + + public Iterator iteratorLevelOrder() { + ArrayList temporaryList = new ArrayList(); + levelorder(root, temporaryList); + return temporaryList.iterator(); + } + + // ITERATORE + + public Iterator iterator() { + return iteratorPreOrder(); + } + + // ITERATORE PREORDER ITERATIVO (NON RICORSIVO) + + /* + * In questo caso stiamo usando un tipo stack. + * Dobbiamo di fatto simulare uno stack se vogliamo + * iterativamente fare questa cosa. + * + * Perchè se prima dobbiamo visitare il nodo centrale, + * poi il nodo a sinistra e poi il nodo a destra inseriamo + * prima quello di destra e poi quello di sinistra? + * + * Lo stack di fatto è una coda LIFO, ovvero last in - first out + * e quindi l'ultimo che entra è il primo che esce. + * + * dal momento che vale questa cosa, se facciamo entrare prima quello + * di destra e poi quello di sinistra, il primo nodo che verrà + * prelevato è ovviamente quello a sinistra. + */ + protected void itpreorder (BinaryNode node, List temporaryList) { + Stack> queueOfNodes = new Stack>(); + BinaryNode current; + + queueOfNodes.add(node); + while (!queueOfNodes.isEmpty()) { + current = queueOfNodes.pop(); + + if (current.getData() != null) temporaryList.add(current.getData()); + // Come già spiegato pushamo prima il nodo di destra + 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); + size++; + } + } + +} diff --git a/asdl/src/comparatori/videogioco/ComparatorVideogioco1.java b/asdl/src/comparatori/videogioco/ComparatorVideogioco1.java new file mode 100644 index 0000000..450be6c --- /dev/null +++ b/asdl/src/comparatori/videogioco/ComparatorVideogioco1.java @@ -0,0 +1,15 @@ +package comparatori.videogioco; + +import java.util.Comparator; + +public class ComparatorVideogioco1 implements Comparator { + + /** + * Ordina i videogiochi per titolo alfabetico crescente + */ + @Override + public int compare(Videogioco v1, Videogioco v2) { + return v1.titolo().compareTo(v2.titolo()); + } + +} diff --git a/asdl/src/comparatori/videogioco/ComparatorVideogioco2.java b/asdl/src/comparatori/videogioco/ComparatorVideogioco2.java new file mode 100644 index 0000000..4d63cd5 --- /dev/null +++ b/asdl/src/comparatori/videogioco/ComparatorVideogioco2.java @@ -0,0 +1,16 @@ +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) { + return v2.voto().compareTo(v1.voto()); + } + +} diff --git a/asdl/src/comparatori/videogioco/ComparatorVideogioco3.java b/asdl/src/comparatori/videogioco/ComparatorVideogioco3.java new file mode 100644 index 0000000..55faff3 --- /dev/null +++ b/asdl/src/comparatori/videogioco/ComparatorVideogioco3.java @@ -0,0 +1,22 @@ +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; + return v1.titolo().compareTo(v2.titolo()); + } + +} diff --git a/asdl/src/comparatori/videogioco/Main.java b/asdl/src/comparatori/videogioco/Main.java new file mode 100644 index 0000000..07c13db --- /dev/null +++ b/asdl/src/comparatori/videogioco/Main.java @@ -0,0 +1,58 @@ +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); + System.out.println(); + + // 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); + System.out.println(); + + // Multi + System.out.println("Multi"); + Collections.sort(giochi, new ComparatorVideogioco3()); + printList(giochi); + System.out.println(); + + } + + 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/asdl/src/comparatori/videogioco/Videogioco.java b/asdl/src/comparatori/videogioco/Videogioco.java new file mode 100644 index 0000000..0e07468 --- /dev/null +++ b/asdl/src/comparatori/videogioco/Videogioco.java @@ -0,0 +1,66 @@ +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 titolo + " " + piattaforma + " " + annoUscita + " " + prezzo + " " + voto; + } +} diff --git a/asdl/src/jcf_set/example/InteriRipetuti.java b/asdl/src/jcf_set/example/InteriRipetuti.java new file mode 100644 index 0000000..b3560c8 --- /dev/null +++ b/asdl/src/jcf_set/example/InteriRipetuti.java @@ -0,0 +1,44 @@ +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(treeset); + } + +} diff --git a/asdl/src/jcf_set/example/TreeSetExample1.java b/asdl/src/jcf_set/example/TreeSetExample1.java new file mode 100644 index 0000000..9f438ce --- /dev/null +++ b/asdl/src/jcf_set/example/TreeSetExample1.java @@ -0,0 +1,37 @@ +package jcf_set.example; + +import java.util.Comparator; +import java.util.TreeSet; + +public class TreeSetExample1 { + + public static void main(String[] args) { + + // Applchiamo l'ordine su un insieme matematico mediante la nostra regola di ordinamento mediante comparator + TreeSet ts = new TreeSet(new Decreasing()); + + ts.add("C"); + ts.add("A"); + ts.add("B"); + ts.add("E"); + ts.add("F"); + ts.add("D"); + + System.out.println("Stampa del TreeSet: "); + System.out.println(ts); + + // Verifica se la TreeSet è vuota o meno + if (ts.isEmpty()) System.out.println("TreeSet è vuoto"); + else System.out.println("TreeSet ha [" + ts.size() + "] elementi"); + } + +} + +class Decreasing implements Comparator{ + + @Override + public int compare(String s1, String s2) { + return s2.compareTo(s1); + } + +} \ No newline at end of file diff --git a/asdl/src/jcf_set/example/TreeSetExample2.java b/asdl/src/jcf_set/example/TreeSetExample2.java new file mode 100644 index 0000000..df462b5 --- /dev/null +++ b/asdl/src/jcf_set/example/TreeSetExample2.java @@ -0,0 +1,40 @@ +package jcf_set.example; + +import java.util.TreeSet; + +public class TreeSetExample2 { + + public static void main(String[] Args) { + new TreeSetExample2().run(); + } + + public void run() { + + // Tree1 + TreeSet tree1 = new TreeSet(); + + tree1.add("yes"); + tree1.add("no"); + tree1.add("maybe"); + tree1.add("always"); + tree1.add("no"); + + System.out.println("Stampa del TreeSet1: "); + System.out.println(tree1); + + // Tree2 + TreeSet tree2 = new TreeSet(tree1); + + System.out.println("Stampa del TreeSet2: "); + System.out.println(tree2); + + // Tree3 + TreeSet tree3 = new TreeSet(new Decreasing()); + tree3.addAll(tree2); + + System.out.println("Stampa del TreeSet3: "); + System.out.println(tree3); + + } + +} diff --git a/asdl/src/module-info.java b/asdl/src/module-info.java new file mode 100644 index 0000000..4e0fed7 --- /dev/null +++ b/asdl/src/module-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * + */ +module asdl { +} \ No newline at end of file diff --git a/asdl/src/parziale/p251110/Clinica.java b/asdl/src/parziale/p251110/Clinica.java new file mode 100644 index 0000000..c9d3289 --- /dev/null +++ b/asdl/src/parziale/p251110/Clinica.java @@ -0,0 +1,46 @@ +package parziale.p251110; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.ListIterator; + +public class Clinica { + + ArrayList pazienti = new ArrayList(); + + // 1. Numero di pazienti Ricoverati + public int pazientiRicoverati() { + return pazienti.size(); + } + + // 2. Inserimento del paziente + /* + * Dal momento che non ci è permesso adoperare + * TreeSet per l'ordine e per l'unicità + */ + public boolean ricoveraPaziente(Paziente paziente) { + // Se il paziente è nullo, nulla verrà aggiunto + if (paziente == null) return false; + + // Controllo se il paziente è già presente nella lista ricoveri + Iterator it = pazienti.iterator(); + while (it.hasNext()) { + Paziente current = it.next(); + if (paziente.equals(current)) return false; // Se è già presente un paziente con id uguale allora non viene aggiunto e ritorna false + } + + // Aggiunta del paziente + ListIterator lit = pazienti.listIterator(); + while (lit.hasNext()) { + Paziente current = lit.next(); + int cmp = current.compareTo(paziente); + if (cmp > 0) { + lit.add(paziente); + return true; + } + } + + return false; + } + +} diff --git a/asdl/src/parziale/p251110/Paziente.java b/asdl/src/parziale/p251110/Paziente.java new file mode 100644 index 0000000..76aca29 --- /dev/null +++ b/asdl/src/parziale/p251110/Paziente.java @@ -0,0 +1,41 @@ +package parziale.p251110; + +public class Paziente implements Comparable { + + private String id; + private int annoNascita; + + public Paziente( + String id, + int annoNascita) { + this.id = id; + this.annoNascita = annoNascita; + } + + public String getId() { + return id; + } + + public int getAnnoNascita() { + return annoNascita; + } + + @Override + public boolean equals(Object o) { + if (o == null) return false; + if (this == o) return true; + if (!(o instanceof Paziente)) return false; + + Paziente p = (Paziente) o; + + return id.equals(p.id); + } + + @Override + public int compareTo(Paziente p) { + if (annoNascita < p.annoNascita) return -1; + if (annoNascita > p.annoNascita) return 1; + return id.compareTo(p.id); + } + +} diff --git a/asdl/src/queue/ArrayListQueue.java b/asdl/src/queue/ArrayListQueue.java new file mode 100644 index 0000000..ebfeb3b --- /dev/null +++ b/asdl/src/queue/ArrayListQueue.java @@ -0,0 +1,34 @@ +package queue; + +import java.util.ArrayList; +import java.util.List; + +public class ArrayListQueue implements MyQueue { + + private List queue = new ArrayList(); + + @Override + public boolean offer(T item) { + return queue.add(item); + } + + @Override + public T remove() { + return queue.remove(0); + } + + @Override + public T peek() { + return queue.get(0); + } + + @Override + public int size() { + return queue.size(); + } + + @Override + public boolean isEmpty() { + return queue.isEmpty(); + } +} diff --git a/asdl/src/queue/MyQueue.java b/asdl/src/queue/MyQueue.java new file mode 100644 index 0000000..94fc1df --- /dev/null +++ b/asdl/src/queue/MyQueue.java @@ -0,0 +1,35 @@ +package queue; + +public interface MyQueue { + + /** + * Aggiungi l'elemento specificato in fondo alla coda + * @param item + */ + boolean offer(T item); + + /** + * Rimuove l'elemento in testa alla coda e lo restituisce + * @return + */ + T remove(); + + /** + * Restituisce l'elemento in cima alla coda senza rimuoverlo. + * @return + */ + T peek(); + + /** + * Restituisce l'elemento il numero di elementi correnti nella coda. + * @return + */ + int size(); + + /** + * Restituisce se la coda è vuota o meno. + * @return + */ + boolean isEmpty(); + +} diff --git a/asdl/src/queue/QueueExample.java b/asdl/src/queue/QueueExample.java new file mode 100644 index 0000000..74cb44a --- /dev/null +++ b/asdl/src/queue/QueueExample.java @@ -0,0 +1,25 @@ +package queue; + +public class QueueExample { + + public static void main(String[] Args) { + + MyQueue queue = new ArrayListQueue(); + + Character a = 'a'; + Character b = 'b'; + Character d = 'd'; + Character c = 'c'; + + queue.offer(a); + queue.offer(b); + queue.offer(c); + queue.offer(d); + + for (int i = queue.size(); i > 0; i--) { + System.out.println(queue.remove()); + } + + } + +} diff --git a/asdl/src/stack/ArrayListStack.java b/asdl/src/stack/ArrayListStack.java new file mode 100644 index 0000000..3217956 --- /dev/null +++ b/asdl/src/stack/ArrayListStack.java @@ -0,0 +1,33 @@ +package stack; + +import java.util.ArrayList; + +public class ArrayListStack implements MyStack { + + private ArrayList array = new ArrayList(); + + @Override + public void push(T item) { + array.add(item); + } + + @Override + public T pop() { + return array.remove(array.size() - 1); + } + + @Override + public T peek() { + return array.get(array.size() - 1); + } + + @Override + public int size() { + return array.size(); + } + + @Override + public boolean isEmpty() { + return array.isEmpty(); + } +} diff --git a/asdl/src/stack/MyStack.java b/asdl/src/stack/MyStack.java new file mode 100644 index 0000000..eedea36 --- /dev/null +++ b/asdl/src/stack/MyStack.java @@ -0,0 +1,34 @@ +package stack; + +public interface MyStack { + + /** + * Aggiunge un elemento al top dello stack. + * @param Elemento che deve essere aggiunto nello stack. + */ + void push(T item); + + /** + * Rimuove l'elemento al top dello stack. + * @return Elemento appena rimosso dallo stack. + */ + T pop(); + + /** + * Ritorna l'elemento al top dello stack ma senza rimuoverlo. + * @return Elemento al top dello stack. + */ + T peek(); + + /** + * Ritorna il numero di elementi correnti nello stack. + * @return Numero di elementi nello stack. + */ + int size(); + + /** + * Ritorna se ci sono elementi nello stack o meno. + * @return Se è vuoto o meno. + */ + boolean isEmpty(); +} diff --git a/asdl/src/stack/StackExample.java b/asdl/src/stack/StackExample.java new file mode 100644 index 0000000..be6ee45 --- /dev/null +++ b/asdl/src/stack/StackExample.java @@ -0,0 +1,26 @@ +package stack; + +public class StackExample { + + public static void main(String[] Args) { + + MyStack stack = new ArrayListStack(); + + Character a = 'a'; + Character b = 'b'; + Character c = 'c'; + Character d = 'd'; + + stack.push(a); + stack.push(b); + stack.push(c); + stack.push(d); + + // Svuotiamo lo stack + for (int i = stack.size(); i > 0; i--) { + Character current = stack.pop(); + System.out.println(current); + } + } + +} diff --git a/asdl/src/test/Main.java b/asdl/src/test/Main.java new file mode 100644 index 0000000..a6cbbf6 --- /dev/null +++ b/asdl/src/test/Main.java @@ -0,0 +1,9 @@ +package test; + +public class Main { + + public static void main (String[] Args) { + System.out.println("Test"); + } + +} diff --git a/asdl/src/vettore_ordinabile/Main.java b/asdl/src/vettore_ordinabile/Main.java new file mode 100644 index 0000000..139aad5 --- /dev/null +++ b/asdl/src/vettore_ordinabile/Main.java @@ -0,0 +1,24 @@ +package vettore_ordinabile; + +public class Main { + + public static void main (String[] Args) { + + System.out.println("\nTest VettoreIntero"); + Integer i1 = Integer.valueOf(3); + Integer i2 = Integer.valueOf(389); + Integer i3 = Integer.valueOf(15); + Integer i4 = Integer.valueOf(10); + + VettoriIntero VI = new VettoriIntero(4); + + VI.aggiungi(i1); + VI.aggiungi(i2); + VI.aggiungi(i3); + VI.aggiungi(i4); + + VI.visualizza(); + + } + +} diff --git a/asdl/src/vettore_ordinabile/VettoreOrdinabile.java b/asdl/src/vettore_ordinabile/VettoreOrdinabile.java new file mode 100644 index 0000000..e30a8e8 --- /dev/null +++ b/asdl/src/vettore_ordinabile/VettoreOrdinabile.java @@ -0,0 +1,80 @@ +package vettore_ordinabile; + +public abstract class VettoreOrdinabile { + + // Variabili di istanza + private Object[] vettore; // Vettore dove memorizzare i dati + private int dimensioneMassima; + private int dimensioneCorrente; + + // Costruttore + public VettoreOrdinabile(int dimensioneMassima) { + if (dimensioneMassima < 0) throw new IllegalArgumentException("La dimensione non può essere negativa"); + this.dimensioneMassima = dimensioneMassima; + this.vettore = new Object[dimensioneMassima]; + } + + // FUNZIONI DI ISTANZA + + /** + * Funzione aggiungi. + * Permette di aggiungere un elemento se la dimensione lo permette. + */ + public boolean aggiungi(Object elemento) { + if (elemento != null && dimensioneCorrente < dimensioneMassima) { + vettore[dimensioneCorrente] = elemento; + dimensioneCorrente++; + return true; + } else return false; + } + + /** + * Funzione leggi. + * Permette di leggere un elemento ad una determinata posizione. + */ + public Object leggi(int indice) { + if (indice >= 0 && indice < dimensioneCorrente) { + return vettore[indice]; + } else return null; + } + + /** + * Funzione che restituisce la dimensione corrente + */ + public int dimensione() { + return dimensioneCorrente; + } + + /** + * Funzione che visualizza l'array + */ + public void visualizza() { + this.ordina(); + for (int i = 0; i < this.dimensioneMassima; i++) { + System.out.println(leggi(i)); + } + } + + /** + * Funzione che ordina l'array + */ + public void ordina() { + for (int i = 0; i < dimensioneCorrente; i++) { + int minimo = i; + int j; + for (j = i+1; j < dimensioneCorrente; j++) { + if (confronta(vettore[minimo], vettore[j]) > 0) minimo = j; + } + Object temp = vettore[minimo]; + vettore[minimo] = vettore[i]; + vettore[i] = temp; + } + } + + // FUNZIONI ASTRATTE + + /** + * Funzione che restituisce il valore della comparazione + */ + protected abstract int confronta(Object elemento1, Object elemento2); +} diff --git a/asdl/src/vettore_ordinabile/VettoriIntero.java b/asdl/src/vettore_ordinabile/VettoriIntero.java new file mode 100644 index 0000000..f323cff --- /dev/null +++ b/asdl/src/vettore_ordinabile/VettoriIntero.java @@ -0,0 +1,55 @@ +package vettore_ordinabile; + +public class VettoriIntero extends VettoreOrdinabile{ + + // Metodo costruttore default + /* + * Contiene solamente 10 elementi + * */ + public VettoriIntero() { + super(10); + } + + // Metodo costruttore custom + /* + * Contiene n elementi + */ + public VettoriIntero(int dimensioneMassima) { + super(dimensioneMassima); + } + + // Metodo aggiungi + /* + * Questo metodo è un metodo bloccante in modo che + * non aggiungino oggetti che non siano di tipo Integer + */ + @Override + public boolean aggiungi(Object object) { + return false; + } + + // Metodo aggiungi + /* + * Questo metodo aggiunge realmente l'elemento di tipo + * Integer + */ + public boolean aggiungi(Integer integer) { + return super.aggiungi(integer); + } + + // Metodo ordina + /* + * Questo metodo permette il funzionamento dell'ordinamento + * tramite comparatore simulato + */ + @Override + protected int confronta(Object elemento1, Object elemento2) { + Integer i1 = (Integer)elemento1; + Integer i2 = (Integer)elemento2; + + if (i1 < i2) return -1; + if (i1 > i2) return 1; + return 0; + } + +}