Skip to content

Commit 645caa9

Browse files
author
phishman3579
committed
Updates to the testing code. Also added a Skip List Map implementation.
git-svn-id: https://java-algorithms-implementation.googlecode.com/svn/trunk@427 032fbc0f-8cab-eb90-e552-f08422b9a96a
1 parent 5147a24 commit 645caa9

File tree

11 files changed

+763
-264
lines changed

11 files changed

+763
-264
lines changed

src/com/jwetherell/algorithms/DataStructures.java

+309-191
Large diffs are not rendered by default.

src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ protected Node<T> getLeast(Node<T> startingNode) {
282282
@Override
283283
public T remove(T value) {
284284
Node<T> nodeToRemove = this.removeValue(value);
285-
return ((nodeToRemove!=null)?nodeToRemove.id:null);
285+
return ((nodeToRemove!=null)?value:null);
286286
}
287287

288288
/**

src/com/jwetherell/algorithms/data_structures/HashMap.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ public HashMap() {
4242
*/
4343
@Override
4444
public V put(K key, V value) {
45-
V prev = null;
45+
V prev = value;
4646
int hashedKey = hashingFunction(key);
4747
List<Pair<K, V>> list = array[hashedKey];
48+
boolean exist = false;
4849
// Do not add duplicates
4950
for (Pair<K, V> p : list) {
5051
if (p.key.equals(key)) {
5152
prev = p.value;
5253
p.value = value;
54+
exist = true;
55+
break;
5356
}
5457
}
55-
list.add(new Pair<K, V>(key, value));
58+
if (!exist)
59+
list.add(new Pair<K, V>(key, value));
5660
size++;
5761
return prev;
5862
}

src/com/jwetherell/algorithms/data_structures/Queue.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,27 @@ public boolean contains(T value) {
151151
*/
152152
@Override
153153
public boolean validate() {
154+
if (size()==0) return true;
155+
154156
int localSize = 0;
157+
int realFirst = firstIndex;
158+
if (firstIndex>array.length)
159+
realFirst = firstIndex%array.length;
160+
int realLast = lastIndex;
161+
if (lastIndex>array.length)
162+
realLast = lastIndex%array.length;
155163
for (int i=0; i<array.length; i++) {
156164
T t = array[i];
157-
if (i<size()) {
158-
if (t==null) return false;
165+
if ((realFirst==realLast) ||
166+
(realFirst<realLast && (i>=realFirst && i<realLast)) ||
167+
(realLast<realFirst && (i<realLast || i>=realFirst))
168+
) {
169+
if (t==null)
170+
return false;
159171
localSize++;
160172
} else {
161-
if (t!=null) return false;
173+
if (t!=null)
174+
return false;
162175
}
163176
}
164177
return (localSize==size());

src/com/jwetherell/algorithms/data_structures/RadixTrie.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public RadixTrie() {
3131
*/
3232
@Override
3333
public V put(K key, V value) {
34-
V prev = null;
34+
V prev = value;
3535
PatriciaTrie.Node node = trie.addSequence(key);
3636

3737
if (node!=null && node instanceof RadixNode) {
3838
RadixNode<K,V> radixNode = (RadixNode<K,V>) node;
39-
prev = radixNode.value;
39+
if (radixNode.value!=null) prev = radixNode.value;
4040
radixNode.value = value;
4141
}
4242

0 commit comments

Comments
 (0)