Creating a zipper for a binary tree.
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).
For example given a rose tree (where each node contains a value and a list of child nodes) a zipper might support these operations:
from_tree
(get a zipper out of a rose tree, the focus is on the root node)to_tree
(get the rose tree out of the zipper)value
(get the value of the focus node)prev
(move the focus to the previous child of the same parent,
returns a new zipper)next
(move the focus to the next child of the same parent, returns a
new zipper)up
(move the focus to the parent, returns a new zipper)set_value
(set the value of the focus node, returns a new zipper)insert_before
(insert a new subtree before the focus node, it
becomes the prev
of the focus node, returns a new zipper)insert_after
(insert a new subtree after the focus node, it becomes
the next
of the focus node, returns a new zipper)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)In order to run the tests, issue the following command from the exercise directory:
For running the tests provided, rebar3
is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.
In order to run the tests, you can issue the following command from the exercise directory.
$ rebar3 eunit
For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
-module(zipper_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
%% Fixtures and helpers
bt(V, L, R) -> zipper:new_tree(V, L, R).
empty() -> nil.
leaf(V) -> zipper:new_tree(V, empty(), empty()).
t1() -> bt(1, bt(2, empty(), leaf(3)), leaf(4)).
t2() -> bt(1, bt(5, empty(), leaf(3)), leaf(4)).
t3() -> bt(1, bt(2, leaf(5), leaf(3)), leaf(4)).
t4() -> bt(1, leaf(2), leaf(4)).
t5() -> bt(1, bt(2, empty(), leaf(3)), bt(6, leaf(7), leaf(8))).
t6() -> bt(1, bt(2, empty(), leaf(5)), leaf(4)).
data_is_retained_test() ->
Exp = t1(),
?assertMatch(Exp, zipper:to_tree(zipper:from_tree(t1()))).
left_right_and_value_test() ->
?assertMatch(3, zipper:value(zipper:right(zipper:left(zipper:from_tree(t1()))))).
dead_end_test() ->
Exp = empty(),
?assertMatch(Exp, zipper:left(zipper:left(zipper:from_tree(t1())))).
tree_from_deep_focus_test() ->
Exp = t1(),
Zipper = zipper:right(zipper:left(zipper:from_tree(t1()))),
?assertMatch(Exp, zipper:to_tree(Zipper)).
traversing_up_from_top_test() ->
Exp = empty(),
?assertMatch(Exp, zipper:up(zipper:from_tree(t1()))).
left_right_and_up_test() ->
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:up(Act1),
Act3 = zipper:right(Act2),
Act4 = zipper:up(Act3),
Act5 = zipper:left(Act4),
Act6 = zipper:right(Act5),
?assertMatch(3, zipper:value(Act6)).
set_value_test() ->
Exp = t2(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_value(Act1, 5),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).
set_value_after_traversiing_up_test() ->
Exp = t2(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:right(Act1),
Act3 = zipper:up(Act2),
Act4 = zipper:set_value(Act3, 5),
Act5 = zipper:to_tree(Act4),
?assertMatch(Exp, Act5).
set_left_with_leaf_test() ->
Exp = t3(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_left(Act1, leaf(5)),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).
set_right_with_empty_test() ->
Exp = t4(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:set_right(Act1, empty()),
Act3 = zipper:to_tree(Act2),
?assertMatch(Exp, Act3).
set_right_with_subtree_test() ->
Exp = t5(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:set_right(Act0, bt(6, leaf(7), leaf(8))),
Act2 = zipper:to_tree(Act1),
?assertMatch(Exp, Act2).
set_value_from_deep_focus_test() ->
Exp = t6(),
Act0 = zipper:from_tree(t1()),
Act1 = zipper:left(Act0),
Act2 = zipper:right(Act1),
Act3 = zipper:set_value(Act2, 5),
Act4 = zipper:to_tree(Act3),
?assertMatch(Exp, Act4).
-module(zipper).
-export([from_tree/1, left/1, new_tree/3, right/1,
set_left/2, set_right/2, set_value/2, to_tree/1, up/1,
value/1]).
new_tree(V, L, R) -> {L, V, R}.
from_tree(Tree) -> {Tree, []}.
to_tree({Curr, []}) -> Curr;
to_tree(Zipper) -> to_tree(up(Zipper)).
value({{_, V, _}, _}) -> V.
set_value({{L, _, R}, Trail}, V) ->
{{L, V, R}, Trail}.
set_left({{_, V, R}, Trail}, L) ->
{{L, V, R}, Trail}.
set_right({{L, V, _}, Trail}, R) ->
{{L, V, R}, Trail}.
left({{nil, _, _}, _}) -> nil;
% add Parent node to Tail instead just Value and RightChild for ease of understanding
left({Parent={L, _, _}, Trail}) ->
{L, [{left, Parent} | Trail]}.
right({{_, _, nil}, _}) -> nil;
right({Parent={_, _, R}, Trail}) ->
{R, [{right, Parent} | Trail]}.
up({_, []}) -> nil;
up({Curr, [{left, _Parent={_, V, R}} | T]}) ->
{{Curr, V, R}, T};
up({Curr, [{right, _Parent={L, V, _}} | T]}) ->
{{L, V, Curr}, T}.
A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments