package edu.xmu.guava.jcip;
import java.util.Map;
public interface Traversable {
public void addWord(String word, int index);
public Traversable getNext(Character c);
public Map<Character, ? extends Traversable> getChildren();
}
package edu.xmu.guava.jcip;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeMap;
public class Dictionary {
public static void main(String[] args) {
Traversable rootNode = new VirtualHeaderNode();
rootNode.addWord("Hello", 0);
rootNode.addWord("Welcome", 0);
rootNode.addWord("Yang", 0);
rootNode.addWord("Yes", 0);
rootNode.addWord("Good", 0);
rootNode.addWord("God", 0);
rootNode.addWord("Yeah", 0);
rootNode.addWord("Yep", 0);
System.out.println(rootNode.getChildren().keySet());
Stack<Traversable> wordStack = new Stack<Traversable>();
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
Traversable currentNode = rootNode;
wordStack.push(currentNode);
while (true) {
if ("POP".equals(str)) {
if (!(wordStack.peek() instanceof VirtualHeaderNode)) {
wordStack.pop();
currentNode = wordStack.peek();
}
} else if ("EXIT".equals(str)) {
scanner.close();
break;
} else {
char alpha = str.charAt(0);
if (currentNode.getChildren().containsKey(alpha)) {
currentNode = currentNode.getChildren().get(alpha);
wordStack.push(currentNode);
}
}
Map<Character, ? extends Traversable> next = currentNode
.getChildren();
System.out.println(next.keySet());
str = scanner.next();
};
}
public static class VirtualHeaderNode implements Traversable {
private Map<Character, Traversable> children = new TreeMap<Character, Traversable>();
@Override
public void addWord(String word, int index) {
char c = word.charAt(0);
Traversable headerNode;
if (children.containsKey(c)) {
headerNode = children.get(c);
} else {
headerNode = new AlphabetNode();
children.put(c, headerNode);
}
headerNode.addWord(word, index);
}
@Override
public Traversable getNext(Character c) {
return children.get(c);
}
@Override
public Map<Character, Traversable> getChildren() {
return children;
}
}
public static class AlphabetNode implements Traversable {
private AlphabetNode parent;
private Map<Character, AlphabetNode> children = new TreeMap<Character, AlphabetNode>();
private char value;
public void addWord(String word, int i) {
this.value = word.charAt(i);
if (i >= (word.length() - 1)) {
return;
}
char childValue = word.charAt(i + 1);
AlphabetNode childNode;
if (children.containsKey(childValue)) {
childNode = children.get(childValue);
} else {
childNode = new AlphabetNode();
childNode.setParent(this);
childNode.setValue(childValue);
children.put(childValue, childNode);
}
childNode.addWord(word, i + 1);
}
public AlphabetNode getParent() {
return parent;
}
public void setParent(AlphabetNode parent) {
this.parent = parent;
}
public Map<Character, AlphabetNode> getChildren() {
return children;
}
public void setChildren(Map<Character, AlphabetNode> children) {
this.children = children;
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
@Override
public String toString() {
return "AlphabetNode [value=" + value + "]";
}
@Override
public AlphabetNode getNext(Character c) {
return children.get(c);
}
}
}