Back to home

Test fileBST ImplementationTesting
Ruby Binary Search Trees main image

Ruby Binary Search Trees

This requires the gem installation of minitest.

Test file

Create file binary_search_trees_test.rb:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 begin gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'binary_search_tree' rescue Gem::LoadError => e puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}" puts 'Minitest 5.0 gem must be installed for the Ruby track.' rescue LoadError => e puts "\nError:\n#{e.backtrace.first} #{e.message}" puts DATA.read exit 1 end # Common test data version: 1.1.0 be3ae66 class BinarySearchTreeTest < Minitest::Test def test_insert # skip n1 = Node.new(5) n1.insert(1) n1.insert(8) n1.insert(3) n2 = n1.left n3 = n1.right n4 = n2.right assert_equal 1, n2.data assert_equal 8, n3.data assert_equal 3, n4.data end def test_contain # skip n1 = Node.new(5) n1.insert(1) n1.insert(8) n1.insert(3) n2 = n1.left n3 = n1.right n4 = n2.right assert_equal true, n1.contain(3) != nil assert_equal false, n1.contain(12) != nil end end __END__

BST Implementation

Create file binary_search_trees.rb:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Node attr_reader :data attr_accessor :left, :right def initialize(data) @data = data @left = nil @right = nil end def insert(data) if data < @data && @left == nil @left = Node.new(data) elsif data < @data && @left != nil @left.insert(data) elsif data > @data && @right == nil @right = Node.new(data) elsif data > @data && @right != nil @right.insert(data) end end def contain(data) if @data == data return self elsif data < @data && @left != nil @left.contain(data) elsif data > @data && @right != nil @right.contain(data) end end end

Testing

Change into directory and run ruby binary_search_trees_test.rb.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.