ruby-heap 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc4d7d961145b02d3388febd7dbfbb96eef78302
4
- data.tar.gz: 1acbb9e94be684a8a6f04afa112d1662a7aea41c
3
+ metadata.gz: 81514e6e431e57dad5a43540277e8d84a7b25df4
4
+ data.tar.gz: 6ed5ed8d1cc587f884238472e00e73596acc2afb
5
5
  SHA512:
6
- metadata.gz: 5207179c28f8677196aefa822c6a57cfeac7fe1b56e5d25605a4cd119a4d8d34a7a350686362051759e362943c9eb1e5bf5446e94351cec256e5b56d669006d8
7
- data.tar.gz: 1b5399aea603ec9f097202686795696909fe2ff7e8f11e1f6310a69b6207afd5340919222b2e6794d37477c2d633a3ad54881484d8939921f2c374e38067ea9d
6
+ metadata.gz: 95450d6ead205907ec28748d29fa2175273a616de38d48bca30d6ada6eedb61024c9893349b9318a73945bbf6d4b829c84fc00ad23dca0215b68ea71373e313e
7
+ data.tar.gz: 9a03daf237471d5a954126335c6039db118cfa87ad690f12dd4068556d981bf8cfb20f3e624a0b9ef39a01d168119d09ede11585fa801cd729767e56693f246d
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/ruby-heap.svg)](https://badge.fury.io/rb/ruby-heap)
2
+ [![Code Climate](https://codeclimate.com/github/pups3s/ruby-heap/badges/gpa.svg)](https://codeclimate.com/github/pups3s/ruby-heap)
2
3
  # Heap (ruby heapsort)
3
4
 
4
5
  Gem is using for making Heaps (binary only for now).
data/lib/Heap.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'Heap/version'
2
2
 
3
+ # Main ruby-heap rb file
3
4
  module Heap
5
+ require 'Heap/binary_heap/binary_heap'
4
6
  require 'Heap/binary_heap/binary_heap_min'
5
7
  require 'Heap/binary_heap/binary_heap_max'
8
+ require 'Heap/multiple_heap/multiple_heap'
6
9
  require 'Heap/multiple_heap/multiple_heap_max'
7
10
  require 'Heap/multiple_heap/multiple_heap_min'
8
11
  end
@@ -0,0 +1,48 @@
1
+ module Heap
2
+ module BinaryHeap
3
+ # Binary Heap template
4
+ class BinaryHeap
5
+ attr_reader :elements
6
+
7
+ def initialize(elements = [])
8
+ @elements = []
9
+ add(elements.pop) until elements.empty?
10
+ end
11
+
12
+ def add(element)
13
+ if element.is_a? Array
14
+ element.each do |el|
15
+ @elements.push el
16
+ swim_up(count)
17
+ end
18
+ elsif defined? element.elements
19
+ add element.elements
20
+ else
21
+ @elements.push element
22
+ swim_up(count)
23
+ end
24
+ end
25
+
26
+ def count
27
+ @elements.length
28
+ end
29
+
30
+ protected
31
+
32
+ def swap(index1, index2)
33
+ temp = @elements[index1 - 1]
34
+ @elements[index1 - 1] = @elements[index2 - 1]
35
+ @elements[index2 - 1] = temp
36
+ end
37
+
38
+ def get_children(index)
39
+ child1 = 2 * index
40
+ child2 = 2 * index + 1
41
+ children = {}
42
+ children[@elements[child1 - 1]] = child1 unless @elements[child1 - 1].nil?
43
+ children[@elements[child2 - 1]] = child2 unless @elements[child2 - 1].nil?
44
+ children
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,31 +1,7 @@
1
1
  module Heap
2
2
  module BinaryHeap
3
- class MaxHeap
4
- attr_reader :elements
5
-
6
- def initialize(elements = [])
7
- @elements = []
8
- add(elements.pop) until elements.empty?
9
- end
10
-
11
- def add(element)
12
- if element.is_a? Array
13
- element.each do |el|
14
- @elements.push el
15
- swim_up(count)
16
- end
17
- elsif defined? element.elements
18
- add element.elements
19
- else
20
- @elements.push element
21
- swim_up(count)
22
- end
23
- end
24
-
25
- def count
26
- @elements.length
27
- end
28
-
3
+ # Binary Heap with max root
4
+ class MaxHeap < BinaryHeap
29
5
  def extract_max
30
6
  @elements[0]
31
7
  end
@@ -47,12 +23,6 @@ module Heap
47
23
 
48
24
  private
49
25
 
50
- def swap(index1, index2)
51
- temp = @elements[index1 - 1]
52
- @elements[index1 - 1] = @elements[index2 - 1]
53
- @elements[index2 - 1] = temp
54
- end
55
-
56
26
  def swim_up(index)
57
27
  return if index == 1
58
28
  parent_index = index / 2
@@ -62,32 +32,12 @@ module Heap
62
32
  end
63
33
 
64
34
  def swim_down(index)
65
- child1_index = 2 * index
66
- child2_index = 2 * index + 1
67
- return if @elements[child1_index - 1].nil? && @elements[child2_index - 1].nil?
68
- if @elements[child2_index - 1].nil?
69
- return if @elements[child1_index - 1] <= @elements[index - 1]
70
- swap(index, child1_index)
71
- swim_down(child1_index)
72
- else
73
- if @elements[child2_index - 1] <= @elements[index - 1] && @elements[child1_index - 1] <= @elements[index - 1]
74
- return
75
- elsif @elements[child2_index - 1] > @elements[index - 1] && @elements[child1_index - 1] > @elements[index - 1]
76
- if @elements[child2_index - 1] > @elements[child1_index - 1]
77
- swap(child2_index, index)
78
- swim_down(child2_index)
79
- else
80
- swap(child1_index, index)
81
- swim_down(child1_index)
82
- end
83
- elsif @elements[child2_index - 1] > @elements[index - 1]
84
- swap(child2_index, index)
85
- swim_down(child2_index)
86
- elsif @elements[child1_index - 1] > @elements[index - 1]
87
- swap(child1_index, index)
88
- swim_down(child1_index)
89
- end
90
- end
35
+ children = get_children(index)
36
+ return if children.empty?
37
+ max_child = children.max
38
+ return if @elements[index - 1] >= max_child[0]
39
+ swap index, max_child[1]
40
+ swim_down max_child[1]
91
41
  end
92
42
  end
93
43
  end
@@ -1,31 +1,7 @@
1
1
  module Heap
2
2
  module BinaryHeap
3
- class MinHeap
4
- attr_reader :elements
5
-
6
- def initialize(elements = [])
7
- @elements = []
8
- add(elements.pop) until elements.empty?
9
- end
10
-
11
- def add(element)
12
- if element.is_a? Array
13
- element.each do |el|
14
- @elements.push el
15
- swim_up(count)
16
- end
17
- elsif defined? element.elements
18
- add element.elements
19
- else
20
- @elements.push element
21
- swim_up(count)
22
- end
23
- end
24
-
25
- def count
26
- @elements.length
27
- end
28
-
3
+ # Binary Heap with min root
4
+ class MinHeap < BinaryHeap
29
5
  def extract_min
30
6
  @elements[0]
31
7
  end
@@ -47,12 +23,6 @@ module Heap
47
23
 
48
24
  private
49
25
 
50
- def swap(index1, index2)
51
- temp = @elements[index1 - 1]
52
- @elements[index1 - 1] = @elements[index2 - 1]
53
- @elements[index2 - 1] = temp
54
- end
55
-
56
26
  def swim_up(index)
57
27
  return if index == 1
58
28
  parent_index = index / 2
@@ -62,32 +32,12 @@ module Heap
62
32
  end
63
33
 
64
34
  def swim_down(index)
65
- child1_index = 2 * index
66
- child2_index = 2 * index + 1
67
- return if @elements[child1_index - 1].nil? && @elements[child2_index - 1].nil?
68
- if @elements[child2_index - 1].nil?
69
- return if @elements[child1_index - 1] >= @elements[index - 1]
70
- swap(index, child1_index)
71
- swim_down(child1_index)
72
- else
73
- if @elements[child2_index - 1] >= @elements[index - 1] && @elements[child1_index - 1] >= @elements[index - 1]
74
- return
75
- elsif @elements[child2_index - 1] < @elements[index - 1] && @elements[child1_index - 1] < @elements[index - 1]
76
- if @elements[child2_index - 1] < @elements[child1_index - 1]
77
- swap(child2_index, index)
78
- swim_down(child2_index)
79
- else
80
- swap(child1_index, index)
81
- swim_down(child1_index)
82
- end
83
- elsif @elements[child2_index - 1] < @elements[index - 1]
84
- swap(child2_index, index)
85
- swim_down(child2_index)
86
- elsif @elements[child1_index - 1] < @elements[index - 1]
87
- swap(child1_index, index)
88
- swim_down(child1_index)
89
- end
90
- end
35
+ children = get_children(index)
36
+ return if children.empty?
37
+ max_child = children.min
38
+ return if @elements[index - 1] <= max_child[0]
39
+ swap index, max_child[1]
40
+ swim_down max_child[1]
91
41
  end
92
42
  end
93
43
  end
@@ -0,0 +1,52 @@
1
+ module Heap
2
+ module MultipleHeap
3
+ # Multiple Heap template
4
+ class MultipleHeap
5
+ attr_reader :elements
6
+ attr_reader :d
7
+
8
+ def initialize(d, elements = [])
9
+ @elements = []
10
+ @d = d
11
+ add(elements.pop) until elements.empty?
12
+ end
13
+
14
+ def add(element)
15
+ if element.is_a? Array
16
+ element.each do |el|
17
+ @elements.push el
18
+ swim_up(count)
19
+ end
20
+ elsif defined? element.elements
21
+ add element.elements
22
+ else
23
+ @elements.push element
24
+ swim_up(count)
25
+ end
26
+ end
27
+
28
+ def count
29
+ @elements.length
30
+ end
31
+
32
+ protected
33
+
34
+ def swap(index1, index2)
35
+ temp = @elements[index1 - 1]
36
+ @elements[index1 - 1] = @elements[index2 - 1]
37
+ @elements[index2 - 1] = temp
38
+ end
39
+
40
+ def get_children(index)
41
+ child_indexes = []
42
+ (2..(@d + 1)).each { |i| child_indexes.push((index - 1) * @d + i) }
43
+ child_indexes.delete_if { |ind| ind > count }
44
+ return if child_indexes.empty?
45
+
46
+ children = {}
47
+ child_indexes.each { |ind| children[@elements[ind - 1]] = ind }
48
+ children
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,33 +1,7 @@
1
1
  module Heap
2
2
  module MultipleHeap
3
- class MaxHeap
4
- attr_reader :elements
5
- attr_reader :d
6
-
7
- def initialize(d, elements = [])
8
- @elements = []
9
- @d = d
10
- add(elements.pop) until elements.empty?
11
- end
12
-
13
- def add(element)
14
- if element.is_a? Array
15
- element.each do |el|
16
- @elements.push el
17
- swim_up(count)
18
- end
19
- elsif defined? element.elements
20
- add element.elements
21
- else
22
- @elements.push element
23
- swim_up(count)
24
- end
25
- end
26
-
27
- def count
28
- @elements.length
29
- end
30
-
3
+ # Multiple Heap with max root
4
+ class MaxHeap < MultipleHeap
31
5
  def extract_max
32
6
  @elements[0]
33
7
  end
@@ -49,12 +23,6 @@ module Heap
49
23
 
50
24
  private
51
25
 
52
- def swap(index1, index2)
53
- temp = @elements[index1 - 1]
54
- @elements[index1 - 1] = @elements[index2 - 1]
55
- @elements[index2 - 1] = temp
56
- end
57
-
58
26
  def swim_up(index)
59
27
  return if index == 1
60
28
  parent_index = ((index.to_f - 1) / @d).ceil
@@ -64,13 +32,8 @@ module Heap
64
32
  end
65
33
 
66
34
  def swim_down(index)
67
- child_indexes = []
68
- (2..(@d + 1)).each { |i| child_indexes.push((index - 1) * @d + i) }
69
- child_indexes.delete_if { |ind| ind > count }
70
- return if child_indexes.empty?
71
-
72
- children = {}
73
- child_indexes.each { |ind| children[@elements[ind - 1]] = ind }
35
+ children = get_children index
36
+ return if children.nil? || children.empty?
74
37
  max_child = children.max
75
38
 
76
39
  return if @elements[index - 1] >= max_child[0]
@@ -1,33 +1,7 @@
1
1
  module Heap
2
2
  module MultipleHeap
3
- class MinHeap
4
- attr_reader :elements
5
- attr_reader :d
6
-
7
- def initialize(d, elements = [])
8
- @elements = []
9
- @d = d
10
- add(elements.pop) until elements.empty?
11
- end
12
-
13
- def add(element)
14
- if element.is_a? Array
15
- element.each do |el|
16
- @elements.push el
17
- swim_up(count)
18
- end
19
- elsif defined? element.elements
20
- add element.elements
21
- else
22
- @elements.push element
23
- swim_up(count)
24
- end
25
- end
26
-
27
- def count
28
- @elements.length
29
- end
30
-
3
+ # Multiple Heap with min root
4
+ class MinHeap < MultipleHeap
31
5
  def extract_min
32
6
  @elements[0]
33
7
  end
@@ -49,12 +23,6 @@ module Heap
49
23
 
50
24
  private
51
25
 
52
- def swap(index1, index2)
53
- temp = @elements[index1 - 1]
54
- @elements[index1 - 1] = @elements[index2 - 1]
55
- @elements[index2 - 1] = temp
56
- end
57
-
58
26
  def swim_up(index)
59
27
  return if index == 1
60
28
  parent_index = ((index.to_f - 1) / @d).ceil
@@ -64,13 +32,8 @@ module Heap
64
32
  end
65
33
 
66
34
  def swim_down(index)
67
- child_indexes = []
68
- (2..(@d + 1)).each { |i| child_indexes.push((index - 1) * @d + i) }
69
- child_indexes.delete_if { |ind| ind > count }
70
- return if child_indexes.empty?
71
-
72
- children = {}
73
- child_indexes.each { |ind| children[@elements[ind - 1]] = ind }
35
+ children = get_children index
36
+ return if children.nil? || children.empty?
74
37
  min_child = children.min
75
38
 
76
39
  return if @elements[index - 1] <= min_child[0]
data/lib/Heap/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Heap
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-heap
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Sysoev
@@ -69,8 +69,10 @@ files:
69
69
  - bin/console
70
70
  - bin/setup
71
71
  - lib/Heap.rb
72
+ - lib/Heap/binary_heap/binary_heap.rb
72
73
  - lib/Heap/binary_heap/binary_heap_max.rb
73
74
  - lib/Heap/binary_heap/binary_heap_min.rb
75
+ - lib/Heap/multiple_heap/multiple_heap.rb
74
76
  - lib/Heap/multiple_heap/multiple_heap_max.rb
75
77
  - lib/Heap/multiple_heap/multiple_heap_min.rb
76
78
  - lib/Heap/version.rb