From 7f2845fe2d8522a03199bc84ff5852993dd570bc Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Tue, 3 Jun 2014 23:12:17 +0200 Subject: [PATCH] Aggiunti alcuni esercizi --- com/xgiovio/Main.java | 22 - com/xgiovio/NodeSequenceTest.java | 3 +- .../BinarySearchTreeWithOtherMethods.java | 205 +++++++++ ...daptablePriorityQueueWithOtherMethods.java | 159 +++++++ .../LinkedBinaryTreeWithOtherMethods.java | 359 +++++++++++++++ .../esercizi/LinkedTreeWithOtherMethods.java | 277 ++++++++++++ .../OrderedListSetWithOtherMethods.java | 228 ++++++++++ com/xgiovio/esercizi/PQStack.java | 45 ++ .../xgiovio/esercizi}/QStack.java | 2 +- .../xgiovio/esercizi}/SQueue.java | 3 +- .../{ => esercizi}/StackCheckParenthesis.java | 2 +- .../{ => esercizi}/StackInvertString.java | 2 +- .../esercizi/start_static_methods.java | 4 +- com/xgiovio/esercizi/static_methods.java | 419 ++++++++++++++++++ com/xgiovio/global.java | 58 --- .../ExBinaryTree_11_2_13.java | 2 +- .../ExGraph_15_6PerEserc.java | 2 +- .../test_2_prova_intercorso}/ExMap15_1.java | 3 +- .../ExPQ_21_2_11.java | 2 +- .../ExTree_17_1_2013.java | 2 +- .../Other_Functions.java | 4 +- .../EmptyMyADT_Exception.java | 2 +- .../ExStack_17_4_13.java | 2 +- .../xgiovio/test_prova_intercorso}/MyADT.java | 2 +- .../test_prova_intercorso}/QueueMyADT.java | 2 +- .../xgiovio/test_prova_intercorso}/test.java | 2 +- esercizi/static_methods.java | 193 -------- graph/DFS.java | 4 +- graph/Dijkstra.java | 2 +- graph/FindCycleDFS.java | 2 +- graph/FindPathDFS.java | 8 +- tree/LinkedTree.java | 27 -- 32 files changed, 1719 insertions(+), 330 deletions(-) delete mode 100644 com/xgiovio/Main.java create mode 100644 com/xgiovio/esercizi/BinarySearchTreeWithOtherMethods.java create mode 100644 com/xgiovio/esercizi/HeapAdaptablePriorityQueueWithOtherMethods.java create mode 100644 com/xgiovio/esercizi/LinkedBinaryTreeWithOtherMethods.java create mode 100644 com/xgiovio/esercizi/LinkedTreeWithOtherMethods.java create mode 100644 com/xgiovio/esercizi/OrderedListSetWithOtherMethods.java create mode 100644 com/xgiovio/esercizi/PQStack.java rename {esercizi => com/xgiovio/esercizi}/QStack.java (97%) rename {esercizi => com/xgiovio/esercizi}/SQueue.java (97%) rename com/xgiovio/{ => esercizi}/StackCheckParenthesis.java (98%) rename com/xgiovio/{ => esercizi}/StackInvertString.java (96%) rename esercizi/start.java => com/xgiovio/esercizi/start_static_methods.java (95%) create mode 100644 com/xgiovio/esercizi/static_methods.java delete mode 100644 com/xgiovio/global.java rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/ExBinaryTree_11_2_13.java (99%) rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/ExGraph_15_6PerEserc.java (99%) rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/ExMap15_1.java (98%) rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/ExPQ_21_2_11.java (99%) mode change 100755 => 100644 rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/ExTree_17_1_2013.java (99%) rename {test_2_prova_intercorso => com/xgiovio/test_2_prova_intercorso}/Other_Functions.java (95%) rename {test_prova_intercorso => com/xgiovio/test_prova_intercorso}/EmptyMyADT_Exception.java (74%) rename {test_prova_intercorso => com/xgiovio/test_prova_intercorso}/ExStack_17_4_13.java (98%) rename {test_prova_intercorso => com/xgiovio/test_prova_intercorso}/MyADT.java (92%) rename {test_prova_intercorso => com/xgiovio/test_prova_intercorso}/QueueMyADT.java (96%) rename {test_prova_intercorso => com/xgiovio/test_prova_intercorso}/test.java (90%) delete mode 100644 esercizi/static_methods.java diff --git a/com/xgiovio/Main.java b/com/xgiovio/Main.java deleted file mode 100644 index 6d2d0a0..0000000 --- a/com/xgiovio/Main.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.xgiovio; - -import position.NodePositionList; - -import java.util.Iterator; - -public class Main { - - public static void main(String[] args) { - - NodePositionList a = new NodePositionList(); - a.addLast(1); - a.addLast(2); - Iterator it = a.iterator(); - a.addLast(3); - a.addLast(4); - - for (;it.hasNext();) - System.out.println(it.next()); - - } -} diff --git a/com/xgiovio/NodeSequenceTest.java b/com/xgiovio/NodeSequenceTest.java index 4b6ad11..8035676 100644 --- a/com/xgiovio/NodeSequenceTest.java +++ b/com/xgiovio/NodeSequenceTest.java @@ -2,6 +2,7 @@ package com.xgiovio; import com.xgiovio.general_utility.test_object; import sequence.NodeSequence; +import com.xgiovio.esercizi.*; /** * Created with xgiovio.macbookair. @@ -52,7 +53,7 @@ public class NodeSequenceTest { a.set(6,new test_object(200)); System.out.print(a); - global.reverse(a); + static_methods.reverse(a); System.out.print(a); diff --git a/com/xgiovio/esercizi/BinarySearchTreeWithOtherMethods.java b/com/xgiovio/esercizi/BinarySearchTreeWithOtherMethods.java new file mode 100644 index 0000000..4134e6b --- /dev/null +++ b/com/xgiovio/esercizi/BinarySearchTreeWithOtherMethods.java @@ -0,0 +1,205 @@ +package com.xgiovio.esercizi; + +import dictionary.Dictionary; +import exceptions.InvalidEntryException; +import exceptions.InvalidKeyException; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import priorityqueue.Entry; +import tree.binarytree.LinkedBinaryTree; +import utility.DefaultComparator; + +import java.util.Comparator; + + +public class BinarySearchTreeWithOtherMethods extends LinkedBinaryTree> implements Dictionary { + + protected Comparator C; // comparator + protected Position> actionPos; // insert node or removed node's parent + protected int numEntries = 0; // number of entries + + public BinarySearchTreeWithOtherMethods() { + C = new DefaultComparator(); + addRoot(null); + } + + public BinarySearchTreeWithOtherMethods(Comparator c) { + C = c; + addRoot(null); + } + + /** Nested class for location-aware binary search tree entries */ + protected static class BSTEntry implements Entry { + protected K key; + protected V value; + protected Position> pos; + BSTEntry(K k, V v, Position> p) { + key = k; value = v; pos = p; + } + public K getKey() { return key; } + public V getValue() { return value; } + public Position> position() { return pos; } + } + + /** Extracts the key of the entry at a given node of the tree. */ + protected K key(Position> position) { + return position.element().getKey(); + } + + /** Extracts the value of the entry at a given node of the tree. */ + protected V value(Position> position) { + return position.element().getValue(); + } + + /** Extracts the entry at a given node of the tree. */ + protected Entry entry(Position> position) { + return position.element(); + } + + /** Replaces an entry with a new entry (and reset the entry's location) */ + protected void replaceEntry(Position > pos, Entry ent) { + ((BSTEntry) ent).pos = pos; + replace(pos, ent); + } + + +protected void checkKey(K key) throws InvalidKeyException { + if(key == null) + throw new InvalidKeyException("null key"); +} + +protected void checkEntry(Entry ent) throws InvalidEntryException { + if(ent == null || !(ent instanceof BSTEntry)) + throw new InvalidEntryException("invalid entry"); +} + +protected Entry insertAtExternal(Position> v, Entry e) { + expandExternal(v,null,null); + replace(v, e); + numEntries++; + return e; +} +/** Auxiliary method for removing an external node and its parent */ +protected void removeExternal(Position> v) { + removeAboveExternal(v); + numEntries--; +} +/** Auxiliary method used by find, insert, and remove. */ +protected Position> treeSearch(K key, Position> pos) { + if (isExternal(pos)) return pos; // key not found; return external node + else { + K curKey = key(pos); + int comp = C.compare(key, curKey); + if (comp < 0) + return treeSearch(key, left(pos)); // search left subtree + else if (comp > 0) + return treeSearch(key, right(pos)); // search right subtree + return pos; // return internal node where key is found + } +} + + +// Adds to L all entries in the subtree rooted at v having keys equal to k +protected void addAll(PositionList> L, Position> v, K k) { + if (isExternal(v)) return; + Position> pos = treeSearch(k, v); + if (!isExternal(pos)) { // we found an entry with key equal to k + addAll(L, left(pos), k); + L.addLast(pos.element()); // add entries in inorder + addAll(L, right(pos), k); + } // this recursive algorithm is simple, but it's not the fastest +} + + /** Returns the number of entries in the tree. */ + public int size() { return numEntries; } + + /** Returns whether the tree is empty. */ + public boolean isEmpty() { return size() == 0; } + + /** Returns an entry containing the given key. Returns null if no such entry exists. */ + public Entry find(K key) throws InvalidKeyException { + checkKey(key); // may throw an InvalidKeyException + Position> curPos = treeSearch(key, root()); + actionPos = curPos; // node where the search ended + if (isInternal(curPos)) return entry(curPos); + return null; + } + + /** Returns an iterable collection of all the entries containing the given key. */ + public Iterable> findAll(K key) throws InvalidKeyException { + checkKey(key); // may throw an InvalidKeyException + PositionList> L = new NodePositionList>(); + addAll(L, root(), key); + return L; + } + + /** Inserts an entry into the tree and returns the newly created entry. */ + public Entry insert(K k, V x) throws InvalidKeyException { + checkKey(k); // may throw an InvalidKeyException + Position> insPos = treeSearch(k, root()); + while (!isExternal(insPos)) // iterative search for insertion position + insPos = treeSearch(k, left(insPos)); + actionPos = insPos; // node where the new entry is being inserted + return insertAtExternal(insPos, new BSTEntry(k, x, insPos)); + } + + /** Removes and returns a given entry. */ + public Entry remove(Entry ent) throws InvalidEntryException { + checkEntry(ent); // may throw an InvalidEntryException + Position> remPos = ((BSTEntry) ent).position(); + Entry toReturn = entry(remPos); // entry to be returned + if (isExternal(left(remPos))) remPos = left(remPos); // left easy case + else if (isExternal(right(remPos))) remPos = right(remPos); // right easy case + else { // entry is at a node with internal children + Position> swapPos = remPos; // find node for moving entry + remPos = right(swapPos); + do + remPos = left(remPos); + while (isInternal(remPos)); + replaceEntry(swapPos, (Entry) parent(remPos).element()); + } + actionPos = sibling(remPos); // sibling of the leaf to be removed + removeExternal(remPos); + return toReturn; + } + + /** Returns an iterator containing all entries in the tree. */ + public Iterable> entries() { + PositionList> entries = new NodePositionList>(); + Iterable>> positer = positions(); + for (Position> cur: positer) + if (isInternal(cur)) + entries.addLast(cur.element()); + return entries; + } + + protected class Integer_Wrapper{ + public int value = 0; + } + + + public Iterable withSmallestKeys(int k){ + PositionList to_return = new NodePositionList(); + Integer_Wrapper counter = new Integer_Wrapper(); + exec_withSmallestKeys (k,counter,root(),to_return); + return to_return; + } + + + protected void exec_withSmallestKeys (int k,Integer_Wrapper counter, Position> p, PositionList l ){ + if (isInternal(left(p))){ + exec_withSmallestKeys(k,counter,left(p),l); + } + if (counter.value < k) { + l.addLast(p.element().getValue()); + counter.value++; + } + if (counter.value < k && isInternal(right(p)) ) { + exec_withSmallestKeys(k,counter,right(p),l); + } + + } + +} + diff --git a/com/xgiovio/esercizi/HeapAdaptablePriorityQueueWithOtherMethods.java b/com/xgiovio/esercizi/HeapAdaptablePriorityQueueWithOtherMethods.java new file mode 100644 index 0000000..dfec20e --- /dev/null +++ b/com/xgiovio/esercizi/HeapAdaptablePriorityQueueWithOtherMethods.java @@ -0,0 +1,159 @@ +package com.xgiovio.esercizi; + +import exceptions.InvalidEntryException; +import exceptions.InvalidKeyException; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import priorityqueue.AdaptablePriorityQueue; +import priorityqueue.Entry; +import priorityqueue.MyEntry; +import priorityqueue.heap.HeapPriorityQueue; + +import java.util.Comparator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 11/05/2014 + * Time: 14:55 + */ +public class HeapAdaptablePriorityQueueWithOtherMethods extends HeapPriorityQueue implements AdaptablePriorityQueue { + + public HeapAdaptablePriorityQueueWithOtherMethods() { + super(); + } + public HeapAdaptablePriorityQueueWithOtherMethods(Comparator comp) { + super(comp); + } + + //override from parent class + public Entry insert (K k, V v) throws InvalidKeyException { + checkKey(k); + LocationAwareEntry entry = new LocationAwareEntry(k,v); + Position > z = heap.add(entry); + entry.setLocation(z); + upHeap(z); + return entry; + } + + protected void swap(Position> u,Position> v) { + super.swap(u,v); + getEntry(u).setLocation(u); + getEntry(v).setLocation(v); + } + + //end override from parent class + + + + + + public Entry remove(Entry entry) throws InvalidEntryException { + LocationAwareEntry ee = checkEntry(entry); + Position < Entry> p = ee.location(); + if(size() == 1) + return (Entry) heap.remove(); + replaceEntry(p,(LocationAwareEntry)heap.remove()); + upHeap(p); + downHeap(p); + ee.setLocation(null); + return ee; + } + + + + + public K replaceKey(Entry entry, K k) throws InvalidEntryException,InvalidKeyException + { + checkKey(k); + LocationAwareEntry ee = checkEntry(entry); + K oldKey = ee.setKey(k); + upHeap(ee.location()); + downHeap(ee.location()); + return oldKey; + } + + public V replaceValue(Entry e, V value) throws InvalidEntryException + { + LocationAwareEntry ee = checkEntry(e); + return ee.setValue(value); + } + + + //auxiliar + + protected Position< Entry> replaceEntry(Position> v, LocationAwareEntry e) { + heap.replace(v,e); + return e.setLocation(v); + } + + protected LocationAwareEntry getEntry(Position >p) { + return (LocationAwareEntry) p.element(); + } + + protected LocationAwareEntry checkEntry(Entry e) throws InvalidEntryException + { + if(e == null || !(e instanceof LocationAwareEntry) || isEmpty() ) + throw new InvalidEntryException("entrata non valida"); + return (LocationAwareEntry) e; + } + + // inner class + protected static class LocationAwareEntry extends MyEntry { + protected Position> loc = null; + + public LocationAwareEntry(K k, V v) { + super(k, v); + } + + public LocationAwareEntry(K k, V v, Position pos) { + super(k, v); + loc = pos; + } + + + + protected Position < Entry> location() { + return loc; + } + protected Position < Entry >setLocation(Position< Entry> pos) { + Position > oldPosition = location(); + loc = pos; + return oldPosition; + } + protected K setKey(K k) { + K oldKey = getKey(); + key = k; + return oldKey; + } + protected V setValue(V v) { + V oldValue = getValue(); + value = v; + return oldValue; + } + + public String toString() { return "(" + key + "," + value + ")"; } + + + + } + + public String toString() { + return super.toString(); + } + + public Iterable >> insertEntries(K[ ] S1,V[ ]S2) { + + PositionList>> to_return = new NodePositionList>>(); + + for (int i = 0; i< S1.length ; i++ ){ + to_return.addLast (((LocationAwareEntry)(insert(S1[i],S2[i]))).location()); + } + + return to_return; + + + } + +} diff --git a/com/xgiovio/esercizi/LinkedBinaryTreeWithOtherMethods.java b/com/xgiovio/esercizi/LinkedBinaryTreeWithOtherMethods.java new file mode 100644 index 0000000..2463039 --- /dev/null +++ b/com/xgiovio/esercizi/LinkedBinaryTreeWithOtherMethods.java @@ -0,0 +1,359 @@ +package com.xgiovio.esercizi; +import exceptions.BoundaryViolationException; +import exceptions.EmptyTreeException; +import exceptions.InvalidPositionException; +import exceptions.NonEmptyTreeException; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import tree.binarytree.BTNode; +import tree.binarytree.BTPosition; +import tree.binarytree.BinaryTree; + +import java.util.Iterator; + + +public class LinkedBinaryTreeWithOtherMethods implements BinaryTree { + protected BTPosition root; + protected int size; + + public LinkedBinaryTreeWithOtherMethods() { + root = null; + size = 0; + } + + + public int size() { + return size; + } + + public boolean isEmpty() { + return (size == 0); + } + + + public boolean isInternal(Position v) throws InvalidPositionException { + checkPosition(v); + return (hasLeft(v) || hasRight(v)); + } + + public boolean isExternal(Position v) throws InvalidPositionException { + return !isInternal(v); + } + + public boolean isRoot(Position v) throws InvalidPositionException { + checkPosition(v); + return (v == root()); + } + + public boolean hasLeft(Position v) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + return (vv.getLeft() != null); + } + + public boolean hasRight(Position v) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + return (vv.getRight() != null); + } + + + public Position root() throws EmptyTreeException { + if (root == null) + throw new EmptyTreeException("The tree is empty"); + return root; + } + + + public Position left(Position v) throws InvalidPositionException, BoundaryViolationException { + BTPosition vv = checkPosition(v); + Position leftPos = vv.getLeft(); + if (leftPos == null) + throw new BoundaryViolationException("No left child"); + return leftPos; + } + + + public Position right(Position v) + throws InvalidPositionException, BoundaryViolationException { + BTPosition vv = checkPosition(v); + Position rightPos = vv.getRight(); + if (rightPos == null) + throw new BoundaryViolationException("No right child"); + return rightPos; + } + + + public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException { + BTPosition vv = checkPosition(v); + Position parentPos = vv.getParent(); + if (parentPos == null) + throw new BoundaryViolationException("No parent"); + return parentPos; + } + + + public Iterable> children(Position v) throws InvalidPositionException { + PositionList> children = new NodePositionList>(); + if (hasLeft(v)) + children.addLast(left(v)); + if (hasRight(v)) + children.addLast(right(v)); + return children; + } + + public Iterable> positions() { + PositionList> positions = new NodePositionList>(); + if(size != 0) + preorderPositions(root(), positions); + return positions; + } + + public Iterator iterator() { + Iterable> positions = positions(); + PositionList elements = new NodePositionList(); + for (Position pos: positions) + elements.addLast(pos.element()); + return elements.iterator(); + } + + public E replace(Position v, E o) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + E temp = v.element(); + vv.setElement(o); + return temp; + } + + + public Position sibling(Position v) throws InvalidPositionException, BoundaryViolationException { + BTPosition vv = checkPosition(v); + BTPosition parentPos = vv.getParent(); + if (parentPos != null) { + BTPosition sibPos; + BTPosition leftPos = parentPos.getLeft(); + if (leftPos == vv) + sibPos = parentPos.getRight(); + else + sibPos = parentPos.getLeft(); + if (sibPos != null) + return sibPos; + } + throw new BoundaryViolationException("No sibling"); + } + + + public Position addRoot(E e) throws NonEmptyTreeException { + if(!isEmpty()) + throw new NonEmptyTreeException("Tree already has a root"); + size = 1; + root = createNode(e,null,null,null); + return root; + } + + + public Position insertLeft(Position v, E e) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + Position leftPos = vv.getLeft(); + if (leftPos != null) + throw new InvalidPositionException("Node already has a left child"); + BTPosition ww = createNode(e, vv, null, null); + vv.setLeft(ww); + size++; + return ww; + } + + public Position insertRight(Position v, E e) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + Position rightPos = vv.getRight(); + if (rightPos != null) + throw new InvalidPositionException("Node already has a right child"); + BTPosition w = createNode(e, vv, null, null); + vv.setRight(w); + size++; + return w; + } + + public E remove(Position v) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + BTPosition leftPos = vv.getLeft(); + BTPosition rightPos = vv.getRight(); + if (leftPos != null && rightPos != null) + throw new InvalidPositionException("Cannot remove node with two children"); + BTPosition ww; + if (leftPos != null) + ww = leftPos; + else if (rightPos != null) + ww = rightPos; + else + ww = null; + if (vv == root) { + if (ww != null) + ww.setParent(null); + root = ww; + } + else { + BTPosition uu = vv.getParent(); + if (vv == uu.getLeft()) + uu.setLeft(ww); + else + uu.setRight(ww); + if(ww != null) + ww.setParent(uu); + } + size--; + return v.element(); + } + + + /** Attaches two trees to be subtrees of an external node. */ + public void attach(Position v, BinaryTree T1, BinaryTree T2) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + if (isInternal(v)) + throw new InvalidPositionException("Cannot attach from internal node"); + int newSize = size + T1.size() + T2.size(); + if (!T1.isEmpty()) { + BTPosition r1 = checkPosition(T1.root()); + vv.setLeft(r1); + r1.setParent(vv); + } + if (!T2.isEmpty()) { + BTPosition r2 = checkPosition(T2.root()); + vv.setRight(r2); + r2.setParent(vv); + } + size = newSize; + } + + public void swapElements(Position v, Position w) throws InvalidPositionException { + BTPosition vv = checkPosition(v); + BTPosition ww = checkPosition(w); + E temp = w.element(); + ww.setElement(v.element()); + vv.setElement(temp); + } + + public void expandExternal(Position v, E l, E r) throws InvalidPositionException { + checkPosition(v); + if (!isExternal(v)) + throw new InvalidPositionException("Node is not external"); + insertLeft(v, l); + insertRight(v, r); + } + + public void removeAboveExternal(Position v) throws InvalidPositionException { + checkPosition(v); + if (!isExternal(v)) + throw new InvalidPositionException("Node is not external"); + if (isRoot(v)) + remove(v); + else { + Position u = parent(v); + remove(v); + remove(u); + } + } + + + public void attachLeaves (PositionList L) { + if (!isEmpty()) + attachLeaves_r(root(),L); + } + + + + protected void attachLeaves_r(Position v, PositionList L) throws InvalidPositionException { + BTPositionvv = checkPosition(v); + + + if (hasLeft(v)) + attachLeaves_r(left(v), L); + if (hasRight(v)) + attachLeaves_r(right(v), L); + + if (isExternal(v)){ + BTPosition a = createNode( L.remove(L.first()) ,vv,null,null); + BTPosition b = createNode( L.remove(L.first()) ,vv,null,null); + vv.setLeft(a); + vv.setRight(b); + size+=2; + } + + } + + + // Other methods + + protected BTPosition checkPosition(Position v) throws InvalidPositionException { + if (v == null || !(v instanceof BTPosition) || isEmpty() ) + throw new InvalidPositionException("The position is invalid"); + return (BTPosition) v; + } + + protected BTPosition createNode(E element, BTPosition parent, BTPosition left, BTPosition right) { + return new BTNode(element,parent,left,right); } + + + + protected void preorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + pos.addLast(v); + if (hasLeft(v)) + preorderPositions(left(v), pos); + if (hasRight(v)) + preorderPositions(right(v), pos); + } + + + protected void inorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + if (hasLeft(v)) + inorderPositions(left(v), pos); + pos.addLast(v); + if (hasRight(v)) + inorderPositions(right(v), pos); + } + + protected void postorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + if (hasLeft(v)) + inorderPositions(left(v), pos); + if (hasRight(v)) + inorderPositions(right(v), pos); + pos.addLast(v); + } + + + public String toString() { + String to_return = ""; + to_return = to_return + "["; + if (size() == 0 ) + return to_return+= "]"; + + NodePositionList> t = new NodePositionList>(); + postorderPositions(root(),t); + + for (Position w :t){ + to_return+=w.element() + ","; + } + to_return = to_return.substring(0, to_return.length()-1); + + return to_return+= "]"; + } + + public void attachLeaves() { + if (size() > 1) + exec_attachLeaves (root()); + } + + + protected void exec_attachLeaves (Position p) { + BTPosition pp = (BTPosition) p; + if (isInternal(p)){ + if(pp.getLeft() == null ) + pp.setLeft(createNode(null,pp,null,null)); + else + exec_attachLeaves(pp.getLeft()); + if(pp.getRight() == null ) + pp.setRight(createNode(null,pp,null,null)); + else + exec_attachLeaves(pp.getRight()); + } + } + +} diff --git a/com/xgiovio/esercizi/LinkedTreeWithOtherMethods.java b/com/xgiovio/esercizi/LinkedTreeWithOtherMethods.java new file mode 100644 index 0000000..d19c948 --- /dev/null +++ b/com/xgiovio/esercizi/LinkedTreeWithOtherMethods.java @@ -0,0 +1,277 @@ +package com.xgiovio.esercizi; + +import exceptions.*; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import tree.Tree; +import tree.TreeNode; +import tree.TreePosition; + +import java.util.Iterator; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 07/04/14 + * Time: 15:45 + */ +public class LinkedTreeWithOtherMethods implements Tree { + + protected TreePosition root = null; + protected int size = 0; + + + public int size() { + return size; + } + + public boolean isEmpty() { + return (size == 0); + } + + + public Iterable> positions() { + PositionList> positions = new NodePositionList>(); + if(size != 0) + preorderPositions(root(), positions); + return positions; + } + + public E replace(Position v, E o) throws InvalidPositionException { + TreePosition vv = checkPosition(v); + E temp = v.element(); + vv.setElement(o); + return temp; + } + + public Position root() throws EmptyTreeException { + if (root == null) + throw new EmptyTreeException("The tree is empty"); + return root; + } + + + public Position parent(Position v) throws InvalidPositionException, BoundaryViolationException { + TreePosition vv = checkPosition(v); + Position parentPos = vv.getParent(); + if (parentPos == null) + throw new BoundaryViolationException("No parent"); + return parentPos; + } + + public Iterable> children(Position v) throws InvalidPositionException { + TreePosition vv = checkPosition(v); + if (isExternal(v)) + throw new InvalidPositionException("External nodes have no children"); + return vv.getChildren(); + } + + public boolean isInternal(Position v) throws InvalidPositionException { + return !isExternal(v); + } + + public boolean isExternal(Position v) throws InvalidPositionException { + TreePosition vv = checkPosition(v); + return vv.getChildren().isEmpty(); + } + + public boolean isRoot(Position v) throws InvalidPositionException { + checkPosition(v); + return (v == root()); + } + + public Iterator iterator() { + Iterable> positions = positions(); + PositionList elements = new NodePositionList(); + for (Position pos: positions) + elements.addLast(pos.element()); + return elements.iterator(); + } + + ////////////////////////// help methods + + protected void preorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + + TreePosition a = checkPosition(v); + pos.addLast(a); + + for (Position w : a.getChildren()) + preorderPositions(w, pos); + } + + protected void postorderPositions(Position v, PositionList> pos) throws InvalidPositionException { + + TreePosition a = checkPosition(v); + + + for (Position w : a.getChildren()) + postorderPositions(w, pos); + + pos.addLast(a); + } + + protected TreePosition checkPosition(Position v) + throws InvalidPositionException { + if (v == null || !(v instanceof TreePosition) || isEmpty() ) + throw new InvalidPositionException("The position is invalid"); + return (TreePosition) v; + } + + + + public Position addRoot(E e) throws NonEmptyTreeException { + if(!isEmpty()) + throw new NonEmptyTreeException("Tree already has a root"); + size = 1; + root = createNode(e,null,null); + return root; + } + + public void swapElements(Position v, Position w) + throws InvalidPositionException { + TreePosition vv = checkPosition(v); + TreePosition ww = checkPosition(w); + E temp = w.element(); + ww.setElement(v.element()); + vv.setElement(temp); + } + + protected TreePosition createNode(E element, TreePosition parent, PositionList> children) { + return new TreeNode(element,parent,children); + } + + + public E removeRoot() throws EmptyTreeException, UndeletableNodeException { + if (isEmpty()) + throw new EmptyTreeException(); + if (size() > 1) + throw new UndeletableNodeException(); + E to_return = root().element(); + root = null; + size = 0; + return to_return; + + } + + public E removeExternalChild(Position v) throws UndeletableNodeException,InvalidPositionException{ + TreePosition a = checkPosition(v); + if (isExternal(a)) + throw new InvalidPositionException(); + PositionList> c = a.getChildren(); + if (isInternal(c.first().element())) + throw new UndeletableNodeException(); + + E to_return = (c.remove(c.first())).element(); + size--; + return to_return; + + } + + public Position addChild (E e , Position v) throws InvalidPositionException { + TreePosition a = checkPosition(v); + a.getChildren().addLast(createNode(e,a,null)); + size++; + return a.getChildren().last().element(); + + } + + + + public E remove (Position v) { + TreePosition a = checkPosition(v); + if (isInternal(a)) + throw new InvalidPositionException(); + if (size() == 1) + return removeRoot(); + + PositionList> list = a.getParent().getChildren(); + Iterable>> it = list.positions(); + + for (Position> w : it){ + if (w.element() == v){ + size--; + return (list.remove(w)).element(); + } + } + + return null; + } + + + + + public int depth ( Position v) throws InvalidPositionException{ + TreePosition a = checkPosition(v); + if (a.getParent() == null) + return 0; + return (depth(a.getParent()) + 1); + } + + public int height () throws EmptyTreeException{ + return height_f(root()); + } + + public int height_f ( Position v) { + TreePosition a = checkPosition(v); + if (isExternal(a)) + return 0; + int max = 0; + for ( Position w : a.getChildren()){ + if (max == 0 || height_f(w) > max){ + max = height_f(w); + } + } + return 1 + max; + } + + @Override + public String toString() { + String to_return = ""; + to_return = to_return + "["; + if (size() == 0 ) + return to_return+= "]"; + + NodePositionList> t = new NodePositionList>(); + postorderPositions(root(),t); + + for (Position w :t){ + to_return+=w.element() + ","; + } + to_return = to_return.substring(0, to_return.length()-1); + + return to_return+= "]"; + } + + + public int arity () { + + if (size() <= 1) return 0; + return exec_arity (root()); + } + + + protected int exec_arity (Position v){ + TreePosition vv = checkPosition(v); + if (isExternal(v)) + return 0; + PositionList> c = vv.getChildren(); + int arity = c.size(); + int max = arity; + for (Position p : c){ + max = max(exec_arity(p),max); + } + return max; + + } + + + int max (int a, int b ){ + if (a > b) + return a; + return b; + } + + + +} diff --git a/com/xgiovio/esercizi/OrderedListSetWithOtherMethods.java b/com/xgiovio/esercizi/OrderedListSetWithOtherMethods.java new file mode 100644 index 0000000..588adcc --- /dev/null +++ b/com/xgiovio/esercizi/OrderedListSetWithOtherMethods.java @@ -0,0 +1,228 @@ +package com.xgiovio.esercizi; + +import dictionary.BinarySearchTree; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import priorityqueue.Entry; +import set.OrderedListSet; +import set.Set; +import utility.DefaultComparator; +import utility.merge.MergeTemplate; + +import java.util.Comparator; +import java.util.Iterator; + +/** + * Created with xgiovio.macbookair. + * User: xgiovio + * Date: 14/05/14 + * Time: 14:23 + */ + +public class OrderedListSetWithOtherMethods implements Set { + private Comparator c; + private PositionList L; + private Position> loc; + + + + public OrderedListSetWithOtherMethods() { + L = new NodePositionList(); + c = new DefaultComparator(); + } + + public OrderedListSetWithOtherMethods(Comparator in_c) { + L = new NodePositionList(); + c = in_c; + } + + public OrderedListSetWithOtherMethods(PositionList in_l){ + L = in_l; + c = new DefaultComparator(); + } + + public OrderedListSetWithOtherMethods(PositionList in_l, Comparator in_c){ + L = in_l; + c = in_c; + } + + public OrderedListSetWithOtherMethods(E in_e, Comparator in_c){ + L = new NodePositionList(); + L.addLast(in_e); + c = in_c; + } + + public OrderedListSetWithOtherMethods(E in_e){ + L = new NodePositionList(); + L.addLast(in_e); + c = new DefaultComparator(); + } + + + public OrderedListSetWithOtherMethods(Comparator c, BinarySearchTree D ) { + + this.c = c; + L = new NodePositionList(); + if (!D.isEmpty()) { + L.addLast(D.root().element().getKey()); + exec_OrderedListSetWithOtherMethods(D, D.root()); + } + } + + public void exec_OrderedListSetWithOtherMethods (BinarySearchTree D , Position> p){ + if (D.isInternal(D.left(p))) { + union(new OrderedListSetWithOtherMethods(D.left(p).element().getKey())); + exec_OrderedListSetWithOtherMethods(D, D.left(p)); + } + if (D.isInternal(D.right(p))) { + union(new OrderedListSetWithOtherMethods(D.right(p).element().getKey())); + exec_OrderedListSetWithOtherMethods(D, D.right(p)); + } + } + + + + @Override + public int size() { + return L.size(); + } + + @Override + public boolean isEmpty() { + return L.isEmpty(); + } + + + + + @Override + public Set union(Set B) { + MergeUnion ret = new MergeUnion(L, ((OrderedListSetWithOtherMethods)B).L , c ); + L = ret.getResult(); + return this; + + } + + @Override + public Set intersect(Set B) { + MergeIntersect ret = new MergeIntersect(L, ((OrderedListSetWithOtherMethods)B).L , c ); + L = ret.getResult(); + return this; + } + + @Override + public Set subtract(Set B) { + MergeSubtract ret = new MergeSubtract(L, ((OrderedListSetWithOtherMethods)B).L , c ); + L = ret.getResult(); + return this; + } + + @Override + public String toString() { + return L.toString(); + } + + + + public Set fastUnion(Set B){ + OrderedListSetWithOtherMethods BB = (OrderedListSetWithOtherMethods) B; + Iterator it = BB.L.iterator(); + for (;it.hasNext();) + L.addLast(it.next()); + return this; + } + + + public E fastInsert(E x) { + L.addLast(x); + return x; + } + + + public Position> location (){ + return loc; + } + + public void setLocation (Position> in){ + loc = in; + } + + public PositionList list (){ + return L; + } + + + //////////////////////// inner class override methods from merge template ///// + + protected class MergeUnion extends MergeTemplate{ + + public MergeUnion (PositionList A , PositionList B, Comparator c){ + super(A,B,c); + } + + @Override + protected void aIsLess(E a) { + s.addLast(a); + } + + @Override + protected void bIsLess(E b) { + s.addLast(b); + } + + @Override + protected void bothAreEqual(E a, E b) { + s.addLast(a); + } + } + + protected class MergeIntersect extends MergeTemplate{ + + public MergeIntersect (PositionList A , PositionList B, Comparator c){ + super(A,B,c); + } + + @Override + protected void aIsLess(E a) { + + } + + @Override + protected void bIsLess(E b) { + + } + + @Override + protected void bothAreEqual(E a, E b) { + s.addLast(a); + } + } + + protected class MergeSubtract extends MergeTemplate{ + + public MergeSubtract (PositionList A , PositionList B, Comparator c){ + super(A,B,c); + } + + @Override + protected void aIsLess(E a) { + s.addLast(a); + } + + @Override + protected void bIsLess(E b) { + + } + + @Override + protected void bothAreEqual(E a, E b) { + + } + } + + + + + +} \ No newline at end of file diff --git a/com/xgiovio/esercizi/PQStack.java b/com/xgiovio/esercizi/PQStack.java new file mode 100644 index 0000000..8d7a3d1 --- /dev/null +++ b/com/xgiovio/esercizi/PQStack.java @@ -0,0 +1,45 @@ +package com.xgiovio.esercizi; + +import exceptions.EmptyStackException; +import priorityqueue.PriorityQueue; +import priorityqueue.heap.HeapPriorityQueue; +import stack.Stack; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 03/06/2014 + * Time: 13:50 + */ +public class PQStack implements Stack { + + private int priority = 0; + private PriorityQueue p = new HeapPriorityQueue(); + + + @Override + public void push(E element) { + priority--; + p.insert(priority,element); + } + + @Override + public E top() throws EmptyStackException { + return p.min().getValue(); + } + + @Override + public E pop() throws EmptyStackException { + return p.removeMin().getValue(); + } + + @Override + public boolean isEmpty() { + return (size()== 0); + } + + @Override + public int size() { + return p.size(); + } +} diff --git a/esercizi/QStack.java b/com/xgiovio/esercizi/QStack.java similarity index 97% rename from esercizi/QStack.java rename to com/xgiovio/esercizi/QStack.java index fb82e3a..ea36bfd 100644 --- a/esercizi/QStack.java +++ b/com/xgiovio/esercizi/QStack.java @@ -1,4 +1,4 @@ -package esercizi; +package com.xgiovio.esercizi; import exceptions.EmptyStackException; import queue.NodeQueue; diff --git a/esercizi/SQueue.java b/com/xgiovio/esercizi/SQueue.java similarity index 97% rename from esercizi/SQueue.java rename to com/xgiovio/esercizi/SQueue.java index b983158..6bc25e9 100644 --- a/esercizi/SQueue.java +++ b/com/xgiovio/esercizi/SQueue.java @@ -1,8 +1,7 @@ -package esercizi; +package com.xgiovio.esercizi; import exceptions.EmptyQueueException; import queue.Queue; -import sequence.ArraySequence; import stack.ArrayStack; /** diff --git a/com/xgiovio/StackCheckParenthesis.java b/com/xgiovio/esercizi/StackCheckParenthesis.java similarity index 98% rename from com/xgiovio/StackCheckParenthesis.java rename to com/xgiovio/esercizi/StackCheckParenthesis.java index b024d30..af41dea 100644 --- a/com/xgiovio/StackCheckParenthesis.java +++ b/com/xgiovio/esercizi/StackCheckParenthesis.java @@ -1,4 +1,4 @@ -package com.xgiovio; +package com.xgiovio.esercizi; import exceptions.EmptyStackException; import stack.NodeStack; diff --git a/com/xgiovio/StackInvertString.java b/com/xgiovio/esercizi/StackInvertString.java similarity index 96% rename from com/xgiovio/StackInvertString.java rename to com/xgiovio/esercizi/StackInvertString.java index 3542b84..ab822ad 100644 --- a/com/xgiovio/StackInvertString.java +++ b/com/xgiovio/esercizi/StackInvertString.java @@ -1,4 +1,4 @@ -package com.xgiovio; +package com.xgiovio.esercizi; import stack.NodeStack; diff --git a/esercizi/start.java b/com/xgiovio/esercizi/start_static_methods.java similarity index 95% rename from esercizi/start.java rename to com/xgiovio/esercizi/start_static_methods.java index abdc84d..f443663 100644 --- a/esercizi/start.java +++ b/com/xgiovio/esercizi/start_static_methods.java @@ -1,4 +1,4 @@ -package esercizi; +package com.xgiovio.esercizi; /** * Created with MONSTER. @@ -6,7 +6,7 @@ package esercizi; * Date: 13/04/2014 * Time: 22:18 */ -public class start { +public class start_static_methods { public static void main(String[] args) { diff --git a/com/xgiovio/esercizi/static_methods.java b/com/xgiovio/esercizi/static_methods.java new file mode 100644 index 0000000..5fa0c92 --- /dev/null +++ b/com/xgiovio/esercizi/static_methods.java @@ -0,0 +1,419 @@ +package com.xgiovio.esercizi; + +import dictionary.Dictionary; +import map.ListMap; +import map.Map; +import position.NodePositionList; +import position.Position; +import position.PositionList; +import priorityqueue.Entry; +import priorityqueue.PriorityQueue; +import priorityqueue.SortedListPriorityQueue; +import priorityqueue.heap.HeapPriorityQueue; +import queue.NodeQueue; +import queue.Queue; +import sequence.Sequence; +import stack.ArrayStack; +import exceptions.*; +import tree.Tree; + +import java.util.Comparator; + +/** + * Created with MONSTER. + * User: xgiovio + * Date: 13/04/2014 + * Time: 22:19 + */ +public class static_methods { + + + + // inver a string using a stack + public static String inverti (String s){ + ArrayStack a = new ArrayStack(); + for (int i = 0 ;i< s.length();i++){ + a.push(s.charAt(i)); + } + + String to_return = ""; + + for (int i = 0 ;i< s.length();i++){ + to_return += a.pop(); + } + + return to_return; + + } + + // verify is a string of [ and ( is correct using a stack + public static boolean checkParentheses (String s) { + + ArrayStack a = new ArrayStack(); + + for (int i = 0 ; i< s.length();i++){ + + if (s.charAt(i) == '('){ + a.push(s.charAt(i)); + continue; + } + if (s.charAt(i) == ')'){ + if (a.isEmpty() || a.top() != '('){ + return false; + } else { + a.pop(); + } + continue; + } + if (s.charAt(i) == '['){ + if ( a.isEmpty() || a.top() != '(') { + a.push(s.charAt(i)); + } else { + return false; + } + continue; + + } + if (s.charAt(i) == ']'){ + if (a.isEmpty() || a.top() != '['){ + return false; + } else { + a.pop(); + } + continue; + } + + } + + if (a.isEmpty()){ + return true; + }else { + return false; + } + + } + + // verify is a string is palindrome using a stack + public static boolean isPalindrome (String s){ + + ArrayStack a = new ArrayStack(); + + if (s.length() < 2){ + return true; + }else { + + boolean pari; + + if (s.length() % 2 == 0) { + pari = true; + } else { + pari = false; + } + + + for (int i= 0 ;i < s.length();i++){ + if (!pari){ + if (i <= s.length()/2){ + a.push(s.charAt(i)); + if (i == s.length()/2 ){ + a.pop(); + } + continue; + } + } else { + if (i <= (s.length()/2) -1){ + a.push(s.charAt(i)); + continue; + } + } + + if (s.charAt(i) != a.pop()){ + return false; + } + + } + return true; + + } + + } + + // extract the k element from a Queue Q. k should be >= 0 + public static Integer extract (Queue Q, int k) throws NotEnoughElements { + + if (Q.size() <= k) { + throw new NotEnoughElements(); + } else { + Integer t = null ; + + for (int i = 0 ;i< Q.size();i++){ + if (i == k){ + t = Q.front(); + } + Q.enqueue(Q.dequeue()); + } + + return t; + + } + + + } + + // invert a Queue without recursive methods + public static void reverse (Queue Q){ + + NodeQueue newq = new NodeQueue(); + + for (;Q.size()> 0;){ + + for (int i = 0;i< Q.size() - 1;i++){ + Q.enqueue(Q.dequeue()); + } + newq.enqueue(Q.dequeue()); + } + + int size = newq.size(); + for (int i = 0;i void recReverse (Queue Q){ + if (Q.size()>1){ + E t = Q.dequeue(); + recReverse(Q); + Q.enqueue(t); + } + } + + // search element x in Queue Q . WARNING THIS FUNCTION ALTER THE ORIGINAL QUEUE Q + public static boolean isContained (QueueQ,E x){ + if (Q.size() > 0){ + E t = Q.dequeue(); + if (t==x){ + return true; + } + else{ + return isContained(Q,x); + } + }else { + return false; + } + } + + + public static void reverse (Sequence in){ + if (in.size()> 1){ + E t = in.removeFirst(); + static_methods.reverse(in); + in.addLast(t); + + } + + } + + public static void cancellaDuplicati (PositionList L){ + + if (L.size() >=2){ + Position first; + Position second; + first = L.first(); + second = L.next(first); + for (;true;) { + if (first.element().equals(second.element())) { + L.remove(second); + if (first!= L.last()) { + second = L.next(first); + }else { + break; + } + } else { + first = second; + if (first!= L.last()) { + second = L.next(second); + }else { + break; + } + + } + } + } + + } + + + + + public static Iterable selectLeaves(Tree T, int k ){ + if (T.isEmpty()) throw new EmptyTreeException(); + NodePositionList to_return = new NodePositionList(); + if (T.size() == 1){ + to_return.addLast(T.root().element()); + } else { + exec_selectLeaves (T,k,T.root(),0,to_return); + } + return to_return; + } + + public static void exec_selectLeaves(Tree T, int k, Position p, int c_k, PositionList list ){ + c_k++; + Iterable> ite = T.children(p); + for (Position t : ite){ + if (c_k == k) + list.addLast(t.element()); + else if (T.isInternal(t)) + exec_selectLeaves (T,k,t,c_k,list); + } + } + + public static boolean isContained (Tree T, E x ) { + if (T.isEmpty()) throw new EmptyTreeException(); + return exec_isContained (T,T.root(),x); + } + + + public static boolean exec_isContained (Tree T, Position p , E x ) { + if (p.element().equals(x)) return true; + if (T.isInternal(p)){ + Iterable> ite = T.children(p); + for (Position t : ite){ + if(exec_isContained (T,t,x)) return true; + } + return false; + } + return false; + } + + + public static class Integer_Wrapper{ + public Integer value = 0; + } + + public static int sumAllNodes(Tree T) { + Integer_Wrapper returned = new Integer_Wrapper(); + exec_sumAllNodes(T, T.root(), returned); + return returned.value; + } + + public static void exec_sumAllNodes(Tree T, Position p , Integer_Wrapper returned) { + returned.value += p.element(); + if (T.isInternal(p)) { + Iterable> ite = T.children(p); + for (Position t : ite) { + exec_sumAllNodes(T, t, returned); + } + + } + } + + public static PriorityQueue f( PriorityQueue P, Comparator cV ){ + + PriorityQueue to_return = new HeapPriorityQueue(); + int flag = 0; + Integer key = null; + V value = null; + for (;P.size() > 0;){ + Entry e = P.removeMin(); + if (flag == 0){ + flag = 1; + key = e.getKey(); + value = e.getValue(); + } else { + if(e.getKey().equals(key)){ + if (cV.compare(e.getValue(),value) < 0 ){ + value = e.getValue(); + } + } else { + to_return.insert(key,value); + key = e.getKey(); + value = e.getValue(); + } + } + + + } + to_return.insert(key,value); + + return to_return; + + + } + + public static PositionList > sort(SortedListPriorityQueue Q1, SortedListPriorityQueue Q2, Comparator c){ + + + PositionList> to_return = new NodePositionList>(); + Entry a = Q1.removeMin(); + Entry b = Q2.removeMin(); + + for (;;){ + + if (c.compare(a.getKey(),b.getKey()) < 0){ + to_return.addLast(a); + if (!Q1.isEmpty()) { + a = Q1.removeMin();continue; + }else break; + + } + if (c.compare(a.getKey(),b.getKey()) == 0){ + to_return.addLast(a); + to_return.addLast(b); + if (!Q1.isEmpty() && !Q2.isEmpty() ) { + a = Q1.removeMin(); + b = Q2.removeMin(); + continue; + }else break; + + } + if (c.compare(a.getKey(),b.getKey()) > 0){ + to_return.addLast(b); + if (!Q2.isEmpty()) { + b = Q2.removeMin();continue; + }else break; + + } + + + } + + + for (;!Q1.isEmpty();){ + a = Q1.removeMin(); + to_return.addLast(a); + } + for (;!Q2.isEmpty();){ + b = Q2.removeMin(); + to_return.addLast(b); + } + + return to_return; + + } + + public static Iterable>sortValues ( Dictionary D,Comparator c ){ + + PriorityQueue> h = new HeapPriorityQueue>(c); + for ( Entry e : D.entries() ){ + h.insert(e.getValue(),e); + } + PositionList> to_return = new NodePositionList>(); + for (;!h.isEmpty();){ + to_return.addLast(h.removeMin().getValue()); + } + return to_return; + } + + public static Iterable subtract( Dictionary D1, Dictionary D2) { + Map m = new ListMap(); + for ( Entry e :D2.entries()){ + for ( Entry e1 :D1.findAll(e.getKey())){ + m.put ( (D1.remove(e1)).getKey() , null ); + } + + } + return m.keys(); + } + +} diff --git a/com/xgiovio/global.java b/com/xgiovio/global.java deleted file mode 100644 index e987cdc..0000000 --- a/com/xgiovio/global.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.xgiovio; - -import position.Position; -import position.PositionList; -import sequence.Sequence; - -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * Created with MONSTER. - * User: xgiovio - * Date: 30/03/2014 - * Time: 21:35 - */ -class global { - public static void reverse (Sequence in){ - if (in.size()> 1){ - E t = in.removeFirst(); - global.reverse(in); - in.addLast(t); - - } - - } - - - - public static void cancellaDuplicati (PositionListL){ - - if (L.size() >=2){ - Position first; - Position second; - first = L.first(); - second = L.next(first); - for (;true;) { - if (first.element().equals(second.element())) { - L.remove(second); - if (first!= L.last()) { - second = L.next(first); - }else { - break; - } - } else { - first = second; - if (first!= L.last()) { - second = L.next(second); - }else { - break; - } - - } - } - } - - } - -} diff --git a/test_2_prova_intercorso/ExBinaryTree_11_2_13.java b/com/xgiovio/test_2_prova_intercorso/ExBinaryTree_11_2_13.java similarity index 99% rename from test_2_prova_intercorso/ExBinaryTree_11_2_13.java rename to com/xgiovio/test_2_prova_intercorso/ExBinaryTree_11_2_13.java index ae2f8d9..ef3fb4b 100644 --- a/test_2_prova_intercorso/ExBinaryTree_11_2_13.java +++ b/com/xgiovio/test_2_prova_intercorso/ExBinaryTree_11_2_13.java @@ -1,4 +1,4 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; /* diff --git a/test_2_prova_intercorso/ExGraph_15_6PerEserc.java b/com/xgiovio/test_2_prova_intercorso/ExGraph_15_6PerEserc.java similarity index 99% rename from test_2_prova_intercorso/ExGraph_15_6PerEserc.java rename to com/xgiovio/test_2_prova_intercorso/ExGraph_15_6PerEserc.java index 42eec7f..70888ca 100644 --- a/test_2_prova_intercorso/ExGraph_15_6PerEserc.java +++ b/com/xgiovio/test_2_prova_intercorso/ExGraph_15_6PerEserc.java @@ -1,4 +1,4 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; /*Il programma deve stampare : diff --git a/test_2_prova_intercorso/ExMap15_1.java b/com/xgiovio/test_2_prova_intercorso/ExMap15_1.java similarity index 98% rename from test_2_prova_intercorso/ExMap15_1.java rename to com/xgiovio/test_2_prova_intercorso/ExMap15_1.java index 807eba3..8f20300 100644 --- a/test_2_prova_intercorso/ExMap15_1.java +++ b/com/xgiovio/test_2_prova_intercorso/ExMap15_1.java @@ -1,4 +1,4 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; /*Il programma deve stampare (ovviamente le entrate di M e D possono essere stampate in un diverso ordine): I test: @@ -18,7 +18,6 @@ Dopo aver invocato addEntries su M e D, si ha M=< (90,49) (70,39) (50,29) (30,19 import dictionary.Dictionary; import dictionary.LinearProbingHashTable; -import exceptions.InvalidKeyException; import map.HashTableMap; import map.Map; import priorityqueue.Entry; diff --git a/test_2_prova_intercorso/ExPQ_21_2_11.java b/com/xgiovio/test_2_prova_intercorso/ExPQ_21_2_11.java old mode 100755 new mode 100644 similarity index 99% rename from test_2_prova_intercorso/ExPQ_21_2_11.java rename to com/xgiovio/test_2_prova_intercorso/ExPQ_21_2_11.java index 3028db4..79a8d12 --- a/test_2_prova_intercorso/ExPQ_21_2_11.java +++ b/com/xgiovio/test_2_prova_intercorso/ExPQ_21_2_11.java @@ -1,4 +1,4 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; import exceptions.*; /*Il programma deve stampare (l'ordine in cui vengono stampate le entrate su ciascuna linea e` ininfluente): diff --git a/test_2_prova_intercorso/ExTree_17_1_2013.java b/com/xgiovio/test_2_prova_intercorso/ExTree_17_1_2013.java similarity index 99% rename from test_2_prova_intercorso/ExTree_17_1_2013.java rename to com/xgiovio/test_2_prova_intercorso/ExTree_17_1_2013.java index 3723094..083b748 100644 --- a/test_2_prova_intercorso/ExTree_17_1_2013.java +++ b/com/xgiovio/test_2_prova_intercorso/ExTree_17_1_2013.java @@ -1,4 +1,4 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; import exceptions.EmptyTreeException; diff --git a/test_2_prova_intercorso/Other_Functions.java b/com/xgiovio/test_2_prova_intercorso/Other_Functions.java similarity index 95% rename from test_2_prova_intercorso/Other_Functions.java rename to com/xgiovio/test_2_prova_intercorso/Other_Functions.java index f7fe06e..cf97cae 100644 --- a/test_2_prova_intercorso/Other_Functions.java +++ b/com/xgiovio/test_2_prova_intercorso/Other_Functions.java @@ -1,9 +1,7 @@ -package test_2_prova_intercorso; +package com.xgiovio.test_2_prova_intercorso; import arraylist.ArrayIndexList; import arraylist.IndexList; -import exceptions.InvalidKeyException; -import graph.ComponentsBFS; import graph.Edge; import graph.Graph; import graph.Vertex; diff --git a/test_prova_intercorso/EmptyMyADT_Exception.java b/com/xgiovio/test_prova_intercorso/EmptyMyADT_Exception.java similarity index 74% rename from test_prova_intercorso/EmptyMyADT_Exception.java rename to com/xgiovio/test_prova_intercorso/EmptyMyADT_Exception.java index bdaaf7d..3858d15 100644 --- a/test_prova_intercorso/EmptyMyADT_Exception.java +++ b/com/xgiovio/test_prova_intercorso/EmptyMyADT_Exception.java @@ -1,4 +1,4 @@ -package test_prova_intercorso; +package com.xgiovio.test_prova_intercorso; public class EmptyMyADT_Exception extends RuntimeException { diff --git a/test_prova_intercorso/ExStack_17_4_13.java b/com/xgiovio/test_prova_intercorso/ExStack_17_4_13.java similarity index 98% rename from test_prova_intercorso/ExStack_17_4_13.java rename to com/xgiovio/test_prova_intercorso/ExStack_17_4_13.java index 60335fa..4ec0e66 100644 --- a/test_prova_intercorso/ExStack_17_4_13.java +++ b/com/xgiovio/test_prova_intercorso/ExStack_17_4_13.java @@ -1,4 +1,4 @@ -package test_prova_intercorso; +package com.xgiovio.test_prova_intercorso; import stack.ArrayStack; diff --git a/test_prova_intercorso/MyADT.java b/com/xgiovio/test_prova_intercorso/MyADT.java similarity index 92% rename from test_prova_intercorso/MyADT.java rename to com/xgiovio/test_prova_intercorso/MyADT.java index fed04a2..53f9607 100644 --- a/test_prova_intercorso/MyADT.java +++ b/com/xgiovio/test_prova_intercorso/MyADT.java @@ -1,4 +1,4 @@ -package test_prova_intercorso; +package com.xgiovio.test_prova_intercorso; public interface MyADT{ diff --git a/test_prova_intercorso/QueueMyADT.java b/com/xgiovio/test_prova_intercorso/QueueMyADT.java similarity index 96% rename from test_prova_intercorso/QueueMyADT.java rename to com/xgiovio/test_prova_intercorso/QueueMyADT.java index 3dc2af5..7830199 100644 --- a/test_prova_intercorso/QueueMyADT.java +++ b/com/xgiovio/test_prova_intercorso/QueueMyADT.java @@ -1,4 +1,4 @@ -package test_prova_intercorso; +package com.xgiovio.test_prova_intercorso; import queue.NodeQueue; diff --git a/test_prova_intercorso/test.java b/com/xgiovio/test_prova_intercorso/test.java similarity index 90% rename from test_prova_intercorso/test.java rename to com/xgiovio/test_prova_intercorso/test.java index f3bc382..86db60d 100644 --- a/test_prova_intercorso/test.java +++ b/com/xgiovio/test_prova_intercorso/test.java @@ -1,4 +1,4 @@ -package test_prova_intercorso; +package com.xgiovio.test_prova_intercorso; public class test { diff --git a/esercizi/static_methods.java b/esercizi/static_methods.java deleted file mode 100644 index 6f442f5..0000000 --- a/esercizi/static_methods.java +++ /dev/null @@ -1,193 +0,0 @@ -package esercizi; - -import queue.NodeQueue; -import queue.Queue; -import stack.ArrayStack; -import exceptions.*; - -/** - * Created with MONSTER. - * User: xgiovio - * Date: 13/04/2014 - * Time: 22:19 - */ -public class static_methods { - - - - // inver a string using a stack - public static String inverti (String s){ - ArrayStack a = new ArrayStack(); - for (int i = 0 ;i< s.length();i++){ - a.push(s.charAt(i)); - } - - String to_return = ""; - - for (int i = 0 ;i< s.length();i++){ - to_return += a.pop(); - } - - return to_return; - - } - - // verify is a string of [ and ( is correct using a stack - public static boolean checkParentheses (String s) { - - ArrayStack a = new ArrayStack(); - - for (int i = 0 ; i< s.length();i++){ - - if (s.charAt(i) == '('){ - a.push(s.charAt(i)); - continue; - } - if (s.charAt(i) == ')'){ - if (a.isEmpty() || a.top() != '('){ - return false; - } else { - a.pop(); - } - continue; - } - if (s.charAt(i) == '['){ - if ( a.isEmpty() || a.top() != '(') { - a.push(s.charAt(i)); - } else { - return false; - } - continue; - - } - if (s.charAt(i) == ']'){ - if (a.isEmpty() || a.top() != '['){ - return false; - } else { - a.pop(); - } - continue; - } - - } - - if (a.isEmpty()){ - return true; - }else { - return false; - } - - } - - // verify is a string is palindrome using a stack - public static boolean isPalindrome (String s){ - - ArrayStack a = new ArrayStack(); - - if (s.length() < 2){ - return true; - }else { - - boolean pari; - - if (s.length() % 2 == 0) { - pari = true; - } else { - pari = false; - } - - - for (int i= 0 ;i < s.length();i++){ - if (!pari){ - if (i <= s.length()/2){ - a.push(s.charAt(i)); - if (i == s.length()/2 ){ - a.pop(); - } - continue; - } - } else { - if (i <= (s.length()/2) -1){ - a.push(s.charAt(i)); - continue; - } - } - - if (s.charAt(i) != a.pop()){ - return false; - } - - } - return true; - - } - - } - - // extract the k element from a Queue Q. k should be >= 0 - public static Integer extract (Queue Q, int k) throws NotEnoughElements { - - if (Q.size() <= k) { - throw new NotEnoughElements(); - } else { - Integer t = null ; - - for (int i = 0 ;i< Q.size();i++){ - if (i == k){ - t = Q.front(); - } - Q.enqueue(Q.dequeue()); - } - - return t; - - } - - - } - - // invert a Queue without recursive methods - public static void reverse (Queue Q){ - - NodeQueue newq = new NodeQueue(); - - for (;Q.size()> 0;){ - - for (int i = 0;i< Q.size() - 1;i++){ - Q.enqueue(Q.dequeue()); - } - newq.enqueue(Q.dequeue()); - } - - int size = newq.size(); - for (int i = 0;i void recReverse (Queue Q){ - if (Q.size()>1){ - E t = Q.dequeue(); - recReverse(Q); - Q.enqueue(t); - } - } - - // search element x in Queue Q . WARNING THIS FUNCTION ALTER THE ORIGINAL QUEUE Q - public static boolean isContained (QueueQ,E x){ - if (Q.size() > 0){ - E t = Q.dequeue(); - if (t==x){ - return true; - } - else{ - return isContained(Q,x); - } - }else { - return false; - } - } - - -} diff --git a/graph/DFS.java b/graph/DFS.java index 0628da6..491b0c7 100644 --- a/graph/DFS.java +++ b/graph/DFS.java @@ -12,7 +12,7 @@ import exceptions.InvalidKeyException; */ public class DFS { protected Graph graph; // The graph being traversed - protected Vertex start; // The start vertex for the DFS + protected Vertex start; // The start_static_methods vertex for the DFS protected I info; // Information object passed to DFS protected R visitResult; // The result of a recursive traversal call protected static Object STATUS = new Object(); // The status attribute @@ -22,7 +22,7 @@ public class DFS { /** Execute a depth first search traversal on graph g, starting - * from a start vertex s, passing in an information object (in) */ + * from a start_static_methods vertex s, passing in an information object (in) */ public R execute(Graph g, Vertex s, I in) throws InvalidKeyException { graph = g; start = s; diff --git a/graph/Dijkstra.java b/graph/Dijkstra.java index 15688a1..5780a2c 100644 --- a/graph/Dijkstra.java +++ b/graph/Dijkstra.java @@ -14,7 +14,7 @@ import java.security.InvalidKeyException; *

To execute the algorithm, use the {@link * #execute(Graph,Vertex,Object) execute} method, and then make * subsequent calls to the {@link #getDist(Vertex) getDist} method to - * obtain the shortest distance from the start to any given vertex. + * obtain the shortest distance from the start_static_methods to any given vertex. * * @author Roberto Tamassia, Michael Goodrich, Eric Zamore */ diff --git a/graph/FindCycleDFS.java b/graph/FindCycleDFS.java index 9e9a0c9..f14e94b 100644 --- a/graph/FindCycleDFS.java +++ b/graph/FindCycleDFS.java @@ -46,7 +46,7 @@ public class FindCycleDFS protected boolean isDone() { return done; } public Iterable finalResult(Iterable r) { - // remove the vertices and edges from start to cycleStart + // remove the vertices and edges from start_static_methods to cycleStart if (!cycle.isEmpty()) { for (Position p: cycle.positions()) { if (p.element() == cycleStart) diff --git a/graph/FindPathDFS.java b/graph/FindPathDFS.java index 04779fe..0c26a19 100644 --- a/graph/FindPathDFS.java +++ b/graph/FindPathDFS.java @@ -5,11 +5,11 @@ import position.NodePositionList; import position.Position; import position.PositionList; -/** Class specializing DFS to find a path between a start vertex and a target +/** Class specializing DFS to find a path between a start_static_methods vertex and a target * vertex. It assumes the target vertex is passed as the info object to the * execute method. It returns an iterable list of the vertices and edges - * comprising the path from start to info. The returned path is empty if - * info is unreachable from start. */ + * comprising the path from start_static_methods to info. The returned path is empty if + * info is unreachable from start_static_methods. */ public class FindPathDFS extends DFS, Iterable> { protected PositionList path; @@ -27,7 +27,7 @@ public class FindPathDFS } protected void finishVisit(Vertex v) { path.remove(path.last()); // remove v from path - if(!path.isEmpty()) // if v is not the start vertex + if(!path.isEmpty()) // if v is not the start_static_methods vertex path.remove(path.last()); // remove discovery edge into v from path } protected void traverseDiscovery(Edge e, Vertex from) { diff --git a/tree/LinkedTree.java b/tree/LinkedTree.java index 0608ca0..70e7673 100644 --- a/tree/LinkedTree.java +++ b/tree/LinkedTree.java @@ -195,33 +195,6 @@ public class LinkedTree implements Tree { return null; } - - - - public int depth ( Position v) throws InvalidPositionException{ - TreePosition a = checkPosition(v); - if (a.getParent() == null) - return 0; - return (depth(a.getParent()) + 1); - } - - public int height () throws EmptyTreeException{ - return height_f(root()); - } - - public int height_f ( Position v) { - TreePosition a = checkPosition(v); - if (isExternal(a)) - return 0; - int max = 0; - for ( Position w : a.getChildren()){ - if (max == 0 || height_f(w) > max){ - max = height_f(w); - } - } - return 1 + max; - } - @Override public String toString() { String to_return = "";