Skip to content

Commit f2944d0

Browse files
junming403FridaTveit
authored andcommitted
Add exercise tree-building (exercism#1642)
* add exercise tree-building * Change test to junit4 * fixes according to review * fixes according to review * minor fixes
1 parent 580118a commit f2944d0

13 files changed

Lines changed: 503 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,20 @@
11551155
"time"
11561156
]
11571157
},
1158+
{
1159+
"slug": "tree-building",
1160+
"uuid": "aeaed0e4-0973-4035-8bc5-07480849048f",
1161+
"core": false,
1162+
"unlocked_by": "linked-list",
1163+
"difficulty": 7,
1164+
"topics": [
1165+
"maps",
1166+
"records",
1167+
"refactoring",
1168+
"sorting",
1169+
"trees"
1170+
]
1171+
},
11581172
{
11591173
"slug": "zipper",
11601174
"uuid": "25d2c7a5-1ec8-464a-b3de-ad1b27464ef1",

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ include 'sublist'
9696
include 'sum-of-multiples'
9797
include 'tournament'
9898
include 'transpose'
99+
include 'tree-building'
99100
include 'triangle'
100101
include 'trinary'
101102
include 'twelve-days'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
import java.util.HashMap;
4+
5+
class BuildTree {
6+
7+
TreeNode buildTree(ArrayList<Record> records) throws InvalidRecordsException {
8+
HashMap<Integer, Integer> parentMap = new HashMap<>();
9+
HashMap<Integer, TreeNode> nodesMap = new HashMap<>();
10+
records.sort(Comparator.comparing(Record::getRecordId));
11+
12+
ArrayList<Integer> orderedRecordIds = new ArrayList<>();
13+
14+
for (Record record : records) {
15+
orderedRecordIds.add(record.getRecordId());
16+
}
17+
18+
for (Record record : records) {
19+
validateRecord(record);
20+
parentMap.put(record.getRecordId(), record.getParentId());
21+
nodesMap.put(record.getRecordId(), new TreeNode(record.getRecordId()));
22+
}
23+
24+
int rootId = 0;
25+
26+
for (int i = 0; i < records.size(); i++) {
27+
if (i != orderedRecordIds.get(i)) {
28+
throw new InvalidRecordsException("Invalid Records");
29+
}
30+
if (orderedRecordIds.get(i) != rootId) {
31+
int parentId = parentMap.get(i);
32+
nodesMap.get(parentId).getChildren().add(nodesMap.get(i));
33+
}
34+
}
35+
36+
return nodesMap.get(rootId);
37+
}
38+
39+
private void validateRecord(Record record) throws InvalidRecordsException {
40+
if (record.getRecordId() == record.getParentId() && record.getRecordId() != 0) {
41+
throw new InvalidRecordsException("Invalid Records");
42+
}
43+
44+
if (record.getParentId() > record.getRecordId()) {
45+
throw new InvalidRecordsException("Invalid Records");
46+
}
47+
}
48+
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class InvalidRecordsException extends Exception {
2+
3+
InvalidRecordsException(String message) {
4+
super(message);
5+
}
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Record {
2+
private int recordId;
3+
private int parentId;
4+
5+
public Record(int recordId, int parentId) {
6+
this.recordId = recordId;
7+
this.parentId = parentId;
8+
}
9+
10+
int getParentId() {
11+
return parentId;
12+
}
13+
14+
int getRecordId() {
15+
return recordId;
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.ArrayList;
2+
3+
class TreeNode {
4+
private int nodeId;
5+
ArrayList<TreeNode> children;
6+
7+
TreeNode(int nodeId) {
8+
this.nodeId = nodeId;
9+
this.children = new ArrayList<>();
10+
}
11+
12+
public int getNodeId() {
13+
return nodeId;
14+
}
15+
16+
public ArrayList<TreeNode> getChildren() {
17+
return children;
18+
}
19+
}

‎exercises/tree-building/README.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Tree Building
2+
3+
Refactor a tree building algorithm.
4+
5+
Some web-forums have a tree layout, so posts are presented as a tree. However
6+
the posts are typically stored in a database as an unsorted set of records. Thus
7+
when presenting the posts to the user the tree structure has to be
8+
reconstructed.
9+
10+
Your job will be to refactor a working but slow and ugly piece of code that
11+
implements the tree building logic for highly abstracted records. The records
12+
only contain an ID number and a parent ID number. The ID number is always
13+
between 0 (inclusive) and the length of the record list (exclusive). All records
14+
have a parent ID lower than their own ID, except for the root record, which has
15+
a parent ID that's equal to its own ID.
16+
17+
An example tree:
18+
19+
```text
20+
root (ID: 0, parent ID: 0)
21+
|-- child1 (ID: 1, parent ID: 0)
22+
| |-- grandchild1 (ID: 2, parent ID: 1)
23+
| +-- grandchild2 (ID: 4, parent ID: 1)
24+
+-- child2 (ID: 3, parent ID: 0)
25+
| +-- grandchild3 (ID: 6, parent ID: 3)
26+
+-- child3 (ID: 5, parent ID: 0)
27+
```
28+
29+
# Running the tests
30+
31+
You can run all the tests for an exercise by entering
32+
33+
```sh
34+
$ gradle test
35+
```
36+
37+
in your terminal.
38+
39+
## Submitting Incomplete Solutions
40+
41+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
4+
class BuildTree {
5+
6+
TreeNode buildTree(ArrayList<Record> records) throws InvalidRecordsException {
7+
records.sort(Comparator.comparing(Record::getRecordId));
8+
ArrayList<Integer> orderedRecordIds = new ArrayList<>();
9+
10+
for (Record record : records) {
11+
orderedRecordIds.add(record.getRecordId());
12+
}
13+
14+
if (records.size() > 0) {
15+
if (orderedRecordIds.get(orderedRecordIds.size() - 1) != orderedRecordIds.size() - 1) {
16+
throw new InvalidRecordsException("Invalid Records");
17+
}
18+
if (orderedRecordIds.get(0) != 0) {
19+
throw new InvalidRecordsException("Invalid Records");
20+
}
21+
}
22+
23+
ArrayList<TreeNode> treeNodes = new ArrayList<>();
24+
25+
for (int i = 0; i < orderedRecordIds.size(); i++) {
26+
for (Record record : records) {
27+
if (orderedRecordIds.get(i) == record.getRecordId()) {
28+
if (record.getRecordId() == 0 && record.getParentId() != 0) {
29+
throw new InvalidRecordsException("Invalid Records");
30+
}
31+
if (record.getRecordId() < record.getParentId()) {
32+
throw new InvalidRecordsException("Invalid Records");
33+
}
34+
if (record.getRecordId() == record.getParentId() && record.getRecordId() != 0) {
35+
throw new InvalidRecordsException("Invalid Records");
36+
}
37+
treeNodes.add(new TreeNode(record.getRecordId()));
38+
}
39+
}
40+
}
41+
42+
for (int i = 0; i < orderedRecordIds.size(); i++) {
43+
TreeNode parent;
44+
for (TreeNode n: treeNodes) {
45+
if (i == n.getNodeId()) {
46+
parent = n;
47+
for (Record record : records) {
48+
if (record.getParentId() == i) {
49+
for (TreeNode node : treeNodes) {
50+
if (node.getNodeId() == 0) {
51+
continue;
52+
}
53+
if (record.getRecordId() == node.getNodeId()) {
54+
parent.getChildren().add(node);
55+
}
56+
}
57+
}
58+
}
59+
break;
60+
}
61+
}
62+
63+
}
64+
65+
if (treeNodes.size() > 0) {
66+
return treeNodes.get(0);
67+
}
68+
69+
return null;
70+
}
71+
72+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class InvalidRecordsException extends Exception {
2+
3+
InvalidRecordsException(String message) {
4+
super(message);
5+
}
6+
}

0 commit comments

Comments
 (0)