diff --git a/.gitignore b/.gitignore index 82cf05e..44084de 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # rspec failure tracking .rspec_status .idea/ +.byebug_history diff --git a/bin/console b/bin/console index ea97587..bc57279 100755 --- a/bin/console +++ b/bin/console @@ -10,6 +10,7 @@ require './lib/numbering_writer' require './lib/check_summing_writer' require './lib/time_stamping_writer' require './lib/data_structures/linked_list/linked_list' +require './lib/data_structures/binary_tree' require './lib/data_structures/linked_list/double_linked_list' require './lib/data_structures/linked_list/circular_linked_list' require './lib/data_structures/linked_list/node' diff --git a/lib/binary_node.rb b/lib/binary_node.rb new file mode 100644 index 0000000..c964d5e --- /dev/null +++ b/lib/binary_node.rb @@ -0,0 +1,9 @@ +class BinaryNode + attr_accessor :left, :right, :value + + def initialize(value=nil, left=nil, right=nil) + @value = value + @left = left + @right = right + end +end diff --git a/lib/data_structures/b_tree.rb b/lib/data_structures/b_tree.rb index 1b90f56..1f5c438 100644 --- a/lib/data_structures/b_tree.rb +++ b/lib/data_structures/b_tree.rb @@ -14,7 +14,7 @@ class BTree attr_accessor :head, :current_node def initialize(head = nil) - @head = Binary::Node.new + @head = head || Binary::Node.new @values = [] end diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb new file mode 100644 index 0000000..c33f83f --- /dev/null +++ b/lib/data_structures/heap.rb @@ -0,0 +1,48 @@ +class Heap + private attr_reader :list + + def initialize(list) + @list = list + end + + def index_of_parent(index) + parent_index = (index - 1) / 2 + return if parent_index.negative? + + parent_index + end + + def value_of_parent(index) + return if index_of_parent(index).nil? + + @list[index_of_parent(index)] + end + + def index_of_left_child(index) + left_child_index = (2 * index) + 1 + return if left_child_index > list.length - 1 + + left_child_index + end + + def value_of_left_child(index) + left_child_index = index_of_left_child(index) + return if left_child_index.nil? + + list[left_child_index] + end + + def index_of_right_child(index) + right_child_index = (2 * index) + 2 + return if right_child_index > list.length - 1 + + right_child_index + end + + def value_of_right_child(index) + right_child_index = index_of_right_child(index) + return if right_child_index.nil? + + list[right_child_index] + end +end diff --git a/lib/data_structures/heap_tree/node.rb b/lib/data_structures/heap_tree/node.rb new file mode 100644 index 0000000..e2b6821 --- /dev/null +++ b/lib/data_structures/heap_tree/node.rb @@ -0,0 +1,9 @@ +module HeapTree + class Node < BinaryNode + attr_accessor :parent + + def initialize(value = nil) + super + end + end +end diff --git a/spec/binary_node_spec.rb b/spec/binary_node_spec.rb new file mode 100644 index 0000000..05373df --- /dev/null +++ b/spec/binary_node_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +RSpec.describe BinaryNode do + subject(:node) { BinaryNode.new } + + describe '#new' do + it 'sets value' do + node.value = 'E' + expect(node.value).to eq('E') + end + + it 'sets left as nil' do + expect(node.left).to be_nil + end + + it 'sets right as nil' do + expect(node.right).to be_nil + end + end + + describe '#left' do + let(:root_node) { node } + let(:left_node) { node } + + context 'when there is not left node' do + it 'is nil for left node' do + expect(root_node.left).to be_nil + end + end + + context 'when there is left node' do + before do + left_node.value = 'E' + root_node.left = left_node + end + + it 'has left node' do + expect(root_node.left).to eq(left_node) + end + + it 'has left node value as E' do + expect(root_node.left.value).to eq(left_node.value) + end + end + end + + describe '#right' do + let(:root_node) { node } + let(:right_node) { node } + + context 'when there is not right node' do + it 'is nil for right node' do + expect(root_node.right).to be_nil + end + end + + context 'when there is right node' do + before do + right_node.value = 'E' + root_node.right = right_node + end + + it 'has right node' do + expect(root_node.right).to eq(right_node) + end + + it 'has right node value as E' do + expect(root_node.right.value).to eq(right_node.value) + end + end + end +end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb new file mode 100644 index 0000000..aa85b02 --- /dev/null +++ b/spec/data_structures/heap_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +RSpec.describe Heap do + subject(:heap) { described_class.new(list) } + let(:list) { [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] } + + describe "#index_of_parent" do + it 'get parent' do + expect(heap.index_of_parent(0)).to be_nil + expect(heap.index_of_parent(1)).to eq(0) + expect(heap.index_of_parent(2)).to eq(0) + expect(heap.index_of_parent(3)).to eq(1) + expect(heap.index_of_parent(4)).to eq(1) + end + end + + describe "#value_of_parent" do + it 'get parent value' do + expect(heap.value_of_parent(0)).to be_nil + expect(heap.value_of_parent(1)).to eq(16) + expect(heap.value_of_parent(2)).to eq(16) + expect(heap.value_of_parent(3)).to eq(14) + expect(heap.value_of_parent(4)).to eq(14) + end + end + + describe "#index_of_left_child" do + it 'get index_of_left_child' do + expect(heap.index_of_left_child(0)).to eq(1) + expect(heap.index_of_left_child(1)).to eq(3) + expect(heap.index_of_left_child(2)).to eq(5) + expect(heap.index_of_left_child(4)).to eq(9) + expect(heap.index_of_left_child(5)).to be_nil + end + end + + describe "#value_of_left_child" do + it 'get index_of_left_child' do + expect(heap.value_of_left_child(0)).to eq(14) + expect(heap.value_of_left_child(1)).to eq(8) + expect(heap.value_of_left_child(2)).to eq(9) + expect(heap.value_of_left_child(4)).to eq(1) + expect(heap.value_of_left_child(5)).to be_nil + end + end + + describe "#index_of_right_child" do + it 'get index_of_right_child' do + expect(heap.index_of_right_child(0)).to eq(2) + expect(heap.index_of_right_child(1)).to eq(4) + expect(heap.index_of_right_child(2)).to eq(6) + expect(heap.index_of_right_child(4)).to be_nil + expect(heap.index_of_right_child(5)).to be_nil + end + end + + describe "#value_of_right_child" do + it 'get value_of_right_child' do + expect(heap.value_of_right_child(0)).to eq(10) + expect(heap.value_of_right_child(1)).to eq(7) + expect(heap.value_of_right_child(2)).to eq(3) + expect(heap.value_of_right_child(4)).to be_nil + expect(heap.value_of_right_child(5)).to be_nil + end + end +end diff --git a/spec/data_structures/heap_tree/node_spec.rb b/spec/data_structures/heap_tree/node_spec.rb new file mode 100644 index 0000000..dc86718 --- /dev/null +++ b/spec/data_structures/heap_tree/node_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +RSpec.describe HeapTree::Node do + it 'has parent nil' do + root_node = described_class.new + expect(root_node.left).to be_nil + expect(root_node.right).to be_nil + expect(root_node.parent).to be_nil + end + + it 'sets and gets attributes' do + root_node = described_class.new(2) + left_node = described_class.new(4) + right_node = described_class.new(6) + + root_node.left = left_node + root_node.right = right_node + right_node.parent = root_node + left_node.parent = root_node + + + expect(root_node.left.value).to eq(4) + expect(root_node.right.value).to eq(6) + expect(root_node.parent).to be_nil + expect(root_node.left.parent.value).to eq(2) + expect(root_node.right.parent.value).to eq(2) + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4dff666..1c1d9d2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,12 +4,15 @@ require "morse_binary_tree" require "binary_search" require "node" +require "binary_node" require "simple_writer" require "writer_decorator" require "numbering_writer" require "time_stamping_writer" require "data_structures/linked_list/linked_list" require "data_structures/b_tree" +require "data_structures/heap" +require "data_structures/heap_tree/node" require "data_structures/linked_list/double_linked_list" require "data_structures/linked_list/circular_linked_list" require "data_structures/linked_list/circular_linked_list/error"