-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTree.java
73 lines (66 loc) · 2.1 KB
/
Tree.java
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
import java.util.ArrayDeque;
import java.util.Deque;
public class Tree {
public Node root;
public double height;
public double width;
public Tree(double height, double width) {
this.height = height;
this.width = width;
this.root = null;
}
public void createTree(Planet[] planets){
this.root = new Node(0, 0, height, width, "0", 0);
for(Planet pl : planets){
this.root.addPlanet(pl);
}
}
public void printTree(){
Deque<Node> queue = new ArrayDeque<>();
queue.addLast(this.root);
Node current;
while (queue.peekFirst() != null) {
current = queue.pollFirst();
if (current.hasPlanet()) {
System.out.println(current.id + ": " + current.planet.toString());
}
else {
System.out.println(current.id + ": Empty node");
}
Node child;
for (int i = 3; i > -1; i--) {
child = current.quadrant[i];
if (child != null) {
queue.addFirst(child);
}
}
}
}
public void prettyPrint(){
Deque<Node> queue = new ArrayDeque<>();
queue.addLast(this.root);
Node current;
while (queue.peekFirst() != null) {
current = queue.pollFirst();
System.out.print("╠");
//System.out.print("mass: " + current.mass + "\t╠");
//System.out.print("center: " + current.centerX + ":" + current.centerY + "\t\t╠");
for(int i = 0; i < current.level; i++){
System.out.print("═══");
}
if (current.hasPlanet()) {
System.out.println(current.planet.toString());
}
else {
System.out.println("Empty node");
}
Node child;
for (int i = 3; i > -1; i--) {
child = current.quadrant[i];
if (child != null) {
queue.addFirst(child);
}
}
}
}
}