Skip to content

Commit 938fc0d

Browse files
LuLechuanSmarticles101
authored andcommitted
Implement new exercise: Zipper (exercism#1327)
* Implement Zipper * Update config * Update readme * Minor changes * Update all methods to package private
1 parent 1c9d61f commit 938fc0d

8 files changed

Lines changed: 480 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,19 @@
10501050
"unlocked_by": "saddle-points",
10511051
"uuid": "97c8fcd4-85b6-41cb-9de2-b4e1b4951222"
10521052
},
1053+
{
1054+
"core": false,
1055+
"difficulty": 7,
1056+
"slug": "zipper",
1057+
"topics": [
1058+
"integer",
1059+
"nodes",
1060+
"tree",
1061+
"links"
1062+
],
1063+
"unlocked_by": "linked-list",
1064+
"uuid": "25d2c7a5-1ec8-464a-b3de-ad1b27464ef1"
1065+
},
10531066
{
10541067
"core": false,
10551068
"difficulty": 8,

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ include 'two-fer'
9595
include 'word-count'
9696
include 'word-search'
9797
include 'wordy'
98+
include 'zipper'
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
class Zipper {
2+
3+
private int value;
4+
BinaryTree tree; // Only the root node will bind to the BinaryTree
5+
Zipper up;
6+
Zipper left;
7+
Zipper right;
8+
9+
Zipper(int val) {
10+
value = val;
11+
tree = null;
12+
up = null;
13+
left = null;
14+
right = null;
15+
}
16+
17+
/**
18+
* Get a zipper out of a rose tree, the focus is on the root node
19+
*/
20+
Zipper fromTree() {
21+
Zipper current = this;
22+
while (current.up != null) {
23+
current = current.up;
24+
}
25+
return current;
26+
}
27+
28+
BinaryTree toTree() {
29+
Zipper current = this;
30+
while (current.up != null) {
31+
current = current.up;
32+
}
33+
return current.tree;
34+
}
35+
36+
int getValue() {
37+
return value;
38+
}
39+
40+
Zipper setLeft(Zipper leftChild) {
41+
left = leftChild;
42+
if (leftChild != null) {
43+
leftChild.up = this;
44+
leftChild.tree = null;
45+
}
46+
return this;
47+
}
48+
49+
Zipper setRight(Zipper rightChild) {
50+
right = rightChild;
51+
if (rightChild != null) {
52+
rightChild.up = this;
53+
rightChild.tree = null;
54+
}
55+
return this;
56+
}
57+
58+
/**
59+
* Move the focus to the previous child of the same parent
60+
* @return a new Zipper
61+
* Conditions: 1. The current Zipper must have a parent, i.e. it is not the root node.
62+
* 2. The current Zipper must be the right child of its parent
63+
*/
64+
Zipper getPrev() {
65+
Zipper current = this;
66+
try {
67+
current = this.up;
68+
current = current.left;
69+
return current;
70+
} catch (Exception e) {
71+
System.out.println(e);
72+
}
73+
return null;
74+
}
75+
76+
/**
77+
* Move the focus to the next child of the same parent
78+
* @return a new Zipper
79+
* Conditions: 1. The current Zipper must have a parent, i.e. it is not the root node.
80+
* 2. The current Zipper must be the left child of its parent
81+
*/
82+
Zipper getNext() {
83+
Zipper current = this;
84+
try {
85+
current = this.up;
86+
current = current.right;
87+
return current;
88+
} catch (Exception e) {
89+
System.out.println(e);
90+
}
91+
return null;
92+
}
93+
94+
/**
95+
* Move the focus to the parent
96+
* @return a new Zipper
97+
*/
98+
Zipper getUp() {
99+
return up;
100+
}
101+
102+
/**
103+
* Set the value of the focus node, returns a new zipper
104+
* @param val
105+
*/
106+
void setValue(int val) {
107+
this.value = val;
108+
}
109+
110+
/**
111+
* Insert a new subtree before the focus node, it becomes the prev of the focus node
112+
* @return a new Zipper
113+
*/
114+
Zipper insertBefore(Zipper toTheLeft) {
115+
this.left = toTheLeft;
116+
return this.left;
117+
}
118+
119+
/**
120+
* Insert a new subtree after the focus node, it becomes the next of the focus node
121+
* @return a new Zipper
122+
*/
123+
Zipper insertAfter(Zipper toTheRight) {
124+
this.right = toTheRight;
125+
return this.right;
126+
}
127+
128+
/**
129+
* Removes the focus node and all subtrees
130+
* Focus moves to the next node if possible otherwise to the prev node if possible, otherwise to the parent node
131+
* @return a new zipper
132+
*/
133+
Zipper delete() {
134+
Zipper current = this;
135+
if (current.up == null) {
136+
current = null;
137+
} else {
138+
if (current.up.left == current) {
139+
current = current.up;
140+
current.left = null;
141+
if (current.right != null) {
142+
current = current.right;
143+
}
144+
} else {
145+
current = current.up;
146+
current.right = null;
147+
if (current.left != null) {
148+
current = current.left;
149+
}
150+
}
151+
}
152+
return current;
153+
}
154+
155+
}
156+
157+
class BinaryTree {
158+
159+
private Zipper root;
160+
161+
BinaryTree(int value) {
162+
root = new Zipper(value);
163+
root.tree = this;
164+
}
165+
166+
BinaryTree(Zipper root) {
167+
this.root = root;
168+
this.root.tree = this;
169+
}
170+
171+
Zipper getRoot() {
172+
return root;
173+
}
174+
175+
void setRoot(Zipper zipper) {
176+
root = zipper;
177+
root.tree = this;
178+
}
179+
180+
String printTree(Zipper zipper) {
181+
String str = "";
182+
if (zipper.left == null && zipper.right == null) {
183+
str += "value: " + zipper.getValue() + ", left: " + null + ", right: " + null;
184+
} else if (zipper.left == null) {
185+
str += "value: " + zipper.getValue() + ", left: " + null + ", right: { " + printTree(zipper.right) + " }";
186+
} else if (zipper.right == null) {
187+
str += "value: " + zipper.getValue() + ", left: { " + printTree(zipper.left) + " }, right: " + null;
188+
} else {
189+
str += "value: " + zipper.getValue() +
190+
", left: { " + printTree(zipper.left) + " }, right: { " + printTree(zipper.right) + " }";
191+
}
192+
return str;
193+
}
194+
195+
String printTree() {
196+
return printTree(root);
197+
}
198+
199+
}

‎exercises/zipper/.meta/version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

‎exercises/zipper/README.md‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Zipper
2+
3+
Creating a zipper for a binary tree.
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 }
31+
32+
# Running the tests
33+
34+
You can run all the tests for an exercise by entering
35+
36+
```sh
37+
$ gradle test
38+
```
39+
40+
in your terminal.
41+
42+
## Submitting Incomplete Solutions
43+
44+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

‎exercises/zipper/build.gradle‎

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+
}

‎exercises/zipper/src/main/java/.keep‎

Whitespace-only changes.

0 commit comments

Comments
 (0)