-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualizeBT.java
More file actions
145 lines (131 loc) · 3.62 KB
/
VisualizeBT.java
File metadata and controls
145 lines (131 loc) · 3.62 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Queue;
import java.util.Stack;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
class BinaryTree {
Node root;
BinaryTree() {
this.root = null;
}
class Node implements Comparable<Node> {
Node left;
Node right;
int data;
int displacement;
Node(int d) {
this.right = null;
this.left = null;
this.data = d;
}
@Override
public int compareTo(Node a) {
return this.displacement - a.displacement;
}
@Override
public String toString() {
return this.data + "";
}
}
private Node add(Node root, int v) {
if (root == null)
return new Node(v);
if (root.data > v)
root.left = add(root.left, v);
else
root.right = add(root.right, v);
return root;
}
public void add(int v) {
this.root = add(this.root, v);
}
public void printLevel() {
Queue<Node> q = new LinkedList<>();
Node r = this.root;
q.add(r);
q.add(null);
while (!q.isEmpty()) {
Node temp = q.remove();
if (temp == null) {
System.out.println();
System.out.println("-------------");
if (q.isEmpty())
break;
q.add(null);
} else {
System.out.print(temp.data + " ");
if (temp.left != null)
q.add(temp.left);
if (temp.right != null)
q.add(temp.right);
}
}
}
private void inorder(int s, Node root, ArrayList<Node> arr) {
if (root == null)
return;
inorder(s - 1, root.left, arr);
root.displacement = s;
arr.add(root);
inorder(s + 1, root.right, arr);
}
public int[] getDisplacement() {
ArrayList<Node> arr = new ArrayList<>();
inorder(0, this.root, arr);
Collections.sort(arr);
int[] res = new int[] { arr.get(0).displacement, arr.get(arr.size() - 1).displacement };
return res;
}
public void visualize() {
int[] minmax = getDisplacement();
Queue<Node> q = new LinkedList<>();
Node r = this.root;
q.add(r);
q.add(null);
int level = 0;
String a = "";
int min;
int max;
while (!q.isEmpty()) {
Node temp = q.remove();
if (temp == null) {
System.out.println(a);
a = "";
// System.out.println("-------------");
System.out.println();
level++;
if (q.isEmpty())
break;
q.add(null);
} else {
min = minmax[0];
while (min < temp.displacement - (a.length() / 5)) {
min++;
a += " ";
}
a += temp.data + " ";
if (temp.left != null)
q.add(temp.left);
if (temp.right != null)
q.add(temp.right);
}
}
}
}
public class VisualizeBT {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.add(10);
tree.add(30);
tree.add(40);
tree.add(5);
tree.add(6);
tree.add(8);
tree.add(-2);
tree.add(2);
tree.add(1);
// tree.printLevel();
tree.visualize();
}
}