Fixato bug e implementato alcune funzioni.
This commit is contained in:
42
com/xgiovio/HeapPrioriyQueueTest.java
Normal file
42
com/xgiovio/HeapPrioriyQueueTest.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import com.xgiovio.general_utility.test_object;
|
||||
import priorityqueue.heap.HeapPriorityQueue;
|
||||
import stack.NodeStack;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
* User: xgiovio
|
||||
* Date: 23/03/14
|
||||
* Time: 20:37
|
||||
*/
|
||||
public class HeapPrioriyQueueTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Integer[] b = new Integer[5] ;
|
||||
b[0] = new Integer(7);
|
||||
b[1] = new Integer(5);
|
||||
b[2] = new Integer(-8);
|
||||
b[3] = new Integer(8);
|
||||
b[4] = new Integer(-2);
|
||||
|
||||
String[] c = new String[5] ;
|
||||
c[0] = new String("ale");
|
||||
c[1] = new String("ale");
|
||||
c[2] = new String("ale");
|
||||
c[3] = new String("ale");
|
||||
c[4] = new String("ale");
|
||||
|
||||
|
||||
|
||||
HeapPriorityQueue<Integer,String> a = new HeapPriorityQueue<Integer, String> (b,c, new DefaultComparator<Integer>());
|
||||
|
||||
System.out.println(a.min().getKey());
|
||||
System.out.println(a);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,22 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import position.NodePositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
NodePositionList<Integer> a = new NodePositionList<Integer>();
|
||||
a.addLast(1);
|
||||
a.addLast(2);
|
||||
Iterator<Integer> it = a.iterator();
|
||||
a.addLast(3);
|
||||
a.addLast(4);
|
||||
|
||||
for (;it.hasNext();)
|
||||
System.out.println(it.next());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dictionary;
|
||||
import exceptions.InvalidEntryException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
@@ -8,7 +9,7 @@ import tree.binarytree.BTPosition;
|
||||
import tree.binarytree.LinkedBinaryTree;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import exceptions.InvalidEntryException;
|
||||
import position.NodePositionList;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@ package dictionary;
|
||||
import exceptions.InvalidEntryException;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
|
||||
public interface Dictionary<K,V> {
|
||||
|
||||
@@ -8,7 +8,7 @@ import position.NodePositionList;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ import position.Position;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
|
||||
11
exceptions/InvalidKeyException.java
Normal file
11
exceptions/InvalidKeyException.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package exceptions;
|
||||
|
||||
|
||||
public class InvalidKeyException extends RuntimeException {
|
||||
public InvalidKeyException(String message) {
|
||||
super (message);
|
||||
}
|
||||
public InvalidKeyException() {
|
||||
super ("Invalid Key Exception");
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import arraylist.IndexList;
|
||||
import position.NodePositionList;
|
||||
import position.PositionList;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
public abstract class BFS<V, E, I, R> {
|
||||
protected Graph<V, E> graph;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
package graph;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
public class ComponentsBFS<V, E> extends BFS<V, E, Object, Integer> {
|
||||
protected Integer compNumber;
|
||||
@@ -13,16 +13,11 @@ public class ComponentsBFS<V, E> extends BFS<V, E, Object, Integer> {
|
||||
}
|
||||
|
||||
protected void startVisit(Vertex<V> v) {
|
||||
try {
|
||||
v.put(COMPONENT, compNumber);
|
||||
}
|
||||
catch (InvalidKeyException e){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected Integer finalResult(Integer bfsResult) {
|
||||
try {
|
||||
|
||||
for(Vertex<V> v : graph.vertices())
|
||||
if(!isVisited(v)) {
|
||||
compNumber++;
|
||||
@@ -31,9 +26,4 @@ public class ComponentsBFS<V, E> extends BFS<V, E, Object, Integer> {
|
||||
|
||||
return compNumber;
|
||||
}
|
||||
catch (InvalidKeyException e){
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package graph;
|
||||
|
||||
//begin#fragment CC
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
/** This class extends DFS to compute the connected components of a graph. */
|
||||
public class ComponentsDFS<V, E> extends DFS<V, E, Object, Integer> {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package graph;
|
||||
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
/** Generic DFS traversal of a graph using the template method pattern.
|
||||
* Parameterized types:
|
||||
|
||||
@@ -1,74 +1,34 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:06
|
||||
*/
|
||||
|
||||
//iterator implementation with cursor for PositionList data structures.
|
||||
|
||||
public class ElementIterator<E> implements Iterator<E> {
|
||||
protected PositionList<E> list; // the underlying list
|
||||
protected Position<E> cursor; // the next position
|
||||
|
||||
public ElementIterator (PositionList<E> structure){
|
||||
|
||||
new_structure = structure;
|
||||
|
||||
public ElementIterator(PositionList<E> L) {
|
||||
list = L;
|
||||
cursor = (list.isEmpty())? null : list.first();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure. size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
public boolean hasNext() { return (cursor != null); }
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos.element();
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
if (cursor == null)
|
||||
throw new NoSuchElementException("No next element");
|
||||
E toReturn = cursor.element();
|
||||
cursor = (cursor == list.last())? null : list.next(cursor);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
public void remove() throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException("remove");
|
||||
}
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import position.NodePositionList;
|
||||
import position.PositionList;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ import position.PositionList;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ package map;
|
||||
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
public interface Map<K,V> {
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import set.OrderedListSet;
|
||||
import set.Set;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
@@ -40,23 +40,18 @@ public class ListPartition <E> implements Partition<E> {
|
||||
|
||||
@Override
|
||||
public Set<E> makeSet(E x) {
|
||||
try {
|
||||
|
||||
OrderedListSet<E> o = new OrderedListSet<E>(x, new DefaultComparator<E>());
|
||||
elementi.put(x, o);
|
||||
partizione.addLast(o);
|
||||
o.setLocation(partizione.last());
|
||||
return o;
|
||||
}
|
||||
catch (InvalidKeyException e){
|
||||
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> union(Set<E> A, Set<E> B) {
|
||||
try {
|
||||
|
||||
if (A.size() > B.size()){
|
||||
OrderedListSet<E> AA = (OrderedListSet<E>) A;
|
||||
OrderedListSet<E> BB = (OrderedListSet<E>) B;
|
||||
@@ -83,22 +78,12 @@ public class ListPartition <E> implements Partition<E> {
|
||||
|
||||
}
|
||||
}
|
||||
catch (InvalidKeyException e){
|
||||
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<E> find(E x) {
|
||||
try {
|
||||
return elementi.get(x);
|
||||
}
|
||||
catch (InvalidKeyException e){
|
||||
|
||||
}
|
||||
return null;
|
||||
return elementi.get(x);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,7 +3,7 @@ package priorityqueue;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.PriorityQueue;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package priorityqueue;
|
||||
|
||||
import exceptions.EmptyPriorityQueueException;
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
|
||||
/**
|
||||
* Created with xgiovio.macbookair.
|
||||
|
||||
@@ -6,7 +6,7 @@ import exceptions.InvalidEntryException;
|
||||
import position.Position;
|
||||
import position.utility.DNode;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import position.NodePositionList;
|
||||
import position.Position;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import position.NodePositionList;
|
||||
import position.Position;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import priorityqueue.AdaptablePriorityQueue;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,11 +4,12 @@ import position.Position;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
import priorityqueue.PriorityQueue;
|
||||
import stack.NodeStack;
|
||||
import tree.binarytree.heap.ArrayListCompleteBinaryTree;
|
||||
import tree.binarytree.heap.CompleteBinaryTree;
|
||||
import utility.DefaultComparator;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import exceptions.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
@@ -28,7 +29,7 @@ public class HeapPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
||||
comp = c;
|
||||
}
|
||||
|
||||
//elementi gia' ordinati
|
||||
|
||||
public HeapPriorityQueue (K k[], V v[], Comparator<K> C){
|
||||
heap = new ArrayListCompleteBinaryTree<Entry<K,V>>();
|
||||
comp = C;
|
||||
@@ -37,11 +38,22 @@ public class HeapPriorityQueue<K,V> implements PriorityQueue<K,V> {
|
||||
|
||||
} else {
|
||||
|
||||
NodeStack<ArrayListCompleteBinaryTree.BTPos<Entry<K, V>>> s = new NodeStack<ArrayListCompleteBinaryTree.BTPos<Entry<K, V>>>();
|
||||
for (int i = 0; i < k.length; i++) {
|
||||
heap.add(new MyEntry<K, V>(k[i],v[i])); // gia' ordinati. evitiamo l'upheap
|
||||
ArrayListCompleteBinaryTree.BTPos<Entry<K, V>> t = (ArrayListCompleteBinaryTree.BTPos<Entry<K, V>>) heap.add(new MyEntry<K, V>(k[i], v[i]));
|
||||
s.push(t);
|
||||
}
|
||||
|
||||
for (;!s.isEmpty();){
|
||||
ArrayListCompleteBinaryTree.BTPos<Entry<K, V>> t = s.pop();
|
||||
if (!heap.isExternal(t))
|
||||
downHeap(t);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setComparator(Comparator<K> c) throws IllegalStateException {
|
||||
|
||||
210
test_2_prova_intercorso/ExBinaryTree_11_2_13.java
Normal file
210
test_2_prova_intercorso/ExBinaryTree_11_2_13.java
Normal file
@@ -0,0 +1,210 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
|
||||
L'albero T usato per il II test e` il seguente:
|
||||
|
||||
|
||||
4
|
||||
|
||||
/ \
|
||||
2 8
|
||||
/ \ / \
|
||||
1 3 4 10
|
||||
\ / \
|
||||
7 2 11
|
||||
|
||||
|
||||
L'albero T usato per il III test e` il seguente:
|
||||
|
||||
|
||||
4
|
||||
|
||||
/ \
|
||||
2 8
|
||||
/ \ / \
|
||||
1 3 4 10
|
||||
\ \ / \
|
||||
2 7 2 11
|
||||
|
||||
|
||||
L'albero T usato per il IV test e` il seguente:
|
||||
|
||||
|
||||
4
|
||||
|
||||
/ \
|
||||
2 8
|
||||
/ \ / \
|
||||
1 3 4 10
|
||||
\ / \ / \
|
||||
8 5 7 2 11
|
||||
/ \
|
||||
1 40
|
||||
|
||||
|
||||
|
||||
|
||||
Il programma deve stampare:
|
||||
|
||||
I test: Albero vuoto
|
||||
|
||||
II test: L'albero soddisfa la proprieta`.
|
||||
|
||||
III test: L'albero NON soddisfa la proprieta`.
|
||||
|
||||
IV test: L'albero NON soddisfa la proprieta`.
|
||||
|
||||
*/
|
||||
|
||||
import exceptions.EmptyTreeException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.Position;
|
||||
import tree.binarytree.BTNode;
|
||||
import tree.binarytree.BTPosition;
|
||||
import tree.binarytree.BinaryTree;
|
||||
import tree.binarytree.LinkedBinaryTree;
|
||||
|
||||
public class ExBinaryTree_11_2_13 {
|
||||
|
||||
public static void main(String[] args){
|
||||
ExamBinaryTree <Integer>T= new ExamBinaryTree<Integer>();
|
||||
|
||||
System.out.print("I test: ");
|
||||
|
||||
try{check(T);}
|
||||
catch(EmptyTreeException e)
|
||||
{System.out.println("Albero vuoto");}
|
||||
|
||||
T.examAddRoot(4);
|
||||
T.examExpandExternal(T.root(), 2, 8 );
|
||||
T.examExpandExternal(T.left(T.root()), 1 , 3 );
|
||||
T.examExpandExternal(T.right(T.root()), 4 , 10 );
|
||||
T.examInsertRight(T.left(T.right(T.root())), 7 );
|
||||
T.examExpandExternal(T.right(T.right(T.root())), 2 , 11 );
|
||||
|
||||
System.out.print( "\nII test: ");
|
||||
if(check(T)) System.out.println("L'albero soddisfa la proprieta`.");
|
||||
else System.out.println("L'albero NON soddisfa la proprieta`.");
|
||||
|
||||
T.examInsertRight(T.right(T.left(T.root())), 2);
|
||||
System.out.print( "\nIII test: ");
|
||||
if(check(T)) System.out.println("L'albero soddisfa la proprieta`.");
|
||||
else System.out.println("L'albero NON soddisfa la proprieta`.");
|
||||
|
||||
T.replace(T.right(T.right(T.left(T.root()))), 8);
|
||||
T.examInsertLeft(T.left(T.right(T.root())),5);
|
||||
T.examExpandExternal(T.left(T.left(T.right(T.root()))), 1, 40);
|
||||
|
||||
System.out.print( "\nIV test: ");
|
||||
if(check(T)) System.out.println("L'albero soddisfa la proprieta`.");
|
||||
else System.out.println("L'albero NON soddisfa la proprieta`.");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean exec_check(BinaryTree<Integer> T, Position<Integer> start) {
|
||||
if (T.isExternal(start))
|
||||
return true;
|
||||
else {
|
||||
|
||||
if (T.hasLeft(start) && T.hasRight(start)){
|
||||
Integer leftv = T.left(start).element();
|
||||
Integer rightv = T.right(start).element();
|
||||
Integer myvalue = start.element();
|
||||
if (leftv < myvalue && rightv > myvalue)
|
||||
return (true && exec_check (T,T.left(start)) && exec_check (T,T.right(start)) ) ;
|
||||
return false;
|
||||
} else {
|
||||
if (T.hasLeft(start) && !T.hasRight(start)) {
|
||||
Integer leftv = T.left(start).element();
|
||||
Integer myvalue = start.element();
|
||||
if (leftv < myvalue)
|
||||
return (true && exec_check(T, T.left(start)));
|
||||
return false;
|
||||
} else {
|
||||
Integer rightv = T.right(start).element();
|
||||
Integer myvalue = start.element();
|
||||
if ( rightv > myvalue)
|
||||
return (true && exec_check (T,T.right(start)) ) ;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//scrivere qui la funzione
|
||||
public static boolean check(BinaryTree<Integer> T){
|
||||
if (T.isEmpty())
|
||||
throw new EmptyTreeException();
|
||||
Position<Integer> start = T.root();
|
||||
return exec_check (T,start) ;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Sostituire size con in nome della variabile di istanza che tiene
|
||||
//traccia del numero di nodi nella vostra classe LinkedBinaryTree
|
||||
public static class ExamBinaryTree <E>extends LinkedBinaryTree<E> implements BinaryTree<E>{
|
||||
|
||||
|
||||
public Position<E> examAddRoot(E e){// throws NonEmptyTreeException {
|
||||
// if(!isEmpty())
|
||||
// throw new NonEmptyTreeException("L'albero ha gia una radice");
|
||||
size = 1;
|
||||
root = examCreateNode(e,null,null,null);
|
||||
return root;
|
||||
}
|
||||
|
||||
protected BTPosition<E> examCreateNode(E element, BTPosition<E> parent,
|
||||
BTPosition<E> left, BTPosition<E> right) {
|
||||
return new BTNode<E>(element,parent,left,right); }
|
||||
|
||||
public void examExpandExternal(Position<E> v, E l, E r)
|
||||
throws InvalidPositionException {
|
||||
if (!isExternal(v))
|
||||
throw new InvalidPositionException("Il nodo non e` una foglia");
|
||||
examInsertLeft(v, l);
|
||||
examInsertRight(v, r);
|
||||
}
|
||||
|
||||
|
||||
public Position<E> examInsertLeft(Position<E> v, E e)
|
||||
throws InvalidPositionException {
|
||||
BTPosition<E> vv = checkPosition(v);
|
||||
Position<E> leftPos = vv.getLeft();
|
||||
if (leftPos != null)
|
||||
throw new InvalidPositionException("il nodo ha gia` un figlio sin");
|
||||
BTPosition<E> ww = examCreateNode(e, vv, null, null);
|
||||
vv.setLeft(ww);
|
||||
size++;
|
||||
return ww;
|
||||
}
|
||||
public Position<E> examInsertRight(Position<E> v, E e)
|
||||
throws InvalidPositionException {
|
||||
BTPosition<E> vv = checkPosition(v);
|
||||
Position<E> rightPos = vv.getRight();
|
||||
if (rightPos != null)
|
||||
throw new InvalidPositionException("il nodo ha gia` un figlio destro");
|
||||
BTPosition<E> w = examCreateNode(e, vv, null, null);
|
||||
vv.setRight(w);
|
||||
size++;
|
||||
return w;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
164
test_2_prova_intercorso/ExGraph_15_6PerEserc.java
Normal file
164
test_2_prova_intercorso/ExGraph_15_6PerEserc.java
Normal file
@@ -0,0 +1,164 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
|
||||
/*Il programma deve stampare :
|
||||
|
||||
Test su un grafo che NON contiene cicli di lunghezza 3 passanti per il vertice 0:
|
||||
Il grafo contiene i vertici: 0 1 2 3 4 5 6 7 8
|
||||
e gli archi: (0,1)(0,2)(0,3)(1,4)(1,5)(1,6)(2,5)(2,6)(3,6)(4,6)(4,7)(4,8)(5,7)(5,8)(6,8)
|
||||
|
||||
Se invochiamo removeClosestVertices sul vertice 0 vengono cancellati i vertici:
|
||||
4 5 6 1 2 3
|
||||
|
||||
Test su un grafo che contiene cicli di lunghezza 3 passanti per il vertice 0:
|
||||
Il grafo contiene i vertici: 0 1 2 3 4 5 6 7 8
|
||||
e gli archi: (0,1)(0,2)(0,3)(0,4)(0,5)(1,4)(1,5)(1,6)(2,5)(2,6)(3,6)(4,6)(4,7)(4,8)(5,7)(5,8)(6,8)
|
||||
|
||||
Se invochiamo removeClosestVertices sul vertice 0 vengono cancellati i vertici:
|
||||
6 1 2 3 7 8 4 5
|
||||
|
||||
|
||||
*PS: ovviamente i vertici e gli archi potrebbero essere stampati in un diverso ordine
|
||||
*/
|
||||
|
||||
|
||||
import graph.AdjacencyListGraph;
|
||||
import graph.Edge;
|
||||
import graph.Graph;
|
||||
import graph.Vertex;
|
||||
import position.NodePositionList;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ExGraph_15_6PerEserc {
|
||||
public static void main(String[] args){
|
||||
AdjacencyListGraph<Integer,String> G1= new AdjacencyListGraph<Integer,String>();
|
||||
AdjacencyListGraph<Integer,String> G2= new AdjacencyListGraph<Integer,String>();
|
||||
|
||||
//I test
|
||||
System.out.println("\n\nTest su un grafo che NON contiene cicli di lunghezza 3 passanti per il vertice 0:");
|
||||
System.out.print("Il grafo contiene i vertici: ");
|
||||
|
||||
Vertex<Integer>[] A= (Vertex<Integer>[]) new Vertex[9];
|
||||
for(int i=0;i<=8;i++)
|
||||
A[i]=G1.insertVertex(i);
|
||||
|
||||
for(int i= 1;i<4;i++)
|
||||
G1.insertEdge(A[0], A[i], null);
|
||||
|
||||
for(int i=1;i<=3;i++){
|
||||
for(int j= i+3;j<7;j++)
|
||||
G1.insertEdge(A[i], A[j], null);
|
||||
}
|
||||
for(int i=4;i<9;i++){
|
||||
for(int j= i+2;j<9;j++)
|
||||
G1.insertEdge(A[i], A[j], null);
|
||||
}
|
||||
|
||||
|
||||
Iterator<Vertex<Integer>> itV=G1.vertices().iterator();
|
||||
while(itV.hasNext())
|
||||
System.out.print(itV.next().element()+" ");
|
||||
|
||||
System.out.print("\ne gli archi: ");
|
||||
Iterator<Edge<String>> itE=G1.edges().iterator();
|
||||
while(itE.hasNext()){
|
||||
Edge <String> e= itE.next();
|
||||
System.out.print("("+G1.endVertices(e)[0].element()+","+G1.endVertices(e)[1].element()+")");
|
||||
}
|
||||
|
||||
|
||||
itV=removeClosestVertices(G1,A[0]).iterator();
|
||||
System.out.println("\n\nSe invochiamo removeClosestVertices sul vertice 0 vengono cancellati i vertici:");
|
||||
|
||||
while(itV.hasNext())
|
||||
System.out.print(itV.next()+" ");
|
||||
|
||||
|
||||
//II test
|
||||
System.out.println("\n\nTest su un grafo che contiene cicli di lunghezza 3 passanti per il vertice 0:");
|
||||
System.out.print("Il grafo contiene i vertici: ");
|
||||
|
||||
for(int i=0;i<=8;i++)
|
||||
A[i]=G2.insertVertex(i);
|
||||
|
||||
for(int i= 1;i<6;i++)
|
||||
G2.insertEdge(A[0], A[i], null);
|
||||
|
||||
for(int i=1;i<=3;i++){
|
||||
for(int j= i+3;j<7;j++)
|
||||
G2.insertEdge(A[i], A[j], null);
|
||||
}
|
||||
for(int i=4;i<8;i++){
|
||||
for(int j= i+2;j<9;j++)
|
||||
G2.insertEdge(A[i], A[j], null);
|
||||
}
|
||||
|
||||
itV=G2.vertices().iterator();
|
||||
while(itV.hasNext())
|
||||
System.out.print(itV.next().element()+" ");
|
||||
|
||||
System.out.print("\ne gli archi: ");
|
||||
itE=G2.edges().iterator();
|
||||
while(itE.hasNext()){
|
||||
Edge <String> e= itE.next();
|
||||
System.out.print("("+G2.endVertices(e)[0].element()+","+G2.endVertices(e)[1].element()+")");
|
||||
}
|
||||
|
||||
|
||||
itV=removeClosestVertices(G2,A[0]).iterator();
|
||||
System.out.println("\n\nSe invochiamo removeClosestVertices sul vertice 0 vengono cancellati i vertici:");
|
||||
|
||||
while(itV.hasNext())
|
||||
System.out.print(itV.next()+" ");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static<V> boolean notpresentin (Vertex<V> v, PositionList<Vertex<V>> list ){
|
||||
|
||||
Iterator<Vertex<V>> it = list.iterator();
|
||||
for (;it.hasNext();){
|
||||
if (v.equals(it.next()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//scrivere qui la funzione
|
||||
public static<V,E> Iterable<Vertex<V>> removeClosestVertices(Graph<V,E> G,Vertex<V> v){
|
||||
NodePositionList<Vertex<V>> vertex_list = new NodePositionList<Vertex<V>>();
|
||||
|
||||
Iterator<Edge<E>> it = G.incidentEdges(v).iterator();
|
||||
for (;it.hasNext();){
|
||||
vertex_list.addLast(G.opposite( v, it.next() ) );
|
||||
}
|
||||
Iterator<Vertex<V>> it2 = vertex_list.iterator();
|
||||
for (;it2.hasNext();){
|
||||
Vertex<V> t = it2.next();
|
||||
Iterator<Edge<E>> it3 = G.incidentEdges(t).iterator();
|
||||
for (;it3.hasNext();){
|
||||
Edge<E> t2 = it3.next();
|
||||
Vertex<V> t3 = G.opposite( t, t2 );
|
||||
if (!t3.equals(v) && notpresentin (t3,vertex_list)){
|
||||
vertex_list.addFirst(t3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Iterator<Vertex<V>> it4 = vertex_list.iterator();
|
||||
|
||||
Vertex<V> t = it4.next();
|
||||
G.removeVertex(t);
|
||||
|
||||
|
||||
return vertex_list;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
117
test_2_prova_intercorso/ExMap15_1.java
Normal file
117
test_2_prova_intercorso/ExMap15_1.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
/*Il programma deve stampare (ovviamente le entrate di M e D possono essere stampate in un diverso ordine):
|
||||
I test:
|
||||
M e` vuota
|
||||
D = < (50,29) (50,39) (50,49) (70,39) (70,49) (70,59) (70,69) (90,49) (90,59) (90,69) (90,79) (90,89) (10,9) (30,19) (30,29) >
|
||||
Dopo aver invocato addEntries su M e D, si ha M=< (90,49) (70,39) (50,29) (30,19) (10,9) >
|
||||
|
||||
II test:
|
||||
M=< (90,49) (70,39) (50,29) (30,19) (10,9) >
|
||||
D = < (50,29) (50,39) (50,49) (50,28) (70,39) (70,49) (70,59) (70,69) (70,38) (90,49) (90,59) (90,69) (90,79) (90,89) (90,48) (10,9) (10,8) (30,19) (30,29) (30,18) >.
|
||||
Nota che la mappa M contiene le entrate inserite dalla prima chiamata di addEntries
|
||||
e che al dizionario D sono state aggiunte le entrate (50,28) (30,18) (90,48) (10,8) (70,38).
|
||||
Dopo aver invocato addEntries su M e D, si ha M=< (90,49) (70,39) (50,29) (30,19) (10,9) >.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
import dictionary.Dictionary;
|
||||
import dictionary.LinearProbingHashTable;
|
||||
import exceptions.InvalidKeyException;
|
||||
import map.HashTableMap;
|
||||
import map.Map;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ExMap15_1 {
|
||||
public static void main(String[] args){
|
||||
Map<Integer,Integer> M = new HashTableMap<Integer,Integer>();
|
||||
Dictionary<Integer,Integer> D= new LinearProbingHashTable<Integer,Integer>();
|
||||
|
||||
|
||||
System.out.println("I test:");
|
||||
System.out.println("M e` vuota ");
|
||||
for(int i=10;i<100;i=i+20)
|
||||
for(int j=i/2+4;j<i;j=j+10)
|
||||
D.insert(i, j);
|
||||
Iterator<Entry<Integer,Integer>> itD= D.entries().iterator();
|
||||
System.out.print("D = < ");
|
||||
while(itD.hasNext())
|
||||
System.out.print(itD.next()+" ");
|
||||
System.out.println(">");
|
||||
|
||||
addEntries(M,D,new IntegerComparator());
|
||||
Iterator<Entry<Integer,Integer>> itM= M.entries().iterator();
|
||||
System.out.print("Dopo aver invocato addEntries su M e D, si ha M=< ");
|
||||
while(itM.hasNext())
|
||||
System.out.print(itM.next()+" ");
|
||||
System.out.println(">");
|
||||
|
||||
|
||||
System.out.println("\nII test: ");
|
||||
itM= M.entries().iterator();
|
||||
System.out.print("M=< ");
|
||||
while(itM.hasNext())
|
||||
System.out.print(itM.next()+" ");
|
||||
System.out.println(">");
|
||||
for(int i=10;i<100;i=i+20)
|
||||
D.insert(i, i/2+3);
|
||||
itD= D.entries().iterator();
|
||||
System.out.print("D = < ");
|
||||
while(itD.hasNext())
|
||||
System.out.print(itD.next()+" ");
|
||||
System.out.println(">.");
|
||||
System.out.println("Nota che la mappa M contiene le entrate inserite dalla prima chiamata di addEntries");
|
||||
System.out.println("e che al dizionario D sono state aggiunte le entrate (50,28) (30,18) (90,48) (10,8) (70,38).");
|
||||
addEntries(M,D,new IntegerComparator());
|
||||
itM= M.entries().iterator();
|
||||
System.out.print("Dopo aver invocato addEntries su M e D, si ha M=< ");
|
||||
while(itM.hasNext())
|
||||
System.out.print(itM.next()+" ");
|
||||
System.out.println(">.");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//scrivere qui la funzione
|
||||
public static <K> void addEntries(Map<K,K> M, Dictionary<K,K> D, Comparator<K> c){
|
||||
|
||||
Iterator<Entry<K,K>> it = D.entries().iterator();
|
||||
for (;it.hasNext();){
|
||||
Entry<K,K> e = it.next();
|
||||
K key = e.getKey();
|
||||
K value = null;
|
||||
if (M.get(key) == null ){
|
||||
Iterator<Entry<K,K>> it2 = D.findAll(key).iterator();
|
||||
int flag = 0;
|
||||
for (;it2.hasNext();){
|
||||
Entry<K,K>e2 = it2.next();
|
||||
if (flag == 0){
|
||||
flag = 1;
|
||||
value = e2.getValue();
|
||||
} else {
|
||||
if ( c.compare( e2.getValue(),value) < 0 ){
|
||||
value = e2.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
M.put(key,value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class IntegerComparator implements Comparator<Integer> {
|
||||
|
||||
public int compare(Integer a, Integer b) throws ClassCastException {
|
||||
return(a-b);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,10 @@ Dopo aver invocato subtract(Q1,Q2), la coda a priorita` Q1 contiene le entrate:
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
||||
|
||||
import position.NodePositionList;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.PriorityQueue;
|
||||
import priorityqueue.heap.HeapPriorityQueue;
|
||||
@@ -105,63 +107,46 @@ public class ExPQ_21_2_11 {
|
||||
|
||||
public static <K,V>void subtract (PriorityQueue<K,V> Q1, PriorityQueue <K,V> Q2,Comparator<K>c ) throws InvalidKeyException{
|
||||
|
||||
if (Q1== null || Q2 == null || Q1.size() == 0 || Q2.size() == 0)
|
||||
if( Q1.isEmpty() || Q2.isEmpty() )
|
||||
throw new EmptyPriorityQueueException();
|
||||
|
||||
PriorityQueue<K,V> tempq = new HeapPriorityQueue<K, V>();
|
||||
|
||||
Entry<K,V> t = Q1.removeMin();
|
||||
Entry<K,V> t2 = Q2.removeMin();
|
||||
Entry<K,V> a = Q1.removeMin();
|
||||
Entry<K,V> b = Q2.removeMin();
|
||||
|
||||
NodePositionList<Entry<K,V>> t = new NodePositionList<Entry<K, V>>();
|
||||
|
||||
for(;;){
|
||||
|
||||
int result = c.compare( t.getKey(),t2.getKey());
|
||||
|
||||
if (result < 0) {
|
||||
tempq.insert(t.getKey(), t.getValue());
|
||||
if (Q1.size() > 0) {
|
||||
t = Q1.removeMin();
|
||||
if (c.compare(a.getKey(),b.getKey()) < 0){
|
||||
t.addLast(a);
|
||||
if (!Q1.isEmpty()){
|
||||
a=Q1.removeMin();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}else break;
|
||||
}
|
||||
|
||||
if (result == 0){
|
||||
if (Q1.size() > 0) {
|
||||
t = Q1.removeMin();
|
||||
if (c.compare(a.getKey(),b.getKey()) == 0){
|
||||
if (!Q1.isEmpty()){
|
||||
a=Q1.removeMin();
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}else break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( result > 0){
|
||||
if (Q2.size() > 0) {
|
||||
|
||||
t2 = Q2.removeMin();
|
||||
if (c.compare(a.getKey(),b.getKey()) > 0){
|
||||
if (!Q2.isEmpty()){
|
||||
b=Q2.removeMin();
|
||||
continue;
|
||||
}else {
|
||||
break;
|
||||
}else break;
|
||||
}
|
||||
}
|
||||
|
||||
Iterator<Entry<K,V>> it = t.iterator();
|
||||
for (;it.hasNext();){
|
||||
Entry<K,V> e = it.next();
|
||||
Q1.insert(e.getKey(),e.getValue());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
for ( ;tempq.size() > 0 ;){
|
||||
t = tempq.removeMin();
|
||||
Q1.insert(t.getKey(), t.getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
186
test_2_prova_intercorso/ExTree_17_1_2013.java
Normal file
186
test_2_prova_intercorso/ExTree_17_1_2013.java
Normal file
@@ -0,0 +1,186 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
|
||||
import exceptions.EmptyTreeException;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
import tree.LinkedTree;
|
||||
import tree.Tree;
|
||||
import tree.TreeNode;
|
||||
import tree.TreePosition;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/*****
|
||||
* L'albero usato per il II test e`
|
||||
*
|
||||
* 9
|
||||
* / / \ \
|
||||
* 10 11 12 13
|
||||
* / | \ / / \ \
|
||||
* 2 3 4 8 7 6 10
|
||||
* | / | \
|
||||
* 10 11 10 9
|
||||
*
|
||||
*
|
||||
|
||||
Il programma deve stampare:
|
||||
|
||||
I test
|
||||
L'albero e` vuoto.
|
||||
|
||||
|
||||
II test: T e` l'albero disegnato all'inizio del file.
|
||||
|
||||
I primi 2 nodi contenenti 10 sono:
|
||||
il nodo avente come padre il nodo contenente 9 e come figli i nodi contenenti gli elementi 2, 3, 4,
|
||||
il nodo avente come padre il nodo contenente 4
|
||||
|
||||
I primi 3 nodi contenenti 10 sono:
|
||||
il nodo avente come padre il nodo contenente 9 e come figli i nodi contenenti gli elementi 2, 3, 4,
|
||||
il nodo avente come padre il nodo contenente 4
|
||||
il nodo avente come padre il nodo contenente 6
|
||||
|
||||
I primi 4 nodi contenenti 10 sono:
|
||||
il nodo avente come padre il nodo contenente 9 e come figli i nodi contenenti gli elementi 2, 3, 4,
|
||||
il nodo avente come padre il nodo contenente 4
|
||||
il nodo avente come padre il nodo contenente 6
|
||||
il nodo avente come padre il nodo contenente 13
|
||||
|
||||
I primi 5 nodi contenenti 10 sono:
|
||||
il nodo avente come padre il nodo contenente 9 e come figli i nodi contenenti gli elementi 2, 3, 4,
|
||||
il nodo avente come padre il nodo contenente 4
|
||||
il nodo avente come padre il nodo contenente 6
|
||||
il nodo avente come padre il nodo contenente 13
|
||||
*/
|
||||
|
||||
public class ExTree_17_1_2013 {
|
||||
|
||||
public static void main(String[]args){
|
||||
|
||||
ExamTree<Integer > T = new ExamTree<Integer>();
|
||||
|
||||
|
||||
|
||||
System.out.println("I test");
|
||||
try{
|
||||
select(T,5,4);
|
||||
}
|
||||
catch(EmptyTreeException e){
|
||||
System.out.println("L'albero e` vuoto.\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
T.examAddRoot(9);
|
||||
for(int i=10;i<=13 ; i++)
|
||||
T.examAddChild(i, T.root());
|
||||
|
||||
Iterator<Position<Integer>> figliRadice= T.children(T.root()).iterator();
|
||||
Position <Integer> figlioRadice = figliRadice.next();
|
||||
Position<Integer> figlio=null;
|
||||
for(int j=1;j<=3;j++)
|
||||
figlio = T.examAddChild(+(1+j), figlioRadice);
|
||||
T.examAddChild(10, figlio);
|
||||
figliRadice.next();
|
||||
figliRadice.next();
|
||||
figlioRadice = figliRadice.next();
|
||||
Position<Integer> nipoteRadice=null;
|
||||
for(int j=1;j<=3;j++)
|
||||
nipoteRadice=T.examAddChild((9-j), figlioRadice);
|
||||
T.examAddChild(10, figlioRadice);
|
||||
for(int j=11;j>=9;j--)
|
||||
T.examAddChild( j, nipoteRadice);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
System.out.println("\nII test: T e` l'albero disegnato all'inizio del file.");
|
||||
|
||||
for(int k=2;k<6;k++){
|
||||
System.out.print("\nI primi "+ k +" nodi contenenti 10 sono: ");
|
||||
|
||||
for(Position<Integer> p: select(T,10,k)){
|
||||
if(T.isRoot(p))System.out.print("\nil nodo radice, ");
|
||||
else System.out.print("\nil nodo avente come padre il nodo contenente "+ T.parent(p).element());
|
||||
if(T.isInternal(p)){
|
||||
System.out.print(" e come figli i nodi contenenti gli elementi ");
|
||||
for(Position<Integer> child : T.children(p))
|
||||
System.out.print(child.element()+", ");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <E> void exec_select (Tree<E> T, Position<E> start, E x, int k, PositionList<Position<E>> l) {
|
||||
|
||||
if (start.element().equals(x) && l.size() < k)
|
||||
l.addLast(start);
|
||||
if (l.size() < k && !T.isExternal(start)){
|
||||
Iterator<Position<E>>it = T.children(start).iterator();
|
||||
for (;it.hasNext();){
|
||||
Position<E> t = it.next();
|
||||
exec_select(T,t,x,k,l);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//scrivere qui la funzione
|
||||
public static <E> Iterable <Position<E>> select(Tree<E> T, E x, int k){
|
||||
if (T.isEmpty()) throw new EmptyTreeException();
|
||||
NodePositionList<Position<E>> to_return = new NodePositionList<Position<E>>();
|
||||
exec_select(T,T.root(),x ,k,to_return);
|
||||
return to_return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class ExamTree<E> extends LinkedTree<E> implements Tree<E>{
|
||||
|
||||
public Position<E> examAddRoot(E e){// throws NonEmptyTreeException {
|
||||
// if(!isEmpty())
|
||||
// throw new NonEmptyTreeException("L'albero ha gia` una radice");
|
||||
size = 1;
|
||||
root = examCreateNode(e,null,null);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
//aggiunge a v un figlio che ha come elemento element
|
||||
public Position <E> examAddChild(E element,Position <E> v){
|
||||
size++;
|
||||
Position<E> newP= examCreateNode(element,(TreePosition<E>) v,null);
|
||||
if(isExternal(v)){
|
||||
PositionList<Position<E>> figli = new NodePositionList<Position<E>>();
|
||||
((TreePosition<E>) v).setChildren(figli);
|
||||
figli.addLast(newP);}
|
||||
else((PositionList<Position<E>>)children(v)).addLast(newP);
|
||||
return newP;
|
||||
}
|
||||
|
||||
protected TreePosition<E> examCreateNode(E element, TreePosition<E> parent,
|
||||
PositionList<Position<E>> children) {
|
||||
return new TreeNode<E>(element,parent,children);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
82
test_2_prova_intercorso/General.java
Normal file
82
test_2_prova_intercorso/General.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package test_2_prova_intercorso;
|
||||
|
||||
import exceptions.InvalidKeyException;
|
||||
import graph.ComponentsBFS;
|
||||
import graph.Edge;
|
||||
import graph.Graph;
|
||||
import graph.Vertex;
|
||||
import partition.ListPartition;
|
||||
import partition.Partition;
|
||||
import position.NodePositionList;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 01/06/2014
|
||||
* Time: 17:32
|
||||
*/
|
||||
public class General {
|
||||
|
||||
public static <V,E> Partition<Vertex<V>> findConnectedComponentsl(Graph<V,E> G){
|
||||
|
||||
|
||||
Partition<Vertex<V>> p = new ListPartition<Vertex<V>>();
|
||||
|
||||
Object VISITED = new Object();
|
||||
Object UNVISITED = new Object();
|
||||
Object STATUS =
|
||||
for (Vertex<V> v : G.vertices()) {
|
||||
v.put()
|
||||
}
|
||||
|
||||
|
||||
|
||||
layers.add(0, new NodePositionList<Vertex<V>>());
|
||||
layers.get(0).addLast(v);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (!layers.get(i).isEmpty()) {
|
||||
layers.add(i + 1, new NodePositionList<Vertex<V>>());
|
||||
|
||||
for (Vertex<V> vertexInLayer : layers.get(i)) {
|
||||
for (Edge<E> e : graph.incidentEdges(vertexInLayer)) {
|
||||
if (!isVisited(e)) {
|
||||
visit(e);
|
||||
Vertex<V> w = graph.opposite(vertexInLayer, e);
|
||||
if (!isVisited(w)) {
|
||||
traverseDiscovery(e, vertexInLayer);
|
||||
|
||||
if (isDone())
|
||||
break;
|
||||
|
||||
visit(w);
|
||||
layers.get(i + 1).addLast(w);
|
||||
|
||||
if (isDone())
|
||||
break;
|
||||
} else {
|
||||
traverseCross(e, v);
|
||||
if (isDone())
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isDone())
|
||||
finishVisit(vertexInLayer);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
return result();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -153,7 +153,7 @@ public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E> {
|
||||
|
||||
|
||||
// internal class
|
||||
protected static class BTPos<E> implements Position<E> {
|
||||
public static class BTPos<E> implements Position<E> {
|
||||
E element; int index;
|
||||
|
||||
public BTPos(E elt, int i) {
|
||||
|
||||
Reference in New Issue
Block a user