5import java.util.Scanner;
12 protected BinarySearchTree<String,String>
tree_ =
null;
14 ((String)
null,
"aud.example.BinarySearchTreeExample");
15 protected BinarySearchTree<String,String>.Decorator
decorator_;
18 @SuppressWarnings(
"unchecked")
20 super(
"aud.example.BinarySearchTreeExample");
35 public static void main(String[] args) {
38 "usage: java aud.example.BinarySearchTreeExample\n"+
39 " Reads and insert words from standard input.\n"+
40 "\tWords may be prefixed with a single character, e.g., '-x':\n"+
41 "\t- '-' remove from the tree.\n"+
42 "\t- '/' apply right rotation to left-left case with key as root\n"+
43 "\t- '\\' apply left rotation to right-right case with key as root\n"+
44 "\t- '<' apply double rotation to right-left case with key as root\n"+
45 "\t- '>' apply double rotation to left-right case with key as root\n"+
46 "\t- '=' apply tri-node restructuring with key as grandchild\n"+
47 "\t- '!' highlights a node (or none if not found)\n"+
48 "\t- '~' pauses execution\n"+
49 "\t- '?' prints this message\n"+
50 "\t- 'quit' quits.\n"+
51 "\tDuring the whole process, the tree will be visualized.";
54 System.err.println(HELP);
58 BinarySearchTree<String,String> tree=
59 new BinarySearchTree<String,String>();
62 app.
halt(
"EMPTY TREE",1);
64 Scanner s=
new Scanner(System.in);
65 s.useDelimiter(
"\\s+");
69 if (key.compareTo(
"quit")==0)
71 else if (key.startsWith(
"?")) {
72 System.err.println(HELP);
74 else if (key.startsWith(
"!")) {
77 app.
halt(
"highlight '"+key+
"'",10);
79 else if (key.startsWith(
"-")) {
82 app.
halt(
"remove '"+key+
"'",200);
84 else if (key.startsWith(
"~")) {
85 app.
halt(
"PAUSE '"+key.substring(1)+
"'",0);
87 else if (key.startsWith(
"<") || key.startsWith(
">") ||
88 key.startsWith(
"/") || key.startsWith(
"\\") ||
89 key.startsWith(
"=")) {
91 tree.restructure(key.charAt(0),key.substring(1));
92 app.
halt(
"restructure '"+key.substring(0,1)+
93 "' near '"+key.substring(1)+
"'",200);
95 }
catch (RuntimeException e) {
96 System.err.println(e);
97 app.
halt(e.getMessage(),1);
99 tree.checkConsistency();
102 tree.insert(key,
null);
103 app.
halt(
"insert '"+key+
"'",200);
106 app.
halt(
"QUIT",200);
example: insert, remove, and restructure entries
BinarySearchTree< String, String >.Decorator decorator_
BinarySearchTree< String, String > tree_
static void main(String[] args)
Start interactive example.
Simple viewer for Graphvizable.
void display(String code)
display dot code
Simple framework for single stepping code.
void halt(String text, int timeout)
display text and wait for user or timeout
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.