|
|
@@ -2,32 +2,30 @@
|
|
2
|
2
|
|
|
3
|
3
|
Creating a zipper for a binary tree.
|
|
4
|
4
|
|
|
5
|
|
-Zippers are a purely functional way of navigating within a data structure and manipulating it. They essentially contain a data structure and a pointer into that data structure (called the focus).
|
|
6
|
|
-
|
|
7
|
|
-Find out more about Zipper: https://en.wikipedia.org/wiki/Zipper_%28data_structure%29
|
|
8
|
|
-
|
|
9
|
|
-A zipper should be able to perform these operations:
|
|
10
|
|
-
|
|
11
|
|
-* `from_tree` (get a zipper out of a rose tree, the focus is on the root node)
|
|
12
|
|
-* `to_tree` (get the rose tree out of the zipper)
|
|
13
|
|
-* `value` (get the value of the focus node)
|
|
14
|
|
-* `prev` (move the focus to the previous child of the same parent, returns a new zipper)
|
|
15
|
|
-* `next` (move the focus to the next child of the same parent, returns a new zipper)
|
|
16
|
|
-* `up` (move the focus to the parent, returns a new zipper)
|
|
17
|
|
-* `left` (move the focus to the left child, returns a new zipper)
|
|
18
|
|
-* `right` (move the focus to the right child, returns a new zipper)
|
|
19
|
|
-* `set_value` (set the value of the focus node, returns a new zipper)
|
|
20
|
|
-* `set_left` (set the left child of the focus node, returns a new zipper)
|
|
21
|
|
-* `set_right` (set the right child of the focus node, returns a new zipper)
|
|
22
|
|
-* `insert_before` (insert a new subtree before the focus node, it becomes the prev of the focus node, returns a new zipper)
|
|
23
|
|
-* `insert_after` (insert a new subtree after the focus node, it becomes the next of the focus node, returns a new zipper)
|
|
24
|
|
-* `delete` (removes the focus node and all subtrees, focus moves to the next node if possible otherwise to the prev node if possible, otherwise to the parent node, returns a new zipper)
|
|
25
|
|
-
|
|
26
|
|
-Together with zippers, implement binary tree objects that contains a zipper as the root.
|
|
27
|
|
-Trees are encoded as nested objects. Each node in the tree has three members: 'value', 'left', and 'right'. Each value is a number (for simplicity). Left and right are trees. An empty node is encoded as null.
|
|
28
|
|
-Implement `toString` method to print the tree as a single string.
|
|
29
|
|
-
|
|
30
|
|
-An example of a tree is as follow: tree: value: 1, left: { value: 2, left: null, right: { value: 3, left: null, right: null } }, right: { value: 4, left: null, right: null }
|
|
|
5
|
+[Zippers](https://en.wikipedia.org/wiki/Zipper_%28data_structure%29) are
|
|
|
6
|
+a purely functional way of navigating within a data structure and
|
|
|
7
|
+manipulating it. They essentially contain a data structure and a
|
|
|
8
|
+pointer into that data structure (called the focus).
|
|
|
9
|
+
|
|
|
10
|
+For example given a rose tree (where each node contains a value and a
|
|
|
11
|
+list of child nodes) a zipper might support these operations:
|
|
|
12
|
+
|
|
|
13
|
+- `from_tree` (get a zipper out of a rose tree, the focus is on the root node)
|
|
|
14
|
+- `to_tree` (get the rose tree out of the zipper)
|
|
|
15
|
+- `value` (get the value of the focus node)
|
|
|
16
|
+- `prev` (move the focus to the previous child of the same parent,
|
|
|
17
|
+ returns a new zipper)
|
|
|
18
|
+- `next` (move the focus to the next child of the same parent, returns a
|
|
|
19
|
+ new zipper)
|
|
|
20
|
+- `up` (move the focus to the parent, returns a new zipper)
|
|
|
21
|
+- `set_value` (set the value of the focus node, returns a new zipper)
|
|
|
22
|
+- `insert_before` (insert a new subtree before the focus node, it
|
|
|
23
|
+ becomes the `prev` of the focus node, returns a new zipper)
|
|
|
24
|
+- `insert_after` (insert a new subtree after the focus node, it becomes
|
|
|
25
|
+ the `next` of the focus node, returns a new zipper)
|
|
|
26
|
+- `delete` (removes the focus node and all subtrees, focus moves to the
|
|
|
27
|
+ `next` node if possible otherwise to the `prev` node if possible,
|
|
|
28
|
+ otherwise to the parent node, returns a new zipper)
|
|
31
|
29
|
|
|
32
|
30
|
# Running the tests
|
|
33
|
31
|
|