-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naloga8.java
182 lines (157 loc) · 4.95 KB
/
Naloga8.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.io.*;
import java.util.*;
public class Naloga8 {
private String inputFilename;
private String outputFilename;
private BufferedReader reader;
private PrintWriter writer;
private HashMap<Integer, Node> nodesById;
private Set<Integer> childIds;
private Node rootNode;
private LinkedList<Node> coordinates;
public static void main(String[] args) throws IOException {
Naloga8 program = new Naloga8(args[0], args[1]);
program.initialise();
program.run();
program.cleanUp();
}
private void run() throws IOException {
readInput();
findRootNode();
buildTree();
assignXCoordinates();
print();
}
private ArrayList<Node> getSortedNodes() {
ArrayList<Node> nodes = new ArrayList<>(coordinates);
nodes.sort(Comparator.comparingInt(Node::getDepth).thenComparingInt(Node::getXCoordinate));
return nodes;
}
private void assignXCoordinates() {
int index = 0;
for (Node node : coordinates) {
node.setXCoordinate(index);
index++;
}
}
private void buildTree() {
coordinates = new LinkedList<>();
coordinates.add(rootNode);
insertNodesIntoList(rootNode, 0, 0);
}
private void insertNodesIntoList(Node node, int index, int depth) {
Node left = nodesById.get(node.getLeftId());
Node right = nodesById.get(node.getRightId());
if (right != null) {
coordinates.add(index + 1, right);
right.setDepth(depth + 1);
}
if (left != null) {
coordinates.add(index, left);
left.setDepth(depth + 1);
}
if (right != null) {
insertNodesIntoList(right, left != null ? index + 2 : index + 1, depth + 1);
}
if (left != null) {
insertNodesIntoList(left, index, depth + 1);
}
}
private void findRootNode() {
for (Node node : nodesById.values()) {
if (!childIds.contains(node.getId())) {
rootNode = node;
rootNode.setDepth(0);
}
}
}
private void readInput() throws IOException {
int numberOfNodes = Integer.parseInt(reader.readLine());
nodesById = new HashMap<>();
childIds = new HashSet<>();
for (int i = 0; i < numberOfNodes; i++) {
String nodeString = reader.readLine();
String[] nodeInformation = nodeString.split(",");
int nodeId = Integer.parseInt(nodeInformation[0]);
int nodeValue = Integer.parseInt(nodeInformation[1]);
int leftChildId = Integer.parseInt(nodeInformation[2]);
childIds.add(leftChildId);
int rightChildId = Integer.parseInt(nodeInformation[3]);
childIds.add(rightChildId);
Node node = new Node(nodeId, nodeValue, leftChildId, rightChildId);
nodesById.put(nodeId, node);
}
}
public void print() {
for (Node node : getSortedNodes()) {
writer.print(node.getValue());
writer.print(",");
writer.print(node.getXCoordinate());
writer.print(",");
writer.print(node.getDepth());
writer.println();
}
writer.flush();
}
public Naloga8(String inputFilename, String outputFilename) {
this.inputFilename = inputFilename;
this.outputFilename = outputFilename;
}
public void cleanUp() {
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialise() {
initialiseReaders();
}
private void initialiseReaders() {
try {
reader = new BufferedReader(new FileReader(inputFilename));
writer = new PrintWriter(outputFilename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private class Node {
private int id;
private int value;
private int depth;
private int xCoordinate;
private int leftId;
private int rightId;
public Node(int id, int value, int leftId, int rightId) {
this.id = id;
this.value = value;
this.leftId = leftId;
this.rightId = rightId;
}
public int getId() {
return id;
}
public int getValue() {
return value;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public int getXCoordinate() {
return xCoordinate;
}
public void setXCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
}
public int getLeftId() {
return leftId;
}
public int getRightId() {
return rightId;
}
}
}