implemented remove()
all junit tests passing !
This commit is contained in:
parent
8eeb871b1a
commit
047a67f73a
5 changed files with 99 additions and 3 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,6 +5,7 @@
|
||||||
bin
|
bin
|
||||||
target
|
target
|
||||||
doc
|
doc
|
||||||
|
javadoc
|
||||||
*.jar
|
*.jar
|
||||||
.settings
|
.settings
|
||||||
.classpath
|
.classpath
|
||||||
|
@ -12,6 +13,7 @@ doc
|
||||||
# Editor specific files and folders
|
# Editor specific files and folders
|
||||||
*~
|
*~
|
||||||
.project
|
.project
|
||||||
|
maps
|
||||||
|
|
||||||
# Project specific files and folders
|
# Project specific files and folders
|
||||||
*.mapfg
|
*.mapfg
|
||||||
|
|
|
@ -135,9 +135,88 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
|
||||||
this.percolateUp(index);
|
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
|
@Override
|
||||||
public void remove(E x) throws ElementNotFoundException {
|
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
|
@Override
|
||||||
|
|
|
@ -61,4 +61,10 @@ public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<
|
||||||
return min;
|
return min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int find(E x) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,4 +78,13 @@ public interface PriorityQueue<E extends Comparable<E>> {
|
||||||
*/
|
*/
|
||||||
public E deleteMin() throws EmptyPriorityQueueException;
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
pom.xml
2
pom.xml
|
@ -12,7 +12,7 @@
|
||||||
<name>be-graphes-all</name>
|
<name>be-graphes-all</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<jdk.version>1.8</jdk.version>
|
<jdk.version>1.14</jdk.version>
|
||||||
<maven.compiler.source>${jdk.version}</maven.compiler.source>
|
<maven.compiler.source>${jdk.version}</maven.compiler.source>
|
||||||
<maven.compiler.target>${jdk.version}</maven.compiler.target>
|
<maven.compiler.target>${jdk.version}</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
Loading…
Reference in a new issue