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