AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
RenderTree.java
Go to the documentation of this file.
1package aud.example;
2
3import java.util.Scanner;
4import java.io.File;
5
6import aud.util.Sys;
7import aud.util.Graphviz;
9import aud.util.DotViewer;
11import aud.AVLTree;
12import aud.RedBlackTree;
13import aud.A234Tree;
14import aud.BTree;
15
18public class RenderTree {
19
20 private static void usage() {
21 final String HELP=
22 "usage: java aud.example.RendererTree TREE FORMAT [M]\n"+
23 " Reads and insert words from standard input into tree, which\n"+
24 " is displayed.\n"+
25 " - TREE is one of {b,avl,rb,234,B}.\n"+
26 " - FORMAT is one of {-,svg,pdf}. If FORMAT is '-' the tree is shown\n"+
27 " in a new window. Otherwise, files 'tree.dot' and 'tree.dot.FORMAT'\n"+
28 " are created instead of direct rendering.\n"+
29 " - M is the parameter of a B-tree of order 2-*m+1.\n"
30 ;
31
32 System.err.println(HELP);
33 System.exit(-1);
34 }
35
36 public static void main(String args[]) {
37
38 if (args.length<2)
39 usage();
40
41 String type=null;
42 String format=null;
43
44 if (args[1].compareTo("svg")==0 ||
45 args[1].compareTo("pdf")==0) {
46 format=args[1];
47 }
48 else if (args[1].compareTo("-")!=0) {
49 System.err.println("invalid FORMAT specifier '"+args[1]+"'");
50 usage();
51 }
52
53 Scanner s=new Scanner(System.in);
54 s.useDelimiter("(\\s|;,\\.-\"\\'\\(\\)\\[\\])+");
55
56 Graphvizable showMe=null;
57 BinarySearchTree<String,String> tree=null;
58
59 if (args[0].compareTo("b")==0) {
60 type="binary search";
61 tree=new BinarySearchTree<String,String>();
62 }
63 if (args[0].compareTo("avl")==0) {
64 type="AVL-";
65 tree=new AVLTree<String,String>();
66 }
67 else if (args[0].compareTo("rb")==0) {
68 type="red-black ";
69 tree=new RedBlackTree<String,String>();
70 }
71
72 if (tree!=null) {
73 while (s.hasNext())
74 tree.insert(s.next(),null);
75 showMe=tree;
76 }
77 else {
78 // unfortunately we didn't specify a common interface, so extra case:
79 if (args[0].compareTo("234")==0) {
80 type="2-3-4-";
81 A234Tree<String> t=new A234Tree<String>();
82 while (s.hasNext())
83 t.insert(s.next());
84 showMe=t;
85 }
86 else if (args[0].compareTo("B")==0) {
87 type="B-";
88 if (args.length<3) {
89 System.err.println("missing parameter M");
90 usage();
91 }
92 BTree<String> t=new BTree<String>(Integer.parseInt(args[2]));
93 while (s.hasNext())
94 t.insert(s.next());
95 showMe=t;
96 }
97 else {
98 System.err.println("invalid TREE specifier '"+args[0]+"'");
99 usage();
100 }
101 }
102
103 System.err.println("Rendering output.");
104
105 if (format!=null) {
106 File dotfile=Sys.writeToFile(new File("tree.dot"),showMe.toDot());
107 File outfile=(new Graphviz()).renderDotFileToFile(dotfile,format);
108 System.err.println("Wrote '"+dotfile+"' and '"+outfile+"'"); // no check!
109 }
110 else
111 DotViewer.displayWindow(showMe,type+"tree").setExitOnClose();
112 }
113}
Utility for rendering various trees.
Definition: RenderTree.java:18
static void main(String args[])
Definition: RenderTree.java:36
Simple viewer for Graphvizable.
Definition: DotViewer.java:48
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:140
void setExitOnClose()
exit application if viewer is closed
Definition: DotViewer.java:155
Use GraphViz to render graph structures.
Definition: Graphviz.java:15
System related utilities.
Definition: Sys.java:58
static File writeToFile(File file, String text)
write text to file @endiliteral
Definition: Sys.java:192
Interface for GraphViz rendering.
String toDot()
Get dot representation.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1