From d7c8bf43b39174ed3a35e3cc4a2cb44effdf07dc Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 13:52:53 -0300 Subject: [PATCH 01/19] test for Heap max heapify --- bin/console | 1 + lib/data_structures/b_tree.rb | 2 +- lib/data_structures/heap.rb | 6 ++++++ spec/data_structures/heap_spec.rb | 20 ++++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 lib/data_structures/heap.rb create mode 100644 spec/data_structures/heap_spec.rb 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/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..2dce7bb --- /dev/null +++ b/lib/data_structures/heap.rb @@ -0,0 +1,6 @@ +class Heap < BTree + def initialize(head = nil) + @head = head + super(@head) + end +end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb new file mode 100644 index 0000000..935dacb --- /dev/null +++ b/spec/data_structures/heap_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +RSpec.describe Heap do + it 'is truthy' do + expect(described_class.new).to be_truthy + end + + it 'max heapify' do + node = Binary::Node.new(4) + heap = described_class.new(node) + heap.head.left = Binary::Node.new(2) + heap.head.right = Binary::Node.new(8) + + heap.max_heapify + + expect(heap.head.value).to eq(8) + expect(heap.head.left.value).to eq(2) + expect(heap.head.right.value).to eq(4) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4dff666..2451c7e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,6 +10,7 @@ require "time_stamping_writer" require "data_structures/linked_list/linked_list" require "data_structures/b_tree" +require "data_structures/heap" 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" From 96b892e458a4b6f3205422a1e55c97a06c1ea2d5 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 20:03:10 -0300 Subject: [PATCH 02/19] Add HeapTree::Node It gets and sets left, right, value and parent. I need it to test if a simple binary node can be extended to a heap node. --- lib/data_structures/heap_tree/node.rb | 9 +++++++ spec/data_structures/heap_tree/node_spec.rb | 29 +++++++++++++++++++++ spec/spec_helper.rb | 1 + 3 files changed, 39 insertions(+) create mode 100644 lib/data_structures/heap_tree/node.rb create mode 100644 spec/data_structures/heap_tree/node_spec.rb diff --git a/lib/data_structures/heap_tree/node.rb b/lib/data_structures/heap_tree/node.rb new file mode 100644 index 0000000..2bcf555 --- /dev/null +++ b/lib/data_structures/heap_tree/node.rb @@ -0,0 +1,9 @@ +module HeapTree + class Node + attr_accessor :left, :right, :value, :parent + + def initialize(value = nil) + @value = value + 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 2451c7e..8a73a96 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,6 +11,7 @@ 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" From d30c39b31b0432ec1dfe08dd7efbc42e68429a8e Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 20:10:54 -0300 Subject: [PATCH 03/19] add binary_node --- lib/binary_node.rb | 9 +++++ spec/binary_node_spec.rb | 72 ++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 1 + 3 files changed, 82 insertions(+) create mode 100644 lib/binary_node.rb create mode 100644 spec/binary_node_spec.rb 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/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/spec_helper.rb b/spec/spec_helper.rb index 8a73a96..1c1d9d2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require "morse_binary_tree" require "binary_search" require "node" +require "binary_node" require "simple_writer" require "writer_decorator" require "numbering_writer" From 94c2c9c38697e14653808a035adb9eb724631eb3 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 20:14:18 -0300 Subject: [PATCH 04/19] heap tree node inherits from binary node Refactor heap tree node. Its initializer simply use super. --- lib/data_structures/heap_tree/node.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/data_structures/heap_tree/node.rb b/lib/data_structures/heap_tree/node.rb index 2bcf555..e2b6821 100644 --- a/lib/data_structures/heap_tree/node.rb +++ b/lib/data_structures/heap_tree/node.rb @@ -1,9 +1,9 @@ module HeapTree - class Node - attr_accessor :left, :right, :value, :parent + class Node < BinaryNode + attr_accessor :parent def initialize(value = nil) - @value = value + super end end end From fa6588e7b0a334dcc076fc4bd8a696847b5f0765 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 20:25:06 -0300 Subject: [PATCH 05/19] refactor heap_spec.rb use HeapTree Node rather than the uncovered binary node. --- spec/data_structures/heap_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 935dacb..6878832 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -6,10 +6,10 @@ end it 'max heapify' do - node = Binary::Node.new(4) + node = HeapTree::Node.new(4) heap = described_class.new(node) - heap.head.left = Binary::Node.new(2) - heap.head.right = Binary::Node.new(8) + heap.head.left = HeapTree::Node.new(2) + heap.head.right = HeapTree::Node.new(8) heap.max_heapify From b29540bbb2c3d984de7a4c82a9c5b844e3eaf5b5 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 5 Oct 2025 21:30:25 -0300 Subject: [PATCH 06/19] Add max_heapofy to heap tree the simple case where there is just 3 nodes. --- .gitignore | 1 + lib/data_structures/heap.rb | 14 ++++++++++++++ spec/data_structures/heap_spec.rb | 10 ++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) 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/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 2dce7bb..802d849 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -3,4 +3,18 @@ def initialize(head = nil) @head = head super(@head) end + + def max_heapify(node = @head.left) + return if node.nil? + + current = node + parent = current.parent + max_value = current.value + if parent.value < current.value + parent_value = node.parent.value + value = current.value + parent.value = value + current.value = parent_value + end + end end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 6878832..38154f8 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -8,13 +8,15 @@ it 'max heapify' do node = HeapTree::Node.new(4) heap = described_class.new(node) - heap.head.left = HeapTree::Node.new(2) - heap.head.right = HeapTree::Node.new(8) + heap.head.right = HeapTree::Node.new(2) + heap.head.left = HeapTree::Node.new(8) + heap.head.left.parent = node + heap.head.right.parent = node heap.max_heapify expect(heap.head.value).to eq(8) - expect(heap.head.left.value).to eq(2) - expect(heap.head.right.value).to eq(4) + expect(heap.head.left.value).to eq(4) + expect(heap.head.right.value).to eq(2) end end From 11c8e5155bc9a50fc625d19f8f0774876468453e Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Mon, 6 Oct 2025 07:18:07 -0300 Subject: [PATCH 07/19] refactor heap test with context --- spec/data_structures/heap_spec.rb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 38154f8..fed3a95 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -1,22 +1,28 @@ require 'spec_helper' RSpec.describe Heap do + subject(:heap) { described_class.new(node) } + let(:node) { HeapTree::Node.new(4) } + it 'is truthy' do expect(described_class.new).to be_truthy end - it 'max heapify' do - node = HeapTree::Node.new(4) - heap = described_class.new(node) - heap.head.right = HeapTree::Node.new(2) - heap.head.left = HeapTree::Node.new(8) - heap.head.left.parent = node - heap.head.right.parent = node + context 'when max is 8 on left' do + before do + heap.head.right = HeapTree::Node.new(2) + heap.head.left = HeapTree::Node.new(8) + heap.head.left.parent = node + heap.head.right.parent = node + end + + it 'max heapify' do - heap.max_heapify + heap.max_heapify - expect(heap.head.value).to eq(8) - expect(heap.head.left.value).to eq(4) - expect(heap.head.right.value).to eq(2) + expect(heap.head.value).to eq(8) + expect(heap.head.left.value).to eq(4) + expect(heap.head.right.value).to eq(2) + end end end From 5499ba763cc3c4ed4616c2f83871f99731d20cab Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Mon, 6 Oct 2025 07:22:12 -0300 Subject: [PATCH 08/19] test when heap has max value on right --- spec/data_structures/heap_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index fed3a95..2d98e94 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -25,4 +25,21 @@ expect(heap.head.right.value).to eq(2) end end + + context 'when max is 16 on right' do + before do + heap.head.right = HeapTree::Node.new(16) + heap.head.left = HeapTree::Node.new(8) + heap.head.left.parent = node + heap.head.right.parent = node + end + + it 'max heapify' do + heap.max_heapify + + expect(heap.head.value).to eq(16) + expect(heap.head.left.value).to eq(4) + expect(heap.head.right.value).to eq(8) + end + end end From 6935f962bc3045731b6123384aebe5b79af85cca Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Tue, 7 Oct 2025 20:13:38 -0300 Subject: [PATCH 09/19] refactor max heapify The first interaction is based on head, not in left node from head. --- lib/data_structures/heap.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 802d849..1b7de8e 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -4,17 +4,17 @@ def initialize(head = nil) super(@head) end - def max_heapify(node = @head.left) + def max_heapify(node = @head) return if node.nil? - current = node - parent = current.parent - max_value = current.value - if parent.value < current.value - parent_value = node.parent.value - value = current.value + parent = node + max_value = parent.value + left_node = parent.left + if parent.value < left_node.value + parent_value = parent.value + value = left_node.value parent.value = value - current.value = parent_value + left_node.value = parent_value end end end From 48b24c11e8e606fb9c85e89943aae34e32854bd1 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Tue, 7 Oct 2025 20:24:50 -0300 Subject: [PATCH 10/19] max heapify for right node --- lib/data_structures/heap.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 1b7de8e..f95df7a 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -10,11 +10,20 @@ def max_heapify(node = @head) parent = node max_value = parent.value left_node = parent.left + right_node = parent.right if parent.value < left_node.value parent_value = parent.value value = left_node.value parent.value = value left_node.value = parent_value end + + if parent.value < right_node.value + parent_value = parent.value + value = right_node.value + parent.value = value + right_node.value = parent_value + end + end end From 635b2de9457f859e724fb65182374d405d34afae Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Tue, 7 Oct 2025 20:36:14 -0300 Subject: [PATCH 11/19] test for max heapify left and right heap --- spec/data_structures/heap_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 2d98e94..23322b2 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -42,4 +42,22 @@ expect(heap.head.right.value).to eq(8) end end + + context 'when the initial head value is less then left and right side' do + before do + heap.head = HeapTree::Node.new(2) + heap.head.right = HeapTree::Node.new(16) + heap.head.left = HeapTree::Node.new(8) + heap.head.left.parent = node + heap.head.right.parent = node + end + + it 'max heapify' do + heap.max_heapify + + expect(heap.head.value).to eq(16) + expect(heap.head.left.value).to eq(2) + expect(heap.head.right.value).to eq(8) + end + end end From 7aa0dff682064c54a581fd9eafd4ae3b5fd3c0a0 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Tue, 7 Oct 2025 21:03:01 -0300 Subject: [PATCH 12/19] partially the left side works for heapify --- lib/data_structures/heap.rb | 2 +- spec/data_structures/heap_spec.rb | 42 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index f95df7a..c123e2b 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -5,7 +5,6 @@ def initialize(head = nil) end def max_heapify(node = @head) - return if node.nil? parent = node max_value = parent.value @@ -25,5 +24,6 @@ def max_heapify(node = @head) right_node.value = parent_value end + max_heapify(parent.left) end end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 23322b2..4a7a406 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -60,4 +60,46 @@ expect(heap.head.right.value).to eq(8) end end + + context 'when heap is deep 4 levels' do + before do + heap.head = HeapTree::Node.new(16) + heap.head.right = HeapTree::Node.new(10) + heap.head.left = HeapTree::Node.new(4) + heap.head.left.parent = node + heap.head.right.parent = node + heap.head.left.left = HeapTree::Node.new(14) + heap.head.left.right = HeapTree::Node.new(7) + heap.head.left.left.parent = heap.head.left + heap.head.left.right.parent = heap.head.left + + heap.head.left.left.left = HeapTree::Node.new(2) + heap.head.left.left.left.parent = heap.head.left.left + + heap.head.left.left.right = HeapTree::Node.new(8) + heap.head.left.left.right.parent = heap.head.left.left + + heap.head.left.right = HeapTree::Node.new(7) + heap.head.left.right.parent = heap.head.left + + heap.head.left.right.left = HeapTree::Node.new(1) + heap.head.left.right.left.parent = heap.head.left.right + + # right side + heap.head.right.left = HeapTree::Node.new(9) + heap.head.right.left.parent = heap.head.right + + heap.head.right.right = HeapTree::Node.new(3) + heap.head.right.right.parent = heap.head.right + end + + it 'max heapify' do + heap.max_heapify + + expect(heap.head.value).to eq(16) + expect(heap.head.left.value).to eq(14) + expect(heap.head.right.value).to eq(10) + expect(heap.head.left.left.value).to eq(8) + end + end end From 6c7f075bf0e97a9267e95d26ccf3fb031173a9d8 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Tue, 7 Oct 2025 21:03:45 -0300 Subject: [PATCH 13/19] fix undefined value in heapify --- lib/data_structures/heap.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index c123e2b..9fd9fa5 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -5,6 +5,7 @@ def initialize(head = nil) end def max_heapify(node = @head) + return if node.left.nil? parent = node max_value = parent.value From 1bdc124db1d0baea041388ca779afdc677fd9a5d Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 11:21:18 -0300 Subject: [PATCH 14/19] implement heap as wraping it in an array --- lib/data_structures/heap.rb | 36 +++----- spec/data_structures/heap_spec.rb | 132 +++++++++++------------------- 2 files changed, 63 insertions(+), 105 deletions(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 9fd9fa5..6252b57 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -1,30 +1,20 @@ -class Heap < BTree - def initialize(head = nil) - @head = head - super(@head) +class Heap + attr_reader :list + + def initialize(list) + @list = list end - def max_heapify(node = @head) - return if node.left.nil? + def index_of_parent(index) + parent_index = (index - 1) / 2 + return if parent_index.negative? - parent = node - max_value = parent.value - left_node = parent.left - right_node = parent.right - if parent.value < left_node.value - parent_value = parent.value - value = left_node.value - parent.value = value - left_node.value = parent_value - end + parent_index + end - if parent.value < right_node.value - parent_value = parent.value - value = right_node.value - parent.value = value - right_node.value = parent_value - end + def value_of_parent(index) + return if index_of_parent(index).nil? - max_heapify(parent.left) + @list[index_of_parent(index)] end end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index 4a7a406..cd5f065 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -1,105 +1,73 @@ require 'spec_helper' RSpec.describe Heap do - subject(:heap) { described_class.new(node) } - let(:node) { HeapTree::Node.new(4) } + subject(:heap) { described_class.new(list) } + let(:list) { [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] } - it 'is truthy' do - expect(described_class.new).to be_truthy - end - - context 'when max is 8 on left' do - before do - heap.head.right = HeapTree::Node.new(2) - heap.head.left = HeapTree::Node.new(8) - heap.head.left.parent = node - heap.head.right.parent = node - end - - it 'max heapify' do - - heap.max_heapify + describe "#list" do - expect(heap.head.value).to eq(8) - expect(heap.head.left.value).to eq(4) - expect(heap.head.right.value).to eq(2) + it 'get the list' do + expect(heap.list).to eq(list) end end - context 'when max is 16 on right' do - before do - heap.head.right = HeapTree::Node.new(16) - heap.head.left = HeapTree::Node.new(8) - heap.head.left.parent = node - heap.head.right.parent = node + 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 - it 'max heapify' do - heap.max_heapify - - expect(heap.head.value).to eq(16) - expect(heap.head.left.value).to eq(4) - expect(heap.head.right.value).to eq(8) + 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 - context 'when the initial head value is less then left and right side' do - before do - heap.head = HeapTree::Node.new(2) - heap.head.right = HeapTree::Node.new(16) - heap.head.left = HeapTree::Node.new(8) - heap.head.left.parent = node - heap.head.right.parent = node + describe "#index_of_left_child" do + it 'get index_of_left_child' do + expect(heap.index_of_left_child(0)).to be_one + 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 - it 'max heapify' do - heap.max_heapify - - expect(heap.head.value).to eq(16) - expect(heap.head.left.value).to eq(2) - expect(heap.head.right.value).to eq(8) + 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 - context 'when heap is deep 4 levels' do - before do - heap.head = HeapTree::Node.new(16) - heap.head.right = HeapTree::Node.new(10) - heap.head.left = HeapTree::Node.new(4) - heap.head.left.parent = node - heap.head.right.parent = node - heap.head.left.left = HeapTree::Node.new(14) - heap.head.left.right = HeapTree::Node.new(7) - heap.head.left.left.parent = heap.head.left - heap.head.left.right.parent = heap.head.left - - heap.head.left.left.left = HeapTree::Node.new(2) - heap.head.left.left.left.parent = heap.head.left.left - - heap.head.left.left.right = HeapTree::Node.new(8) - heap.head.left.left.right.parent = heap.head.left.left - - heap.head.left.right = HeapTree::Node.new(7) - heap.head.left.right.parent = heap.head.left - - heap.head.left.right.left = HeapTree::Node.new(1) - heap.head.left.right.left.parent = heap.head.left.right - - # right side - heap.head.right.left = HeapTree::Node.new(9) - heap.head.right.left.parent = heap.head.right - - heap.head.right.right = HeapTree::Node.new(3) - heap.head.right.right.parent = heap.head.right + 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 - it 'max heapify' do - heap.max_heapify - - expect(heap.head.value).to eq(16) - expect(heap.head.left.value).to eq(14) - expect(heap.head.right.value).to eq(10) - expect(heap.head.left.left.value).to eq(8) + 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 From 80fcdd31ed9859e7416565016c230e41e4b4b0a9 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 13:31:28 -0300 Subject: [PATCH 15/19] add #index_of_left_child --- lib/data_structures/heap.rb | 7 +++++++ spec/data_structures/heap_spec.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 6252b57..8c0aac0 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -17,4 +17,11 @@ def value_of_parent(index) @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 end diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index cd5f065..b3a5db2 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -33,7 +33,7 @@ describe "#index_of_left_child" do it 'get index_of_left_child' do - expect(heap.index_of_left_child(0)).to be_one + 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) From cb0394d5f786abfe7847f84bcd6adf1ce296b3e5 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 13:35:32 -0300 Subject: [PATCH 16/19] add #value_of_left_child --- lib/data_structures/heap.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 8c0aac0..2a6a4a1 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -24,4 +24,11 @@ def index_of_left_child(index) 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[index_of_left_child(index)] + end end From 8d5feae3c2f99b467772398c43ab766c1d323bc2 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 13:45:53 -0300 Subject: [PATCH 17/19] refactor left child --- lib/data_structures/heap.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 2a6a4a1..bfe3cf9 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -29,6 +29,6 @@ def value_of_left_child(index) left_child_index = index_of_left_child(index) return if left_child_index.nil? - list[index_of_left_child(index)] + list[left_child_index] end end From 1379f3ddea101809f8addf8700e68648d4862518 Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 13:46:13 -0300 Subject: [PATCH 18/19] add #value_of_left_child --- lib/data_structures/heap.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index bfe3cf9..6c755f4 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -31,4 +31,18 @@ def value_of_left_child(index) 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 From c5ff89e5c928e2daab93f525ef36f98d8b6d72bb Mon Sep 17 00:00:00 2001 From: Pauloparakleto Date: Sun, 14 Dec 2025 13:47:37 -0300 Subject: [PATCH 19/19] private heap list --- lib/data_structures/heap.rb | 2 +- spec/data_structures/heap_spec.rb | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/data_structures/heap.rb b/lib/data_structures/heap.rb index 6c755f4..c33f83f 100644 --- a/lib/data_structures/heap.rb +++ b/lib/data_structures/heap.rb @@ -1,5 +1,5 @@ class Heap - attr_reader :list + private attr_reader :list def initialize(list) @list = list diff --git a/spec/data_structures/heap_spec.rb b/spec/data_structures/heap_spec.rb index b3a5db2..aa85b02 100644 --- a/spec/data_structures/heap_spec.rb +++ b/spec/data_structures/heap_spec.rb @@ -4,13 +4,6 @@ subject(:heap) { described_class.new(list) } let(:list) { [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] } - describe "#list" do - - it 'get the list' do - expect(heap.list).to eq(list) - end - end - describe "#index_of_parent" do it 'get parent' do expect(heap.index_of_parent(0)).to be_nil