diff --git a/.gitignore b/.gitignore index 8963733..ccf9401 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ bin target doc +javadoc *.jar .settings .classpath @@ -12,9 +13,10 @@ doc # Editor specific files and folders *~ .project +maps # Project specific files and folders *.mapfg *.mapgr *.path -*.tgz \ No newline at end of file +*.tgz diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java index 2c1a239..308c8ef 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java @@ -134,10 +134,89 @@ public class BinaryHeap> implements PriorityQueue { this.arraySet(index, x); this.percolateUp(index); } + + // recursive version + private int find(int indexStart, E x) throws ElementNotFoundException { + + //it's a heap so uses it to find a log(n) speed the element + + int ileft = indexLeft(indexStart); + int iright = ileft + 1; + boolean hasRight = iright < this.currentSize; + boolean hasLeft = ileft < this.currentSize; + + if(hasRight) { + try { + if(x.compareTo(this.array.get(iright)) == 0) { + return iright; + } + else if(x.compareTo(this.array.get(iright)) > 0) { + return find(iright, x); + } + }catch (ElementNotFoundException e) { + //do nothing... + } + } + + if(hasLeft) { + try { + if(x.compareTo(this.array.get(ileft)) == 0) { + return ileft; + } + else if(x.compareTo(this.array.get(ileft)) > 0) { + return find(ileft, x); + } + }catch (ElementNotFoundException e) { + //do nothing... + } + } + + //here we dont find anything from this index, throw exception + throw new ElementNotFoundException(x); + } + + @Override + public int find(E x) throws ElementNotFoundException { + + if(this.currentSize == 0) { + throw new ElementNotFoundException(x); + } + + + /*int index = this.array.indexOf(x); + + if(index < 0 || index >= this.currentSize) { + throw new ElementNotFoundException(x); + } + else { + return index; + }*/ + + //try to see if it's root + if(this.array.get(0).compareTo(x) == 0) { + return 0; + } + + //so now we need to find deeper. + return find(0, x); + } @Override public void remove(E x) throws ElementNotFoundException { - // TODO: + // first step, find the element on the heap + + int myIndex = find(x); + + + //now we have the element of the object to remove + //so it's like deleting root, but instead of root it's index of x + + E lastItem = this.array.get(--this.currentSize); + this.arraySet(myIndex, lastItem); + this.percolateUp(myIndex); + this.percolateDown(myIndex); + + } @Override diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java index 32dfbb1..f23b2f9 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java @@ -61,4 +61,10 @@ public class BinarySearchTree> implements PriorityQueue< return min; } + @Override + public int find(E x) { + // TODO Auto-generated method stub + return -1; + } + } diff --git a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java index 104f79e..4de2a11 100644 --- a/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java +++ b/be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java @@ -77,5 +77,14 @@ public interface PriorityQueue> { * @throws EmptyPriorityQueueException if this queue is empty. */ public E deleteMin() throws EmptyPriorityQueueException; + + + /** + * Find the index of the element E + * @return The index of the element E + * + * @throws ElementNotFoundException + */ + public int find(E x); } diff --git a/pom.xml b/pom.xml index c275f19..4a70fb7 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ be-graphes-all - 1.8 + 1.14 ${jdk.version} ${jdk.version} UTF-8