Avatar of Ric0chet

Ric0chet's solution

to Zipper in the Scala Track

Published at Dec 17 2019 · 0 comments
Test suite

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)

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

Submitting Incomplete Solutions

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


import org.scalatest.{FunSuite, Matchers}

/** @version created manually **/
class ZipperTest extends FunSuite with Matchers {
  def empty[A]: Option[BinTree[A]] = None

  def bt[A](v: A, l: Option[BinTree[A]], r: Option[BinTree[A]]): Option[BinTree[A]] =
    Some(BinTree(v, l, r))

  def leaf[A](v: A): Option[BinTree[A]] =
    Some(BinTree(v, None, None))

  val t1: BinTree[Int] = BinTree(1, bt(2, empty,   leaf(3)), leaf(4))
  val t2: BinTree[Int] = BinTree(1, bt(5, empty,   leaf(3)), leaf(4))
  val t3: BinTree[Int] = BinTree(1, bt(2, leaf(5), leaf(3)), leaf(4))
  val t4: BinTree[Int] = BinTree(1, leaf(2),                 leaf(4))

  def fromSome[T](o: Option[T]) = o.get

  val z = Zipper

  test("data is retained") {
   z.toTree(z.fromTree(t1)) should be (t1)

  test("left, right and value") {
    z.value(fromSome(z.right(fromSome(z.left(z.fromTree(t1)))))) should be (3)

  test("dead end") {
    (z.left(fromSome(z.left(z.fromTree(t1))))) should be (None)

  test("tree from deep focus") {
    z.toTree(fromSome(z.right(fromSome(z.left(z.fromTree(t1)))))) should be (t1)

  test("setValue") {
    z.toTree(z.setValue(5, (fromSome(z.left(z.fromTree(t1)))))) should be (t2)

  test("setLeft with Some") {
    z.toTree(z.setLeft(Some(BinTree(5, None, None)),
        (fromSome(z.left(z.fromTree(t1)))))) should be (t3)

  test("setRight with None") {
    z.toTree(z.setRight(None, (fromSome(z.left(z.fromTree(t1)))))) should be (t4)

  test("different paths to same zipper") {
    z.right(fromSome(z.up(fromSome(z.left(z.fromTree(t1)))))) should be
import Zipper._
//  12-17-19

// 39 ms
object Zipper {
  type Crumbs[A] = List[Crumb[A]]
  type Zipper[A] = (BinTree[A], Crumbs[A])
  type ZipOpt[A] = Option[Zipper[A]]
  type BTrOpt[A] = Option[BinTree[A]]

  def fromTree[A](bt: BinTree[A]): Zipper[A] = (bt, List())

  def toTree[A](zip: Zipper[A]): BinTree[A] = zip match {
    case (bt, List()) => bt
    case _            => toTree(up(zip).get)

  def value[A](zip: Zipper[A]): A = zip._1.value  // binTree.value

  def left[A](zip: Zipper[A]): ZipOpt[A] = zip match {
    case (BinTree(x, Some(l), r), bs) => Some((l, lCrumb(x, r) :: bs))
    case _ => None

  def right[A](zip: Zipper[A]): ZipOpt[A] = zip match {
    case (BinTree(x, l, Some(r)), bs) => Some((r, rCrumb(x, l) :: bs))
    case _ => None

  def up[A](zip: Zipper[A]): ZipOpt[A] = zip match {
    case (t, lCrumb(x, r) :: bs) => Some((BinTree(x, Some(t), r), bs))
    case (t, rCrumb(x, l) :: bs) => Some((BinTree(x, l, Some(t)), bs))
    case _ => None

  def setValue[A](v: A, zip: Zipper[A]): Zipper[A] =
    (zip._1.copy(value = v), zip._2)   // (binTree, crumbs)

  def setLeft[A](l: BTrOpt[A], zip: Zipper[A]): Zipper[A] =
    (zip._1.copy(left = l), zip._2)    // (binTree, crumbs)

  def setRight[A](r: BTrOpt[A], zip: Zipper[A]): Zipper[A] =
    (zip._1.copy(right = r), zip._2)   // (binTree, crumbs)

trait Crumb[A]
case class lCrumb[A](value: A, tree: BTrOpt[A]) extends Crumb[A]
case class rCrumb[A](value: A, tree: BTrOpt[A]) extends Crumb[A]  // "R.Crumb" Haha!! =D

case class BinTree[A](value: A, left: BTrOpt[A], right:BTrOpt[A])

Community comments

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

Ric0chet's Reflection

A quick data structure solution (not much latitude for a benchmark comparison).