import java.util.*; public class DLB { private Node root = new Node(); private static class Node { private char ch; private Object val; private LinkedList list = new LinkedList(); } public void put(String key, Value val) { put(root, key, val, 0); } private void put(Node x, String key, Value val, int d) { if (d == key.length()) { x.val = val; return; } // Search siblings, similar to R-way trie key indexing '[]' operation. Node next = null; for (int i = 0; i < x.list.size(); ++i) { if (x.list.get(i).ch == key.charAt(d)) { next = x.list.get(i); break; } } if (null == next) { next = new Node(); next.ch = key.charAt(d); x.list.add(next); } // Search children using recursive call. put(next, key, val, d + 1); } public Value get(String key) { Node x = get(root, key, 0); if (null == x) return null; // !!!! Node.val could be a valid value, or a 'null' value denoting non-existence of the word. // 'null' happens when this invalid key is a prefix of an valid key. return (Value) x.val; } private Node get(Node x, String key, int d) { if (d == key.length()) { return x; } for (int i = 0; i < x.list.size(); ++i) { if (x.list.get(i).ch == key.charAt(d)) { return get(x.list.get(i), key, d + 1); } } return null; } }