Browse Source

implemented remove()

all junit tests passing !
Adiren Barbanson 7 months ago
parent
commit
047a67f73a

+ 3
- 1
.gitignore View File

@@ -5,6 +5,7 @@
5 5
 bin
6 6
 target
7 7
 doc
8
+javadoc
8 9
 *.jar
9 10
 .settings
10 11
 .classpath
@@ -12,9 +13,10 @@ doc
12 13
 # Editor specific files and folders
13 14
 *~
14 15
 .project
16
+maps
15 17
 
16 18
 # Project specific files and folders
17 19
 *.mapfg
18 20
 *.mapgr
19 21
 *.path
20
-*.tgz
22
+*.tgz

+ 80
- 1
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinaryHeap.java View File

@@ -134,10 +134,89 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
134 134
         this.arraySet(index, x);
135 135
         this.percolateUp(index);
136 136
     }
137
+    
138
+    // recursive version
139
+    private int find(int indexStart, E x) throws ElementNotFoundException {
140
+    	
141
+    	//it's a heap so uses it to find a log(n) speed the element	
142
+    	
143
+        int ileft = indexLeft(indexStart);
144
+        int iright = ileft + 1;
145
+        boolean hasRight = iright < this.currentSize;
146
+        boolean hasLeft = ileft < this.currentSize;
147
+        
148
+        if(hasRight) {
149
+        	try {
150
+        		if(x.compareTo(this.array.get(iright)) == 0) {
151
+        			return iright;
152
+        		}
153
+        		else if(x.compareTo(this.array.get(iright)) > 0) {
154
+            		return find(iright, x);
155
+            	}
156
+        	}catch (ElementNotFoundException e) {
157
+        		//do nothing...
158
+        	}
159
+        }
160
+        
161
+        if(hasLeft) {
162
+        	try {
163
+        		if(x.compareTo(this.array.get(ileft)) == 0) {
164
+        			return ileft;
165
+        		}
166
+        		else if(x.compareTo(this.array.get(ileft)) > 0) {
167
+            		return find(ileft, x);
168
+            	}
169
+        	}catch (ElementNotFoundException e) {
170
+        		//do nothing...
171
+        	}
172
+        }
173
+        
174
+        //here we dont find anything from this index, throw exception
175
+        throw new ElementNotFoundException(x);
176
+    }
177
+    
178
+    @Override
179
+    public int find(E x) throws ElementNotFoundException {
180
+    	
181
+    	if(this.currentSize == 0) {
182
+    		throw new ElementNotFoundException(x);
183
+    	}
184
+    	
185
+    	
186
+    	/*int index =  this.array.indexOf(x);
187
+    	
188
+    	if(index < 0 || index >= this.currentSize) {
189
+    		throw new ElementNotFoundException(x);
190
+    	}
191
+    	else {
192
+    		return index;
193
+    	}*/
194
+    	
195
+    	//try to see if it's root
196
+    	if(this.array.get(0).compareTo(x) == 0) {
197
+    		return 0;
198
+    	}
199
+    	
200
+    	//so now we need to find deeper.
201
+		return find(0, x);
202
+    }
137 203
 
138 204
     @Override
139 205
     public void remove(E x) throws ElementNotFoundException {
140
-        // TODO:
206
+        // first step, find the element on the heap
207
+    	
208
+    	int myIndex = find(x);
209
+    	
210
+    	
211
+    	//now we have the element of the object to remove
212
+    	//so it's like deleting root, but instead of root it's index of x
213
+    	
214
+    	E lastItem = this.array.get(--this.currentSize);
215
+        this.arraySet(myIndex, lastItem);
216
+        this.percolateUp(myIndex);
217
+        this.percolateDown(myIndex);
218
+    	
219
+    	
141 220
     }
142 221
 
143 222
     @Override

+ 6
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/BinarySearchTree.java View File

@@ -61,4 +61,10 @@ public class BinarySearchTree<E extends Comparable<E>> implements PriorityQueue<
61 61
         return min;
62 62
     }
63 63
 
64
+	@Override
65
+	public int find(E x) {
66
+		// TODO Auto-generated method stub
67
+		return -1;
68
+	}
69
+
64 70
 }

+ 9
- 0
be-graphes-algos/src/main/java/org/insa/graphs/algorithm/utils/PriorityQueue.java View File

@@ -77,5 +77,14 @@ public interface PriorityQueue<E extends Comparable<E>> {
77 77
      * @throws EmptyPriorityQueueException if this queue is empty.
78 78
      */
79 79
     public E deleteMin() throws EmptyPriorityQueueException;
80
+    
81
+    
82
+    /**
83
+     * Find the index of the element E
84
+     * @return The index of the element E
85
+     * 
86
+     * @throws ElementNotFoundException
87
+     */
88
+    public int find(E x);
80 89
 
81 90
 }

+ 1
- 1
pom.xml View File

@@ -12,7 +12,7 @@
12 12
 	<name>be-graphes-all</name>
13 13
 
14 14
 	<properties>
15
-		<jdk.version>1.8</jdk.version>
15
+		<jdk.version>1.14</jdk.version>
16 16
 		<maven.compiler.source>${jdk.version}</maven.compiler.source>
17 17
 		<maven.compiler.target>${jdk.version}</maven.compiler.target>
18 18
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Loading…
Cancel
Save