AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
RedBlackTreeExample.java
Go to the documentation of this file.
1package aud.example;
2
3import aud.util.*;
4import aud.RedBlackTree;
6import java.util.Scanner;
7
11public class RedBlackTreeExample extends SingleStepper {
12
13 class MyRedBlackTree extends RedBlackTree<String,String> {
14 RedBlackTreeExample app_ = null;
15 public MyRedBlackTree(RedBlackTreeExample app) { super(); }
16
17 @Override protected void onInsert(Node node) {
18 ((SimpleDecorator) tree_.getDecorator()).highlightNode(node);
19 halt("insert '"+node.getKey()+"' before rebalancing");
20 ((SimpleDecorator) tree_.getDecorator()).highlightNode(null);
21 super.onInsert(node);
22 }
23 @Override protected void onRestructuring() {
24 halt("- after restructuring");
25 super.onRestructuring();
26 }
27 @Override protected void onRecoloring() {
28 halt("- after one recoloring step");
29 super.onRestructuring();
30 }
31 }
32
33 protected MyRedBlackTree tree_ = null;
34 protected DotViewer viewer_ = DotViewer.displayWindow
35 ((String) null,"aud.example.RedBlackTreeExample");
36
39 super("aud.example.RedBlackTreeExample");
40 tree_=new MyRedBlackTree(this);
41 }
42
43 public MyRedBlackTree getTree() { return tree_; }
44
45 @Override protected void onHalt() {
46 if (tree_!=null)
48 }
49
51 public static void main(String[] args) {
52
53 final String HELP=
54 "usage: java aud.example.RedBlackTreeExample [pause]\n"+
55 " Reads and insert words from standard input.\n"+
56 " 'quit' quits.\n"+
57 "\tpause [milliseconds] set pause between animation steps\n"+
58 "\t A value of 0 requires to explicitly push the 'continue'\n"+
59 "\t button. The default value is 0 (or the value of then\n"+
60 "\t environment variable 'AUD_TIMEOUT')!\n";
61
63
64 if (args.length>0) {
65 try {
66 app.setTimeout(Integer.parseInt(args[0]));
67 } catch (NumberFormatException e) {
68 System.err.println(HELP);
69 System.exit(-1);
70 }
71 }
72
73 app.halt("EMPTY TREE");
74
75 Scanner s=new Scanner(System.in);
76 s.useDelimiter("\\s+");
77
78 while (s.hasNext()) {
79 String key=s.next();
80 if (key.compareTo("quit")==0)
81 break;
82 else {
83 app.getTree().insert(key,null);
84 app.halt("inserted '"+key+"' and rebalanced");
85 }
86 }
87 app.halt("QUIT");
88 System.exit(0);
89 }
90}
example: insert entries and maintain balance
static void main(String[] args)
start interactive example
RedBlackTreeExample()
create application instance
Simple viewer for Graphvizable.
Definition: DotViewer.java:48
void display(String code)
display dot code
Definition: DotViewer.java:108
Example for a simple decorator.
Simple framework for single stepping code.
void halt()
wait for user
void setTimeout(int timeout)
Set global timeout.
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