Compare commits
9 Commits
29ef5b6655
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 75d3f924eb | |||
| 5736e5ec0a | |||
| 2a51a8993d | |||
| ddf329482a | |||
| cec7009044 | |||
| 66f053aba3 | |||
| 30b66dd99b | |||
| 646f4dc077 | |||
| e4d2eb3ab8 |
@@ -0,0 +1,3 @@
|
||||
/.metadata/
|
||||
asdl/bin/
|
||||
*.class
|
||||
@@ -1,4 +1,5 @@
|
||||
#Sun May 10 01:31:23 CEST 2026
|
||||
host=DESKTOP-ALBQHEK
|
||||
process-id=13140
|
||||
user=eslusarz
|
||||
#Sat May 23 20:39:23 CEST 2026
|
||||
display=\:0
|
||||
host=rawmilk
|
||||
process-id=7482
|
||||
user=rawmilk
|
||||
|
||||
+2472
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
[ {
|
||||
"version" : "9.6.0-20260502004807+0000",
|
||||
"buildTime" : "20260502004807+0000",
|
||||
"commitId" : "b6e68b2bcae07ceceb366b4d0a900e4e93ba4c1d",
|
||||
"version" : "9.7.0-20260523064614+0000",
|
||||
"buildTime" : "20260523064614+0000",
|
||||
"commitId" : "531c48a8258a99d5a1e101b1eaaa6ac41a161c4e",
|
||||
"current" : false,
|
||||
"snapshot" : true,
|
||||
"nightly" : true,
|
||||
@@ -10,16 +10,84 @@
|
||||
"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",
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.7.0-20260523064614+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.7.0-20260523064614+0000-bin.zip.sha256",
|
||||
"checksum" : "f651cacdccd14c8e3e4bb8a5a8c18e27c9badae833fbd88bd82c12e077fbc129",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.7.0-20260523064614+0000-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.6.0-20260523053203+0000",
|
||||
"buildTime" : "20260523053203+0000",
|
||||
"commitId" : "14406b4a865693d1a0dc0e1d9df2503f9322a753",
|
||||
"current" : false,
|
||||
"snapshot" : true,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : true,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260523053203+0000-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260523053203+0000-bin.zip.sha256",
|
||||
"checksum" : "6299d5aee12cab39f3b3bb2e7e0bfeee1d4f4ffbbf4dccc3c38a8e969b27fa70",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions-snapshots/gradle-9.6.0-20260523053203+0000-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.5.1",
|
||||
"buildTime" : "20260512131942+0000",
|
||||
"commitId" : "fd78213f09782e62ca4957f9cfd3d90c6c3f1767",
|
||||
"current" : true,
|
||||
"snapshot" : false,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : false,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions/gradle-9.5.1-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.1-bin.zip.sha256",
|
||||
"checksum" : "bafc141b619ad6350fd975fc903156dd5c151998cc8b058e8c1044ab5f7b031f",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.5.1-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "9.6.0-milestone-2",
|
||||
"buildTime" : "20260510110612+0000",
|
||||
"commitId" : "62000451ad7b25de53fa89a155ef8ecb401621bb",
|
||||
"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-2-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-2-bin.zip.sha256",
|
||||
"checksum" : "a67385a5e8acaed7840b5114feb76e455f6e3567e02d4414726f6fd1179753a9",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-9.6.0-milestone-2-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "497c8c2a7e5031f6aa847f88104aa80a93532ec32ee17bdb8d1d2f67a194a9c7"
|
||||
}, {
|
||||
"version" : "8.14.5",
|
||||
"buildTime" : "20260507110329+0000",
|
||||
"commitId" : "62345becae08b13e793521816d585102fea66398",
|
||||
"current" : false,
|
||||
"snapshot" : false,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : false,
|
||||
"activeRc" : false,
|
||||
"rcFor" : "",
|
||||
"milestoneFor" : "",
|
||||
"broken" : false,
|
||||
"downloadUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-bin.zip",
|
||||
"checksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-bin.zip.sha256",
|
||||
"checksum" : "6f74b601422d6d6fc4e1f9a1ab6522f642c2fdcbc15ae33ebd30ba3d7198e854",
|
||||
"wrapperChecksumUrl" : "https://services.gradle.org/distributions/gradle-8.14.5-wrapper.jar.sha256",
|
||||
"wrapperChecksum" : "7d3a4ac4de1c32b59bc6a4eb8ecb8e612ccd0cf1ae1e99f66902da64df296172"
|
||||
}, {
|
||||
"version" : "9.5.0",
|
||||
"buildTime" : "20260428120530+0000",
|
||||
"commitId" : "3fe117d68f3907790f3809f121aa36303a9151f8",
|
||||
"current" : true,
|
||||
"current" : false,
|
||||
"snapshot" : false,
|
||||
"nightly" : false,
|
||||
"releaseNightly" : false,
|
||||
@@ -32,23 +100,6 @@
|
||||
"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",
|
||||
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < size; i++) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T>
|
||||
}
|
||||
|
||||
}
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
Node<E> currentNode = head;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-305
@@ -1,305 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
|
||||
* destra
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while () {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>();
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-491
@@ -1,491 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public int numberLeaf() {
|
||||
Integer number = 0;
|
||||
numberLeaf(root, number);
|
||||
return number;
|
||||
}
|
||||
|
||||
public int numberLeaf(BinaryNode<E> node) {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
-327
@@ -1,327 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
|
||||
* destra
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-317
@@ -1,317 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
|
||||
* destra
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-92
@@ -1,92 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
}
|
||||
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
|
||||
|
||||
// METODI
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+414
@@ -0,0 +1,414 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-468
@@ -1,468 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package jcf_set.example;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
public class InteriRipetuti {
|
||||
|
||||
public static void main(String[] Args) {
|
||||
new InteriRipetuti().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int N = 20;
|
||||
int MAX_INT = 50;
|
||||
|
||||
// HASHSET
|
||||
Set<Integer> hashset = new HashSet<Integer>();
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-129
@@ -1,129 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData != null)
|
||||
}
|
||||
|
||||
}
|
||||
-78
@@ -1,78 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package vettore_ordinabile;
|
||||
|
||||
public class VettoriIntero {
|
||||
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T
|
||||
}
|
||||
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
}
|
||||
|
||||
}
|
||||
-307
@@ -1,307 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
|
||||
* destra
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if ()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-439
@@ -1,439 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
BinaryNode<E> currentNode = root;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
E currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
currentNode.setData(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
|
||||
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
|
||||
if (citta == null) throw new NullPointerException();
|
||||
|
||||
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
|
||||
|
||||
for (String s : archivio.keySet()) {
|
||||
Cliente currentCliente = archivio.get(s);
|
||||
String currentCitta = currentCliente.cittaResidenza();
|
||||
|
||||
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
|
||||
}
|
||||
|
||||
return autoSelezionate;
|
||||
}
|
||||
|
||||
// 5. Ordinamento dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili
|
||||
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
|
||||
TreeSet<String, Cliente> archivioOrdinato = new TreeSet<String, Cliente>();
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in un'altra città.
|
||||
|
||||
|
||||
}
|
||||
+225
@@ -0,0 +1,225 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
-492
@@ -1,492 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public int numberLeaf() {
|
||||
Integer number = 0;
|
||||
numberLeaf(root, number);
|
||||
return number;
|
||||
}
|
||||
|
||||
public int numberLeaf(BinaryNode<E> node) {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
-91
@@ -1,91 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldLeft = this.parent.left;
|
||||
this.parent.left = this;
|
||||
|
||||
return oldLeft;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldRight = this.parent.right;
|
||||
}
|
||||
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package comparatori.videogioco;
|
||||
|
||||
public class Videogioco implements Comparable<Videogioco>{
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-68
@@ -1,68 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree.root = root.getLeft();
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class ExerciseBinaryTree {
|
||||
|
||||
|
||||
/*
|
||||
* Esercizio 1 II parziale 10.01.2018
|
||||
* Un albero binario di interi si dice pari se tutti i nodi contengono
|
||||
* un intero positivo pari. Realizzare il metodo statico
|
||||
* public static boolean IsEvenBtree (BinaryTree<Integer> btree)
|
||||
* che verifica se l'albero binario dato btree e' pari.
|
||||
*/
|
||||
public static boolean isEvenBtree(BinaryTree<Integer> btree) {
|
||||
boolean even = true;
|
||||
}
|
||||
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
// Insiemi di test
|
||||
Integer[] aArray = {1, -2, 3, -4, 3};
|
||||
Integer[] bArray = {1, 5, -6, -4, 5};
|
||||
|
||||
// Input come HashSet (Ordine non garantito)
|
||||
Set<Integer> aHashSet = new HashSet<Integer>(Arrays.asList(aArray));
|
||||
Set<Integer> bHashSet = new HashSet<Integer>(Arrays.asList(bArray));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Operazioni di insiemistica
|
||||
|
||||
|
||||
}
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
// Insiemi di test
|
||||
Integer[] aArray = {1, -2, 3, -4, 3};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-74
@@ -1,74 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
}
|
||||
-488
@@ -1,488 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public void numberLeaf() {
|
||||
Integer number = 0;
|
||||
numberLeaf(root, number);
|
||||
}
|
||||
|
||||
public void numberLeaf(BinaryNode<E> node, Integer number) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) number++;
|
||||
else {
|
||||
numberLeaf(node.getLeft(), number);
|
||||
numberLeaf(node.getRight(), number);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package comparatori.videogioco;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] Args) {
|
||||
|
||||
ArrayList<Videogioco> giochi = new ArrayList<Videogioco>();
|
||||
|
||||
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<T> 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("}");
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import binary_tree.BinaryNode;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
* Il risultato è il conteggio totale degli elementi che risultano duplicati
|
||||
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
|
||||
* hanno almeno un duplicato)
|
||||
*/
|
||||
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
|
||||
|
||||
}
|
||||
|
||||
protected static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node, HashMap<T, Integer> mappa) {
|
||||
if (node == null) return mappa;
|
||||
|
||||
if (node.getLeft() != null)
|
||||
}
|
||||
|
||||
}
|
||||
+195
@@ -0,0 +1,195 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if ()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-483
@@ -1,483 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public int numberLeaf() {
|
||||
Integer number = 0;
|
||||
numberLeaf(root, number);
|
||||
return number;
|
||||
}
|
||||
|
||||
public int numberLeaf(BinaryNode<E> node) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
}
|
||||
-79
@@ -1,79 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
if (this.parent == null) return null;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class CompanyMap {
|
||||
|
||||
/*
|
||||
* EX1
|
||||
* Supponiamo che chi vengano forniti il nome e il numero
|
||||
* di divisione di ciascun dipendente di un'azienda.
|
||||
* Non ci sono nomi duplicati. Vorremmo memorizzare
|
||||
* queste informazioni in ordine alfabetico per nome.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione: si intende adoperare chiaramente un TreeMap.
|
||||
* In modo che vi sia una associazione chiave-valore e un ordinamento basato
|
||||
* sulla chiave.
|
||||
* Chiave: String
|
||||
* Valore: Integer
|
||||
*/
|
||||
|
||||
TreeMap<>
|
||||
|
||||
}
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T>
|
||||
|
||||
}
|
||||
+273
@@ -0,0 +1,273 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext = 0;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-327
@@ -1,327 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra, il primo nodo da inserire è quello di
|
||||
* destra
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
stackOfNodes.push(current);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-138
@@ -1,138 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder();
|
||||
}
|
||||
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(List<T> ) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst(object);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in un'altra città.
|
||||
public Map<String, Paziente>
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import binary_tree.BinaryNode;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
* Il risultato è il conteggio totale degli elementi che risultano duplicati
|
||||
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
|
||||
* hanno almeno un duplicato)
|
||||
*/
|
||||
public static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node) {
|
||||
|
||||
}
|
||||
|
||||
protected static <T> Map<T, Integer> ContaDuplicati(BinaryNode<T> node, TreeMap<T, Integer> mappa) {
|
||||
if (node == null) return mappa;
|
||||
|
||||
if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa);
|
||||
if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa);
|
||||
|
||||
if (node.getData() == null) return mappa;
|
||||
|
||||
T data = node.getData();
|
||||
if (mappa.get(data) == null) mappa.put();
|
||||
}
|
||||
|
||||
}
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
}
|
||||
|
||||
}
|
||||
-84
@@ -1,84 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldLeft = this.parent.left;
|
||||
this.parent.left = this;
|
||||
|
||||
return oldLeft;
|
||||
}
|
||||
|
||||
}
|
||||
-248
@@ -1,248 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
protected void itpreorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
}
|
||||
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package jcf_set.example;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
public class InteriRipetuti {
|
||||
|
||||
public static void main(String[] Args) {
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int N = 20;
|
||||
int MAX_INT = 50;
|
||||
|
||||
// HASHSET
|
||||
Set<Integer> hashset = new HashSet<Integer>();
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-62
@@ -1,62 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+393
@@ -0,0 +1,393 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package test;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main (String[] Args) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-106
@@ -1,106 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
-128
@@ -1,128 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldLeft = this.parent.left;
|
||||
this.parent.left = this;
|
||||
|
||||
return oldLeft;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldRight = this.parent.right;
|
||||
this.parent.right = this;
|
||||
|
||||
return oldRight;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setAsRoot() {
|
||||
if (parent == null) return null;
|
||||
|
||||
BinaryNode<E> oldParent = parent;
|
||||
parent = null;
|
||||
|
||||
if (oldParent.left == this) oldParent.left = null;
|
||||
else oldParent.right = null;
|
||||
|
||||
return oldParent;
|
||||
}
|
||||
|
||||
public boolean hasLeft() {
|
||||
return left != null;
|
||||
}
|
||||
|
||||
public boolean hasRight() {
|
||||
return right != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null) return false;
|
||||
if (other == this) return true;
|
||||
if (!(other instanceof BinaryNode<?>)) return false;
|
||||
|
||||
BinaryNode<?> otherType = (BinaryNode<?>)other;
|
||||
|
||||
return this.left.equals(otherType.left)
|
||||
&& this.right.equals(otherType.right)
|
||||
&& this.parent.equals(otherType.parent)
|
||||
&& this.data.equals(otherType.data);
|
||||
}
|
||||
|
||||
}
|
||||
+386
@@ -0,0 +1,386 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
size--;
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
posNext--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-281
@@ -1,281 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
}
|
||||
|
||||
// ITERATOR
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Cliente {
|
||||
|
||||
// Variabili di instanza
|
||||
private String nominativo;
|
||||
private String cf;
|
||||
private String cittaResidenza;
|
||||
|
||||
// Costruttore
|
||||
public Cliente(
|
||||
String nominativo,
|
||||
String cf,
|
||||
String cittaResidenza) {
|
||||
this.nominativo = nominativo;
|
||||
this.cf = cf;
|
||||
this.cittaResidenza = cittaResidenza;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String nominativo() {
|
||||
return nominativo;
|
||||
}
|
||||
|
||||
public String cf() {
|
||||
return cf;
|
||||
}
|
||||
|
||||
public String cittaResidenza() {
|
||||
return cittaResidenza;
|
||||
}
|
||||
|
||||
// Hash
|
||||
public int hashCode() {
|
||||
return Objects.hash(nominativo, cf, cittaResidenza);
|
||||
}
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null)
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
|
||||
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
|
||||
if (citta == null) throw new NullPointerException();
|
||||
|
||||
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
|
||||
|
||||
for (String s : archivio.keySet()) {
|
||||
Cliente currentCliente = archivio.get(s);
|
||||
String currentCitta = currentCliente.cittaResidenza();
|
||||
|
||||
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
|
||||
}
|
||||
|
||||
return autoSelezionate;
|
||||
}
|
||||
|
||||
// 5. Ordinamento dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili
|
||||
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
|
||||
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(archivio);
|
||||
return archivioOrdinato;
|
||||
}
|
||||
|
||||
// 6. Stampa su video l'archivio attuale
|
||||
public void stampaArchivio() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-132
@@ -1,132 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
}
|
||||
-494
@@ -1,494 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public int numberLeaf() {
|
||||
return numberLeaf(root);
|
||||
}
|
||||
|
||||
public int numberLeaf(BinaryNode<E> 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<E> 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
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Dispari<E> {
|
||||
|
||||
/*
|
||||
* Esercizio 1 - Esame del 5 settembre 2017
|
||||
*
|
||||
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
|
||||
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
|
||||
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
|
||||
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
|
||||
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
|
||||
* e restituisce true se myList è dispari;
|
||||
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
|
||||
*/
|
||||
|
||||
// Variabile di instanza
|
||||
private LinkedList<E> myList = new LinkedList<E>();
|
||||
|
||||
// Metodo di esercizio
|
||||
public boolean isDispari() {
|
||||
boolean dispari = true;
|
||||
|
||||
// Se la lista è vuota allora returna dispari
|
||||
if (myList.isEmpty()) return dispari;
|
||||
|
||||
// Contatore di elementi nella lista
|
||||
HashMap<E, Integer> valori = new HashMap<E, Integer>();
|
||||
|
||||
Iterator<E> it = myList.iterator();
|
||||
while (it.hasNext()) {
|
||||
E current = it.next();
|
||||
if (valori.get(current) == null) valori.put(current, 1);
|
||||
else valori.put(current, valori.get(current) + 1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
public class CompanyMap {
|
||||
|
||||
/*
|
||||
* EX1
|
||||
* Supponiamo che chi vengano forniti il nome e il numero
|
||||
* di divisione di ciascun dipendente di un'azienda.
|
||||
* Non ci sono nomi duplicati. Vorremmo memorizzare
|
||||
* queste informazioni in ordine alfabetico per nome.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione:
|
||||
*/
|
||||
|
||||
}
|
||||
+424
@@ -0,0 +1,424 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return object == this;
|
||||
}
|
||||
|
||||
// Parte iteratori
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
|
||||
}
|
||||
|
||||
public ListIterator<E> listIterator() {
|
||||
|
||||
}
|
||||
|
||||
private class MyLinkedListIterator implements ListIterator<E> {
|
||||
|
||||
// Variabili adoperate
|
||||
private Node<E> nextNode;
|
||||
private Node<E> prevNode;
|
||||
private Node<E> lastReturned = null;
|
||||
private int posNext;
|
||||
|
||||
public MyLinkedListIterator() {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
}
|
||||
|
||||
public MyLinkedListIterator(int index) {
|
||||
if ((index < 0) || (index > size)) throw new IndexOutOfBoundsException();
|
||||
// Caso in cui sia il primo
|
||||
if (index == 0) {
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
return;
|
||||
}
|
||||
// Caso in cui sia l'ultimo
|
||||
if (index == size) {
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
return;
|
||||
}
|
||||
nextNode = head;
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
while (posNext < index) {
|
||||
nextNode = nextNode.next;
|
||||
prevNode = nextNode.prev;
|
||||
posNext++;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return nextNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return prevNode != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
if (!hasNext()) throw new NoSuchElementException();
|
||||
E data = nextNode.data;
|
||||
lastReturned = nextNode;
|
||||
prevNode = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
posNext++;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E previous() {
|
||||
if (!hasPrevious()) throw new NoSuchElementException();
|
||||
E data = prevNode.data;
|
||||
lastReturned = prevNode;
|
||||
nextNode = prevNode;
|
||||
prevNode = prevNode.prev;
|
||||
posNext--;
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
return posNext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
return posNext - 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Il metodo remove si occupa della rimozione del nodo appena restituito
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
// Se non è stato restituito alcun nodo
|
||||
if (lastReturned == null) throw new NoSuchElementException();
|
||||
// Se l'ultimo elemento ad essere stato restituito è la testa
|
||||
if (lastReturned == head) {
|
||||
removeFirst();
|
||||
nextNode = head; // Head dopo la rimozione del primo nodo è il successivo a quello appena rimosso
|
||||
prevNode = null;
|
||||
posNext = 0;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se l'ultimo elemento ad essere stato restituito è la coda
|
||||
if (lastReturned == tail) {
|
||||
removeLast();
|
||||
nextNode = null;
|
||||
prevNode = tail;
|
||||
posNext = size;
|
||||
lastReturned = null;
|
||||
return;
|
||||
}
|
||||
// Se è un elemento generico
|
||||
Node<E> removingNode = lastReturned;
|
||||
Node<E> nNode = removingNode.next;
|
||||
Node<E> pNode = removingNode.prev;
|
||||
|
||||
pNode.next = nNode;
|
||||
nNode.prev = pNode;
|
||||
removingNode.prev = null;
|
||||
removingNode.next = null;
|
||||
|
||||
if (lastReturned == prevNode) posNext--;
|
||||
|
||||
nextNode = nNode;
|
||||
prevNode = pNode;
|
||||
size--;
|
||||
lastReturned = null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Quando viene richiamata la add, l'elemento viene aggiunto a sinistra del cursore.
|
||||
* Esempio di seguito:
|
||||
* A | B
|
||||
* chiamata add(X)
|
||||
* A X | B
|
||||
* Come detto prima la X viene aggiunta a sinistra
|
||||
*/
|
||||
@Override
|
||||
public void add(E data) {
|
||||
if (data == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(data, null, null);
|
||||
// Se la lista è vuota
|
||||
if (size == 0) {
|
||||
tail = head = newNode;
|
||||
prevNode = newNode;
|
||||
size++;
|
||||
posNext = 1;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è prima dell'HEAD
|
||||
if (prevNode == null) {
|
||||
addFirst(data);
|
||||
prevNode = head;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
// Se il cursore è dopo la TAIL
|
||||
if (prevNode == tail) {
|
||||
addLast(data);
|
||||
prevNode = tail;
|
||||
posNext++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) {
|
||||
|
||||
// Caso in cui esso sia un primo nodo
|
||||
if (currentNode == head) {
|
||||
removeFirst();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui esso sia un ultimo nodo
|
||||
if (currentNode == tail) {
|
||||
removeLast();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Caso in cui sia in mezzo
|
||||
Node<E> prevNode = currentNode.prev;
|
||||
Node<E> nextNode = currentNode.next;
|
||||
|
||||
prevNode.next = nextNode;
|
||||
nextNode.prev = prevNode;
|
||||
currentNode.prev = null;
|
||||
currentNode.next = null;
|
||||
|
||||
size--;
|
||||
|
||||
return true;
|
||||
}
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode = head;
|
||||
while (currentNode != null) {
|
||||
if (currentNode.data.equals(object)) return true;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
if (head == null) return null;
|
||||
Object[] array = new Object[size];
|
||||
Node<E> currentNode = head;
|
||||
int i = 0;
|
||||
while (currentNode != null) {
|
||||
array[i++] = currentNode;
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
|
||||
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
|
||||
if (citta == null) throw new NullPointerException();
|
||||
|
||||
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
|
||||
|
||||
for (String s : archivio.keySet()) {
|
||||
Cliente currentCliente = archivio.get(s);
|
||||
String currentCitta = currentCliente.cittaResidenza();
|
||||
|
||||
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
|
||||
}
|
||||
|
||||
return autoSelezionate;
|
||||
}
|
||||
|
||||
// 5. Ordinamento dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili
|
||||
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
|
||||
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(new Comparator()<String> {
|
||||
@Override
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-121
@@ -1,121 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class BinaryNode<E> {
|
||||
|
||||
// VARIABILI
|
||||
|
||||
private BinaryNode<E> left, right, parent;
|
||||
private E data;
|
||||
|
||||
// COSTRUTTORI
|
||||
|
||||
public BinaryNode(E data) {
|
||||
this.data = data;
|
||||
|
||||
left = right = parent = null;
|
||||
}
|
||||
|
||||
public BinaryNode(BinaryNode<E> left, E data, BinaryNode<E> right) {
|
||||
this.data = data;
|
||||
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
this.parent = null;
|
||||
|
||||
if (this.left != null) this.left.parent = this;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public E getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public BinaryNode<E> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return parent == null;
|
||||
}
|
||||
|
||||
public boolean isLeftChild() {
|
||||
return parent != null && parent.getLeft() == this;
|
||||
}
|
||||
|
||||
public boolean isRightChild() {
|
||||
return parent != null && parent.getRight() == this;
|
||||
}
|
||||
|
||||
public void setData(E data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setLeft(BinaryNode<E> left) {
|
||||
this.left = left;
|
||||
if (this.left != null) this.left.parent = this;
|
||||
}
|
||||
|
||||
public void setRight(BinaryNode<E> right) {
|
||||
this.right = right;
|
||||
if (this.right != null) this.right.parent = this;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsLeftChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldLeft = this.parent.left;
|
||||
this.parent.left = this;
|
||||
|
||||
return oldLeft;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setParentAsRightChild(BinaryNode<E> parent) {
|
||||
if (parent == null) return null;
|
||||
|
||||
this.parent = parent;
|
||||
BinaryNode<E> oldRight = this.parent.right;
|
||||
this.parent.right = this;
|
||||
|
||||
return oldRight;
|
||||
}
|
||||
|
||||
public BinaryNode<E> setAsRoot() {
|
||||
if (parent == null) return null;
|
||||
|
||||
BinaryNode<E> oldParent = parent;
|
||||
parent = null;
|
||||
|
||||
if (oldParent.left == this) oldParent.left = null;
|
||||
else oldParent.right = null;
|
||||
|
||||
return oldParent;
|
||||
}
|
||||
|
||||
public boolean hasLeft() {
|
||||
return left != null;
|
||||
}
|
||||
|
||||
public boolean hasRight() {
|
||||
return right != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null) return false;
|
||||
if (other == this) return true;
|
||||
if (!(other instanceof BinaryNode<?>))
|
||||
}
|
||||
|
||||
}
|
||||
-193
@@ -1,193 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
}
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
}
|
||||
|
||||
}
|
||||
-94
@@ -1,94 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
}
|
||||
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ExerciseBinaryTree {
|
||||
|
||||
|
||||
/*
|
||||
* Esercizio 1 II parziale 10.01.2018
|
||||
* Un albero binario di interi si dice pari se tutti i nodi contengono
|
||||
* un intero positivo pari. Realizzare il metodo statico
|
||||
* public static boolean IsEvenBtree (BinaryTree<Integer> 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<Integer> btree) {
|
||||
boolean even = true;
|
||||
Iterator<Integer> 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<Integer> 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<Integer> btree) {
|
||||
Iterator<Integer> iterator = btree.iteratorInOrder();
|
||||
Integer maxValue;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ExerciseBinaryTree {
|
||||
|
||||
|
||||
/*
|
||||
* Esercizio 1 II parziale 10.01.2018
|
||||
* Un albero binario di interi si dice pari se tutti i nodi contengono
|
||||
* un intero positivo pari. Realizzare il metodo statico
|
||||
* public static boolean IsEvenBtree (BinaryTree<Integer> 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<Integer> btree) {
|
||||
boolean even = true;
|
||||
Iterator<Integer> iterator = btree.iteratorInOrder();
|
||||
while (iterator.hasNext() && even) {
|
||||
Integer current = iterator.next();
|
||||
if (!(current % 2 == 0)) even = false;
|
||||
}
|
||||
return even;
|
||||
}
|
||||
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-432
@@ -1,432 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
BinaryNode<E> currentNode = root;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
E currentObject = iterator.next();
|
||||
|
||||
currentNode.setData(currentObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package jcf_set.example;
|
||||
|
||||
public class InteriRipetuti {
|
||||
|
||||
public static void main(String[] Args) {
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int N = 20;
|
||||
int MAX_INT = 50;
|
||||
|
||||
// HASHSET
|
||||
}
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Dispari<E extends Comparable<? super E>> {
|
||||
|
||||
/*
|
||||
* Esercizio 1 - Esame del 5 settembre 2017
|
||||
*
|
||||
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
|
||||
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
|
||||
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
|
||||
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
|
||||
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
|
||||
* e restituisce true se myList è dispari;
|
||||
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
|
||||
*/
|
||||
|
||||
// Variabile di instanza
|
||||
private LinkedList<E> myList = new LinkedList<E>();
|
||||
|
||||
// Metodo di esercizio
|
||||
public boolean isDispari() {
|
||||
// Se la lista è vuota allora returna dispari
|
||||
if (myList.isEmpty()) return true;
|
||||
|
||||
// Contatore di elementi nella lista
|
||||
HashMap<E, Integer> valori = new HashMap<E, Integer>();
|
||||
|
||||
Iterator<E> it = myList.iterator();
|
||||
while (it.hasNext()) {
|
||||
E current = it.next();
|
||||
if (valori.get(current) == null) valori.put(current, 1);
|
||||
else valori.put(current, valori.get(current) + 1);
|
||||
}
|
||||
|
||||
// Controllo short circuit di disparità
|
||||
for (E chiave : valori.keySet()) {
|
||||
int valore = valori.get(chiave);
|
||||
if (valore % 2 == 0) return false;
|
||||
}
|
||||
|
||||
// Arrivati qui la lista è dispari, manca solo l'ordinamento.
|
||||
Collections.sort(myList);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (Object x : this) result.append(x + " ");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public boolean remove(Object object) {
|
||||
if (object == null || head == null) return false;
|
||||
Node<E> currentNode;
|
||||
// Caso in cui sia il primo
|
||||
currentNode = head;
|
||||
if (currentNode.data.equals(object)) {
|
||||
removeFirst();
|
||||
}
|
||||
// Iterazione per la ricerca dell'oggetto
|
||||
Node<E> currentNode = head;
|
||||
|
||||
while (currentNode.next != null) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Dispari<E> {
|
||||
|
||||
/*
|
||||
* Esercizio 1 - Esame del 5 settembre 2017
|
||||
*
|
||||
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
|
||||
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
|
||||
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
|
||||
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
|
||||
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
|
||||
* e restituisce true se myList è dispari;
|
||||
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
|
||||
*/
|
||||
|
||||
// Variabile di instanza
|
||||
private LinkedList<E> myList = new LinkedList<E>();
|
||||
|
||||
// Metodo di esercizio
|
||||
public boolean isDispari() {
|
||||
// Verifica che la lista si pari o dispari
|
||||
}
|
||||
}
|
||||
-224
@@ -1,224 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) templist.add();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-383
@@ -1,383 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
|
||||
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
|
||||
if (citta == null) throw new NullPointerException();
|
||||
|
||||
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
|
||||
|
||||
for (String s : archivio.keySet()) {
|
||||
Cliente currentCliente = archivio.get(s);
|
||||
String currentCitta = currentCliente.cittaResidenza();
|
||||
|
||||
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
|
||||
}
|
||||
|
||||
return autoSelezionate;
|
||||
}
|
||||
|
||||
// 5. Ordinamento dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili
|
||||
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
|
||||
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(archivio);
|
||||
return archivioOrdinato;
|
||||
}
|
||||
|
||||
// 6. Stampa su video l'archivio attuale
|
||||
public void stampaArchivio() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
-500
@@ -1,500 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
rightTree.root.setAsRoot();
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
protected BinaryNode<E> find(E targetElement, BinaryNode<E> root) {
|
||||
if (root == null) return null;
|
||||
if (root.getData().equals(targetElement)) return root;
|
||||
BinaryNode<E> resNode;
|
||||
resNode = find(targetElement, root.getLeft());
|
||||
if (resNode == null) resNode = find(targetElement, root.getRight());
|
||||
return resNode;
|
||||
}
|
||||
|
||||
public boolean remove(E targetElement) {
|
||||
if (targetElement == null) return false;
|
||||
BinaryNode<E> temp = find(targetElement, root);
|
||||
if (temp != null) {
|
||||
temp.setData(null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(E targetElement) {
|
||||
return find(targetElement, root) != null;
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del preorder.
|
||||
* Le regole per il preorder sono queste:
|
||||
* - Visita prima se stesso
|
||||
* - Poi visita tutto ciò che si trova sinistra
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void preorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
preorder(node.getLeft(), temporaryList);
|
||||
preorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
preorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE INORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo del inorder.
|
||||
* Le regole per l'inorder sono queste:
|
||||
* - Visita prima tutto ciò che si trova a sinistra
|
||||
* - Poi visita se stesso
|
||||
* - Infine visita tutto ciò che si trova a destra
|
||||
*/
|
||||
|
||||
protected void inorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
inorder(node.getLeft(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
|
||||
inorder(node.getRight(), temporaryList);
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
inorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE POSTORDER
|
||||
|
||||
/*
|
||||
* Qui si utilizza il metodo postorder.
|
||||
* Le regole per il postorder sono queste:
|
||||
* - Visita prima di tutto ciò che si trova a sinistra
|
||||
* - Poi visita tutto ciò che si trova a destra
|
||||
* - Infine visita il sestesso
|
||||
*/
|
||||
|
||||
protected void postorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
if (node == null) return;
|
||||
|
||||
postorder(node.getLeft(), temporaryList);
|
||||
postorder(node.getRight(), temporaryList);
|
||||
|
||||
if (node.getData() != null) temporaryList.add(node.getData());
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
postorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE LEVEL ORDER
|
||||
|
||||
/*
|
||||
* Qui si usa il metodo levelorder.
|
||||
* Si analizza tutto il livello dell'albero.
|
||||
* Per chiarire di seguito un esempio:
|
||||
*
|
||||
* A
|
||||
* / \
|
||||
* B C
|
||||
* / \ \
|
||||
* D E F
|
||||
*
|
||||
* La lettura avviene quindi in questo modo:
|
||||
* A - B - C - D - E - F
|
||||
*
|
||||
* Si adopera una coda (Queue) di tipo FI-FO (First In - First Out)
|
||||
*/
|
||||
|
||||
protected void levelorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Queue<BinaryNode<E>> queueOfNodes = new LinkedList<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while(!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.remove();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
|
||||
if (current.getLeft() != null) queueOfNodes.add(current.getLeft());
|
||||
if (current.getRight() != null) queueOfNodes.add(current.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> iteratorLevelOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
levelorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATORE
|
||||
|
||||
public Iterator<E> iterator() {
|
||||
return iteratorPreOrder();
|
||||
}
|
||||
|
||||
// ITERATORE PREORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso stiamo usando un tipo stack.
|
||||
* Dobbiamo di fatto simulare uno stack se vogliamo
|
||||
* iterativamente fare questa cosa.
|
||||
*
|
||||
* Perchè se prima dobbiamo visitare il nodo centrale,
|
||||
* poi il nodo a sinistra e poi il nodo a destra inseriamo
|
||||
* prima quello di destra e poi quello di sinistra?
|
||||
*
|
||||
* Lo stack di fatto è una coda LIFO, ovvero last in - first out
|
||||
* e quindi l'ultimo che entra è il primo che esce.
|
||||
*
|
||||
* dal momento che vale questa cosa, se facciamo entrare prima quello
|
||||
* di destra e poi quello di sinistra, il primo nodo che verrà
|
||||
* prelevato è ovviamente quello a sinistra.
|
||||
*/
|
||||
protected void itpreorder (BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
BinaryNode<E> current;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
// Come già spiegato pushamo prima il nodo di destra
|
||||
if (current.getRight() != null) queueOfNodes.push(current.getRight());
|
||||
// Infine aggiungiamo il nodo di sinistra
|
||||
if (current.getLeft() != null) queueOfNodes.push(current.getLeft());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPreOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpreorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR INORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* Questo metodo riguarda lo stesso di quello superiore.
|
||||
* Il metodo inorder richiede che si visiti prima sinistra,
|
||||
* poi il nodo stesso, infine il nodo di destra.
|
||||
*
|
||||
* Possiamo quindi sempre adoperare uno Stack per siumlare la
|
||||
* ricorsione.
|
||||
*
|
||||
* Come detto prima, se il primo nodo ad essere prelevato è
|
||||
* l'ultimo che si inserisce (coda LIFO, Stack) allora per simulare
|
||||
* sinistra -> centro -> destra.
|
||||
*
|
||||
* Nel metodo sono state inserite delle check con uno stack di
|
||||
* flag che permette di capire quando il nodo centrale è visitare
|
||||
* oppure no.
|
||||
*/
|
||||
private void itinorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.add(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è da visitare
|
||||
|
||||
// prima si aggiunge il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.add(current.getRight());
|
||||
flags.add(false);
|
||||
}
|
||||
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.add(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorInOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itinorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
// ITERATOR POST ORDER ITERATIVO (NON RICORSIVO)
|
||||
|
||||
/*
|
||||
* In questo caso la logica è la seguente. si ha la
|
||||
* necessità di fare il seguente ciclo: SINISTRA -> DESTRA -> NODO CORRENTE
|
||||
*
|
||||
* Per simulare lo stack dobbiamo quindi ripercorrerlo al contrario,
|
||||
* dunque NODO CORRENTE -> DESTRA -> SINISTRA
|
||||
*
|
||||
* Possiamo dunque rifare lo stesso, di ciò che abbiamo fatto sopra.
|
||||
*/
|
||||
protected void itpostorder(BinaryNode<E> node, List<E> temporaryList) {
|
||||
Stack<BinaryNode<E>> queueOfNodes = new Stack<BinaryNode<E>>();
|
||||
Stack<Boolean> flags = new Stack<Boolean>();
|
||||
|
||||
BinaryNode<E> current;
|
||||
Boolean flag;
|
||||
|
||||
queueOfNodes.push(node);
|
||||
flags.push(false);
|
||||
|
||||
while (!queueOfNodes.isEmpty()) {
|
||||
current = queueOfNodes.pop();
|
||||
flag = flags.pop();
|
||||
|
||||
if (flag) {
|
||||
// il nodo è da visitare
|
||||
if (current.getData() != null) temporaryList.add(current.getData());
|
||||
} else {
|
||||
// il nodo non è ancora da visitare
|
||||
|
||||
// Visitiamo il nodo corrente
|
||||
queueOfNodes.push(current);
|
||||
flags.push(true);
|
||||
|
||||
// visitiamo il nodo di destra
|
||||
if (current.getRight() != null) {
|
||||
queueOfNodes.push(current.getRight());
|
||||
flags.push(false);
|
||||
}
|
||||
|
||||
// visitiamo il nodo di sinistra infine
|
||||
if (current.getLeft() != null) {
|
||||
queueOfNodes.push(current.getLeft());
|
||||
flags.push(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<E> ititeratorPostOrder() {
|
||||
ArrayList<E> temporaryList = new ArrayList<E>();
|
||||
itpostorder(root, temporaryList);
|
||||
return temporaryList.iterator();
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022/2023
|
||||
* Realizzare un metodo costruttore della classe LinkedBinaryTree<E> che
|
||||
* prende in input una lista di oggetti di tipo E e costruisce una catena
|
||||
* casuale tale che l'iesimo elemento della lista è posizionato al livello iesimo
|
||||
* della catena e ogni nodo ha probabilità 1/2 di avere un figlio sinistro/destro.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione intuitiva (visto che la melideo intende complicare la consegna):
|
||||
* Data ad esempio una lista del genere:
|
||||
* [A, B, C, D]
|
||||
*
|
||||
* Si ottiene quindi una catena del genere:
|
||||
* A
|
||||
* \
|
||||
* B
|
||||
* /
|
||||
* C
|
||||
* \
|
||||
* D
|
||||
*/
|
||||
public LinkedBinaryTree(List<E> objectList) {
|
||||
if (objectList == null) throw new NullPointerException();
|
||||
|
||||
Iterator<E> iterator = objectList.iterator();
|
||||
if (!iterator.hasNext()) return;
|
||||
|
||||
E currentObject = iterator.next();
|
||||
root = new BinaryNode<E>(currentObject);
|
||||
BinaryNode<E> currentNode = root;
|
||||
currentNode.setData(currentObject);
|
||||
size = 1;
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
currentObject = iterator.next();
|
||||
int wing = (int) (Math.random() * 2);
|
||||
|
||||
BinaryNode<E> newNode = new BinaryNode<E>(currentObject);
|
||||
|
||||
if (wing == 0) {
|
||||
// Left
|
||||
newNode.setParentAsLeftChild(currentNode);
|
||||
} else {
|
||||
// Right
|
||||
newNode.setParentAsRightChild(currentNode);
|
||||
}
|
||||
|
||||
currentNode = newNode;
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* II parziale 2022
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo
|
||||
* che stampa le foglie dall'albero corrente (da sinistra verso destra)
|
||||
*/
|
||||
public void printLeaf() {
|
||||
printLeaf(root);
|
||||
}
|
||||
|
||||
public void printLeaf(BinaryNode<E> node) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() == null && node.getRight() == null) System.out.println(node.getData().toString());
|
||||
else {
|
||||
printLeaf(node.getLeft());
|
||||
printLeaf(node.getRight());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Si aggiunga alla classe LinkedBinaryTree<E> un metodo che conta il numero
|
||||
* di foglie dell'albero corrente
|
||||
*/
|
||||
public int numberLeaf() {
|
||||
return numberLeaf(root);
|
||||
}
|
||||
|
||||
public int numberLeaf(BinaryNode<E> 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<E> 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<E> node) {
|
||||
if (node == null) return;
|
||||
}
|
||||
|
||||
}
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
package binary_tree;
|
||||
|
||||
public class LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
// VARIABILI D'INSTANZA
|
||||
private BinaryNode<E> root;
|
||||
private int size;
|
||||
|
||||
// Metodi costruttori
|
||||
public LinkedBinaryTree() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(E data) {
|
||||
root = new BinaryNode<E>(data);
|
||||
size = 1;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree(LinkedBinaryTree<E> left, E data, LinkedBinaryTree<E> right) {
|
||||
root = new BinaryNode<E>(left.root, data, right.root);
|
||||
size = 1 + left.size + right.size;
|
||||
}
|
||||
|
||||
// METODI
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return root == null;
|
||||
}
|
||||
|
||||
public E getRoot() {
|
||||
if (isEmpty()) return null;
|
||||
return root.getData();
|
||||
}
|
||||
|
||||
public BinaryNode<E> getRootNode() {
|
||||
if (isEmpty()) return null;
|
||||
return root;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
private int getSize(BinaryNode<E> node) {
|
||||
if (node == null) return 0;
|
||||
|
||||
int nLeft = (node.getLeft() == null) ? 0 : getSize(node.getLeft());
|
||||
int nRight = (node.getRight() == null) ? 0 : getSize(node.getRight());
|
||||
|
||||
return 1 + nLeft + nRight;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeLeft() {
|
||||
LinkedBinaryTree<E> leftTree = null;
|
||||
if (root.getLeft() == null) return leftTree;
|
||||
|
||||
leftTree = new LinkedBinaryTree<E>();
|
||||
leftTree.root = root.getLeft();
|
||||
leftTree.size = getSize(root.getLeft());
|
||||
leftTree.root.setAsRoot();
|
||||
|
||||
size = size - leftTree.size;
|
||||
|
||||
return leftTree;
|
||||
}
|
||||
|
||||
public LinkedBinaryTree<E> removeRight() {
|
||||
LinkedBinaryTree<E> rightTree = null;
|
||||
if (root.getRight() == null) return rightTree;
|
||||
|
||||
rightTree = new LinkedBinaryTree<E>();
|
||||
rightTree.root = root.getRight();
|
||||
rightTree.size = getSize(root.getRight());
|
||||
|
||||
size = size - rightTree.size;
|
||||
|
||||
return rightTree;
|
||||
}
|
||||
|
||||
}
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
package test;
|
||||
|
||||
public class Main {
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user