-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseTree.java
More file actions
52 lines (38 loc) · 944 Bytes
/
ParseTree.java
File metadata and controls
52 lines (38 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class ParseTree {
private TreeNode root;
public ParseTree() {
this.root = null;
}
public ParseTree(TreeNode root) {
this.root = root;
}
public TreeNode getRoot() {
return this.root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
private String spaces(int num) {
String s = "";
for (int i = 0; i < num - 1; i++)
s += "| ";
s += "|-";
return s;
}
private String stringify(TreeNode current, int depth) {
String s = current.toString() + "\n";
if (current.getChildren().size() > 0) {
for (int i = 0; i < current.getChildren().size() - 1; i++) {
s += spaces(depth + 1) + stringify(current.getChildren().get(i), depth + 1);
}
s += spaces(depth + 1) + stringify(current.getChildren().get(current.getChildren().size() - 1), depth + 1);
}
return s;
}
@Override
public String toString() {
if (null == root)
return "EMPTY TREE";
return stringify(root, 0);
}
}