Avatar of cmccandless

cmccandless'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.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercism.zipper
{
	class Zipper<T>
	{
		public T Value { get { return pointer == null ? default(T) : pointer.Value; } }
		private BinTree<T> pointer;
		private Zipper(BinTree<T> tree)
		{
			pointer = tree;
		}
		public static Zipper<T> FromTree(BinTree<T> tree)
		{
			return new Zipper<T>(tree);
		}
		public Zipper<T> Up()
		{
			return pointer.Parent == null ? null : new Zipper<T>(pointer.Parent);
		}
		public Zipper<T> Right() 
		{
			return pointer.Right == null ? null : new Zipper<T>(pointer.Right);
		}
		public Zipper<T> Left()
		{
			return pointer.Left == null ? null : new Zipper<T>(pointer.Left);
		}
		public Zipper<T> SetValue(T v)
		{
			pointer.Value = v;
			return this;
		}
		public Zipper<T> SetRight(BinTree<T> tree)
		{
			pointer.Right = tree;
			return this;
		}
		public Zipper<T> SetLeft(BinTree<T> tree)
		{
			pointer.Left = tree;
			return this;
		}
		public BinTree<T> ToTree()
		{
			return pointer.Parent == null ? new BinTree<T>(pointer) : this.Up().ToTree();
		}
	}
	class BinTree<T>
	{
		public T Value;
		public BinTree<T> Parent;
		private BinTree<T> left;
		public BinTree<T> Left
		{
			get { return left; }
			set
			{
				if (left != null) left.Parent = null;
				left = value;
				if (left != null) left.Parent = this;
			}
		}
		private BinTree<T> right;
		public BinTree<T> Right
		{
			get { return right; }
			set
			{
				if (right != null) right.Parent = null;
				right = value;
				if (right != null) right.Parent = this;
			}
		}
		public BinTree(T v, BinTree<T> left, BinTree<T> right)
		{
			this.Value = v;
			this.Left = left;
			this.Right = right;
			this.Parent = null;
		}
		public BinTree(BinTree<T> tree, int direction = 0)
		{
			this.Value = tree.Value;
			if (direction >= 0)
			{
				if (tree.left != null) this.left = new BinTree<T>(tree.left,1);
				if (tree.right != null) this.right = new BinTree<T>(tree.right,1);
			}
			else
			{
				this.left = tree.left;
				this.right = tree.right;
			}
			if (direction <= 0) 
			{
				if (tree.Parent != null) this.Parent = new BinTree<T>(tree.Parent, -1);
			}
			else this.Parent = tree.Parent;
		}
		public override string ToString()
		{
			var result = Value.ToString();
			var leftNull = Left != null;
			var rightNull = Right != null;
			if (leftNull || rightNull) result += ':';
			if (leftNull) result += " L(" + Left.ToString() + ")";
			if (rightNull) result += " R(" + Right.ToString() + ")";
			return result;
		}
		public override bool Equals(object obj)
		{
			var other = obj as BinTree<T>;
			if (other == null) return false;
			if (!this.Value.Equals(other.Value)) return false;
			if (this.Left == null && other.Left != null) return false;
			if (this.Left != null && !this.Left.Equals(other.Left)) return false;
			if (this.Right == null && other.Right != null) return false;
			if (this.Right != null && !this.Right.Equals(other.Right)) return false;
			return true;
		}
		public override int GetHashCode()
		{
			return this.ToString().GetHashCode();
		}
	}
}

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?