Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created New Exercise: Retree #1424

Merged
merged 11 commits into from
Jan 8, 2019
66 changes: 66 additions & 0 deletions exercises/retree/canonical-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"exercise": "retree",
"version": "1.0.0",
"cases": [
{
"description": "Empty tree",
"property": "treeFromTraversals",
"input": {
"preorder": [],
"inorder": []
},
"expected": {}
},
{
"description": "Tree with one item",
"property": "treeFromTraversals",
"input": {
"preorder": ["a"],
"inorder": ["a"]
},
"expected": {"v":"a", "l": {}, "r": {}}
This conversation was marked as resolved.
Show resolved Hide resolved
},
{
"description": "Tree with many items",
"property": "treeFromTraversals",
"input": {
"preorder": ["a", "i", "x", "f", "r"],
"inorder": ["i", "a", "f", "x", "r"]
},
"expected": {"v": "a",
"l": {"v": "i", "l": {}, "r": {}},
"r": {"v": "x",
"l": {"v": "f", "l": {}, "r": {}},
"r": {"v": "r", "l": {}, "r": {}}
}
}
},
{
"description": "Reject traversals of different length",
"property": "treeFromTraversals",
"input": {
"preorder": ["a", "b"],
"inorder": ["b", "a", "r"]
},
"expected": {"error": "traversals must have the same length"}
},
{
"description": "Reject inconsistent traversals of same length",
"property": "treeFromTraversals",
"input": {
"preorder": ["x", "y", "z"],
"inorder": ["a", "b", "c"]
},
"expected": {"error": "traversals must have the same elements"}
},
{
"description": "Reject traversals with repeated items",
"property": "treeFromTraversals",
"input": {
"preorder": ["a", "b", "a"],
"inorder": ["b", "a", "a"]
},
"expected": {"error": "traversals must contain unique items"}
}
]
}
27 changes: 27 additions & 0 deletions exercises/retree/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Imagine you need to transmit a binary tree to a satellite approaching Alpha
Centauri and you have limited bandwidth. Since the tree has no repeating
items it can be uniquely represented by its [pre-order and in-order traversals][wiki].

Write the software for the satellite to rebuild the tree from the traversals.

A pre-order traversal reads the value of the current node before (hence "pre")
sshine marked this conversation as resolved.
Show resolved Hide resolved
reading the left subtree in pre-order. Afterwards the right subtree is read
in pre-order.

An in-order traversal reads the left subtree in-order then the current node and
finally the right subtree in-order. So in order from left to right.

For example the pre-order traversal of this tree is [a, i, x, f, r].
The in-order traversal of this tree is [i, a, f, x, r]

```
a
/ \
i x
/ \
f r
```

Note: the first item in the pre-order traversal is always the root.

[wiki]: https://en.wikipedia.org/wiki/Tree_traversal
3 changes: 3 additions & 0 deletions exercises/retree/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: "Retree"
blurb: "Rebuild binary trees from pre-order and in-order traversals."