Skip to content
Snippets Groups Projects
Commit cfa66f82 authored by @thx's avatar @thx
Browse files

Refactor to Java Map-based HashTrieNode

parent 3e8e0589
No related branches found
No related tags found
No related merge requests found
package datastructures.dictionaries;
import cse332.datastructures.containers.Item;
import cse332.exceptions.NotYetImplementedException;
import cse332.interfaces.misc.Dictionary;
import cse332.interfaces.misc.SimpleIterator;
import cse332.interfaces.trie.TrieMap;
import cse332.types.BString;
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;
/**
* See cse332/interfaces/trie/TrieMap.java
......@@ -18,27 +15,23 @@ import java.util.function.Supplier;
* for method specifications.
*/
public class HashTrieMap<A extends Comparable<A>, K extends BString<A>, V> extends TrieMap<A, K, V> {
public class HashTrieNode extends TrieNode<Dictionary<A, HashTrieNode>, HashTrieNode> {
public class HashTrieNode extends TrieNode<Map<A, HashTrieNode>, HashTrieNode> {
public HashTrieNode() {
this(null);
}
public HashTrieNode(V value) {
this.pointers = new ChainingHashTable<A, HashTrieNode>(new Supplier<Dictionary<A, HashTrieNode>>() {
@Override
public Dictionary<A, HashTrieNode> get() {
return new MoveToFrontList<>();
}
});
this.pointers = new HashMap<>();
this.value = value;
}
@Override
public Iterator<Entry<A, HashTrieNode>> iterator() {
throw new NotYetImplementedException();
public Iterator<Entry<A, HashTrieMap<A, K, V>.HashTrieNode>> iterator() {
return pointers.entrySet().iterator();
}
}
public HashTrieMap(Class<K> KClass) {
super(KClass);
throw new NotYetImplementedException();
......
package p2.wordcorrector;
import cse332.types.AlphabeticString;
import datastructures.dictionaries.HashTrieMap;
import java.util.Map.Entry;
public class AutocompleteTrie extends HashTrieMap<Character, AlphabeticString, Integer> {
public AutocompleteTrie() {
......@@ -16,10 +13,12 @@ public class AutocompleteTrie extends HashTrieMap<Character, AlphabeticString, I
@SuppressWarnings("unchecked")
HashTrieNode current = (HashTrieNode) this.root;
for (Character item : key.toCharArray()) {
current = current.pointers.find(item);
if (current == null) {
if (!current.pointers.containsKey(item)) {
return null;
}
else {
current = current.pointers.get(item);
}
}
String result = key;
......@@ -28,9 +27,8 @@ public class AutocompleteTrie extends HashTrieMap<Character, AlphabeticString, I
if (current.value != null) {
return null;
}
Entry<Character, HashTrieNode> next = current.iterator().next();
result += next.getKey();
current = next.getValue();
result += current.pointers.keySet().iterator().next();
current = current.pointers.values().iterator().next();
}
// Switch this to return null to only complete if we're at the end of
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment