AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
BinarySearchTreeExample.java
Go to the documentation of this file.
1package aud.example;
2
3import aud.util.*;
5import java.util.Scanner;
6
11
12 protected BinarySearchTree<String,String> tree_ = null;
13 protected DotViewer viewer_ = DotViewer.displayWindow
14 ((String) null,"aud.example.BinarySearchTreeExample");
15 protected BinarySearchTree<String,String>.Decorator decorator_;
16
18 @SuppressWarnings("unchecked") // see below
19 public BinarySearchTreeExample(BinarySearchTree<String,String> tree) {
20 super("aud.example.BinarySearchTreeExample");
21 tree_=tree;
22 decorator_=(BinarySearchTree.Decorator)
23 //(BinarySearchTree<String,String>.Decorator) // compile fails on some platforms!?
24 tree_.getDecorator(); // same per tree
25 }
26
27 @Override protected void onHalt() {
28 if (tree_!=null)
30 }
31
35 public static void main(String[] args) {
36
37 final String HELP=
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.";
52
53 if (args.length>0) {
54 System.err.println(HELP);
55 System.exit(-1);
56 }
57
58 BinarySearchTree<String,String> tree=
59 new BinarySearchTree<String,String>();
61
62 app.halt("EMPTY TREE",1);
63
64 Scanner s=new Scanner(System.in);
65 s.useDelimiter("\\s+");
66
67 while (s.hasNext()) {
68 String key=s.next();
69 if (key.compareTo("quit")==0)
70 break;
71 else if (key.startsWith("?")) {
72 System.err.println(HELP);
73 }
74 else if (key.startsWith("!")) {
75 key=key.substring(1);
76 app.decorator_.highlight(key);
77 app.halt("highlight '"+key+"'",10);
78 }
79 else if (key.startsWith("-")) {
80 key=key.substring(1);
81 tree.remove(key);
82 app.halt("remove '"+key+"'",200);
83 }
84 else if (key.startsWith("~")) {
85 app.halt("PAUSE '"+key.substring(1)+"'",0);
86 }
87 else if (key.startsWith("<") || key.startsWith(">") ||
88 key.startsWith("/") || key.startsWith("\\") ||
89 key.startsWith("=")) {
90 try {
91 tree.restructure(key.charAt(0),key.substring(1));
92 app.halt("restructure '"+key.substring(0,1)+
93 "' near '"+key.substring(1)+"'",200);
94 //System.out.println(tree.toText());
95 } catch (RuntimeException e) {
96 System.err.println(e);
97 app.halt(e.getMessage(),1);
98 }
99 tree.checkConsistency();
100 }
101 else {
102 tree.insert(key,null);
103 app.halt("insert '"+key+"'",200);
104 }
105 }
106 app.halt("QUIT",200);
107 System.exit(0);
108 }
109}
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.
Definition: DotViewer.java:48
void display(String code)
display dot code
Definition: DotViewer.java:108
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)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1