Avatar of paulfioravanti

paulfioravanti's solution

to Zipper in the Ruby Track

Published at May 31 2019 · 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)

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby zipper_test.rb

To include color from the command line:

ruby -r minitest/pride zipper_test.rb

Submitting Incomplete Solutions

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

zipper_test.rb

require 'minitest/autorun'
require_relative 'zipper'

# Common test data version: 1.1.0 6fb5601
class ZipperTest < Minitest::Test
  def test_data_is_retained
    # skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.to_tree
    expected =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_left_right_and_value
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.right.value
    expected = 3
    assert_equal expected, value
  end

  def test_dead_end
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.left
    expected = nil
    assert_nil value
  end

  def test_tree_from_deep_focus
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.right.to_tree
    expected =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_traversing_up_from_top
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.up
    expected = nil
    assert_nil value
  end

  def test_left_right_and_up
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.up.right.up.left.right.value
    expected = 3
    assert_equal expected, value
  end

  def test_set_value
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.set_value(5).to_tree
    expected =
      Node.new(1,
        Node.new(5,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_set_value_after_traversing_up
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.right.up.set_value(5).to_tree
    expected =
      Node.new(1,
        Node.new(5,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_set_left_with_leaf
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.set_left(
      Node.new(5,
        nil,
        nil)).to_tree
    expected =
      Node.new(1,
        Node.new(2,
          Node.new(5,
            nil,
            nil),
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_set_right_with_null
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.set_right(nil).to_tree
    expected =
      Node.new(1,
        Node.new(2,
          nil,
          nil),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_set_right_with_subtree
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.set_right(
      Node.new(6,
        Node.new(7,
          nil,
          nil),
        Node.new(8,
          nil,
          nil))).to_tree
    expected =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(6,
          Node.new(7,
            nil,
            nil),
          Node.new(8,
            nil,
            nil)))
    assert_equal expected, value
  end

  def test_set_value_on_deep_focus
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.right.set_value(5).to_tree
    expected =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(5,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    assert_equal expected, value
  end

  def test_different_paths_to_same_zipper
    skip
    tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    zipper = Zipper.from_tree(tree)
    value = zipper.left.up.right
    expected_tree =
      Node.new(1,
        Node.new(2,
          nil,
          Node.new(3,
            nil,
            nil)),
        Node.new(4,
          nil,
          nil))
    expected_zipper = Zipper.from_tree(expected_tree)
    expected = expected_zipper.right
    assert_equal expected, value
  end
end
# frozen_string_literal: true

require "forwardable"

Node = Struct.new(:value, :left, :right)

class Zipper
  extend Forwardable

  def self.from_tree(tree, focus = tree, parents = [])
    new(tree, focus, parents)
  end

  private_class_method :new

  def_delegators :focus, :value, :nil?
  def_delegator :self, :tree, :to_tree
  def_delegator self, :from_tree

  def initialize(tree, focus, parents)
    @tree = tree
    @focus = focus
    @parents = parents
  end

  def left
    from_tree(tree, focus.left, parents.push(focus))
  end

  def right
    from_tree(tree, focus.right, parents.push(focus))
  end

  def up
    from_tree(tree, parents.pop, parents)
  end

  # rubocop:disable Naming/AccessorMethodName
  def set_value(value)
    focus.value = value
    self
  end

  def set_left(node)
    focus.left = node
    self
  end

  def set_right(node)
    focus.right = node
    self
  end
  # rubocop:enable Naming/AccessorMethodName

  def ==(other)
    protected_methods
      .each
      .with_object(other)
      .all?(&method(:equal_to_other?))
  end

  protected

  attr_reader :focus, :tree, :parents

  private

  def equal_to_other?(method, other)
    send(method) == other.send(method)
  end
end

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?