Avatar of insideoutclub

insideoutclub's solution

to Zipper in the C# Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

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)

Hints

This exercise deals with custom equality. For more information see this page.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ZipperTest.cs

// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class ZipperTest
{
    [Fact]
    public void Data_is_retained()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.ToTree();
        var expected = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Left_right_and_value()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Right().Value();
        var expected = 3;
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Dead_end()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Left();
        Assert.Null(actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Tree_from_deep_focus()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Right().ToTree();
        var expected = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Traversing_up_from_top()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Up();
        Assert.Null(actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Left_right_and_up()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Up().Right().Up().Left().Right().Value();
        var expected = 3;
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_value()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().SetValue(5).ToTree();
        var expected = new BinTree(1, new BinTree(5, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_value_after_traversing_up()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Right().Up().SetValue(5).ToTree();
        var expected = new BinTree(1, new BinTree(5, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_left_with_leaf()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().SetLeft(new BinTree(5, null, null)).ToTree();
        var expected = new BinTree(1, new BinTree(2, new BinTree(5, null, null), new BinTree(3, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_right_with_null()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().SetRight(null).ToTree();
        var expected = new BinTree(1, new BinTree(2, null, null), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_right_with_subtree()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.SetRight(new BinTree(6, new BinTree(7, null, null), new BinTree(8, null, null))).ToTree();
        var expected = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(6, new BinTree(7, null, null), new BinTree(8, null, null)));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Set_value_on_deep_focus()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Right().SetValue(5).ToTree();
        var expected = new BinTree(1, new BinTree(2, null, new BinTree(5, null, null)), new BinTree(4, null, null));
        Assert.Equal(expected, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Different_paths_to_same_zipper()
    {
        var tree = new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null));
        var sut = Zipper.FromTree(tree);
        var actual = sut.Left().Up().Right();
        var expected = Zipper.FromTree(new BinTree(1, new BinTree(2, null, new BinTree(3, null, null)), new BinTree(4, null, null))).Right();
        Assert.Equal(expected, actual);
    }
}
using System;
using System.Collections.Immutable;

public class BinTree<T> : IEquatable<BinTree<T>>
{
    public BinTree(T value, BinTree<T> left, BinTree<T> right) => (Value, Left, Right) = (value, left, right);

    public T Value { get; }
    public BinTree<T> Left { get; }
    public BinTree<T> Right { get; }

    public bool Equals(BinTree<T> other) => (Value, Left, Right).Equals((other.Value, other.Left, other.Right));
}

// Adapted from http://learnyouahaskell.com/zippers
public struct Crumb<T>
{
    public bool IsLeft { get; }
    public T Value { get; }
    public BinTree<T> Tree { get; }
    public Crumb(bool isLeft, T value, BinTree<T> tree) => (IsLeft, Value, Tree) = (isLeft, value, tree);
}

public class Zipper<T>
{   
    public BinTree<T> Tree { get; }
    public IImmutableStack<Crumb<T>> BreadCrumbs { get; }

    private Zipper(BinTree<T> tree, IImmutableStack<Crumb<T>> breadCrumbs) => (Tree, BreadCrumbs) = (tree, breadCrumbs);

    public T Value { get => Tree.Value; }

    public Zipper<T> SetValue(T newValue) => new Zipper<T>(new BinTree<T>(newValue, Tree.Left, Tree.Right), BreadCrumbs);

    public Zipper<T> SetLeft(BinTree<T> binTree) => new Zipper<T>(new BinTree<T>(Tree.Value, binTree, Tree.Right), BreadCrumbs);

    public Zipper<T> SetRight(BinTree<T> binTree) => new Zipper<T>(new BinTree<T>(Tree.Value, Tree.Left, binTree), BreadCrumbs);

    public Zipper<T> Left() => Tree.Left == null ? null : new Zipper<T>(Tree.Left, BreadCrumbs.Push(new Crumb<T>(true, Tree.Value, Tree.Right)));

    public Zipper<T> Right() => Tree.Right == null ? null : new Zipper<T>(Tree.Right, BreadCrumbs.Push(new Crumb<T>(false, Tree.Value, Tree.Left)));

    public Zipper<T> Up() => new Zipper<T>(new BinTree<T>(BreadCrumbs.Peek().Value, BreadCrumbs.Peek().IsLeft ? Tree : BreadCrumbs.Peek().Tree, BreadCrumbs.Peek().IsLeft ? BreadCrumbs.Peek().Tree : Tree), BreadCrumbs.Pop());

    public BinTree<T> ToTree()
    {
        for (var that = this; ; that = that.Up())
            if(that.BreadCrumbs.IsEmpty)
                return that.Tree;
    }

    public static Zipper<T> FromTree(BinTree<T> tree) => new Zipper<T>(tree, ImmutableStack<Crumb<T>>.Empty);
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?