Skip to content
Open

Heap #42

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# rspec failure tracking
.rspec_status
.idea/
.byebug_history
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions lib/binary_node.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/data_structures/b_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions lib/data_structures/heap.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/data_structures/heap_tree/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module HeapTree
class Node < BinaryNode
attr_accessor :parent

def initialize(value = nil)
super
end
end
end
72 changes: 72 additions & 0 deletions spec/binary_node_spec.rb
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions spec/data_structures/heap_spec.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions spec/data_structures/heap_tree/node_spec.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down