Skip to content

Instantly share code, notes, and snippets.

@iandouglas
Created January 13, 2021 04:29
Show Gist options
  • Save iandouglas/21e56d1c07a65f66c23612a98ddca75e to your computer and use it in GitHub Desktop.
Save iandouglas/21e56d1c07a65f66c23612a98ddca75e to your computer and use it in GitHub Desktop.
Binary Tree Starter Kit
require 'minitest'
require 'minitest/autorun'
require 'minitest/pride'
require './binarytree'
require './node'
class NodeTest < Minitest::Test
def test_it_exists
node = Node.new(99)
assert_instance_of Node, node
end
def test_it_has_a_value
node = Node.new(99)
assert_equal 99, node.data
end
def test_it_has_a_next_attribute_set_to_nil
node = Node.new(99)
assert_nil node.left
assert_nil node.right
end
end
class BinaryTreeTest < Minitest::Test
def test_it_exists
tree = BinaryTree.new
assert_instance_of BinaryTree, tree
end
def test_it_has_a_root_node_set_to_nil
tree = BinaryTree.new
assert_nil tree.root
end
def test_it_can_insert_a_node
tree = BinaryTree.new
tree.insert(23)
refute_nil tree.root
assert_equal 23, tree.root.data
end
def test_it_can_insert_several_nodes_in_the_right_order
tree = BinaryTree.new
tree.insert(61)
tree.insert(16)
tree.insert(92)
tree.insert(50)
refute_nil tree.root
assert_equal 61, tree.root.data
assert_equal 16, tree.root.left.data
assert_equal 92, tree.root.right.data
assert_equal 50, tree.root.left.right.data
end
end
class BinaryTree
attr_reader :root
def initialize
@root = nil
end
def insert(value)
@root = insert_node(@root, value)
end
private
def insert_node(root, value)
# your code goes here
end
end
class Node
attr_reader :data
attr_accessor :left, :right
def initialize(payload)
@data = payload
@left = nil
@right = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment