ruby-heap 0.2.2 → 1.0.0

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: c9974abcd60bd8451a0b255d5a92a5662f017643
4
- data.tar.gz: 619d3a2efe5beb55f18451dfef513a55deb10255
3
+ metadata.gz: bc4d7d961145b02d3388febd7dbfbb96eef78302
4
+ data.tar.gz: 1acbb9e94be684a8a6f04afa112d1662a7aea41c
5
5
  SHA512:
6
- metadata.gz: 0a49cc38bbdcb3d910ec6a80487bd6ff3fdbc7f9a5171d1a19e50db844702fa31b1320d78ab07f3496d0352c30ffe88f99829f655b01d24fefc57675b5a9e7e5
7
- data.tar.gz: 3eb7d687cad878bf3d38bc7e373d21e8d6d132b5fa6604a2254aebc394874c4c48b1c554e2c78d6f590ed63f8e87d925d5022557448b345124f045c7d5311a1a
6
+ metadata.gz: 5207179c28f8677196aefa822c6a57cfeac7fe1b56e5d25605a4cd119a4d8d34a7a350686362051759e362943c9eb1e5bf5446e94351cec256e5b56d669006d8
7
+ data.tar.gz: 1b5399aea603ec9f097202686795696909fe2ff7e8f11e1f6310a69b6207afd5340919222b2e6794d37477c2d633a3ad54881484d8939921f2c374e38067ea9d
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Gem Version](https://badge.fury.io/rb/ruby-heap.svg)](https://badge.fury.io/rb/ruby-heap)
1
2
  # Heap (ruby heapsort)
2
3
 
3
4
  Gem is using for making Heaps (binary only for now).
@@ -20,6 +21,8 @@ Or install it yourself as:
20
21
 
21
22
  ## Usage
22
23
 
24
+ ### Binary Heaps
25
+
23
26
  #### Binary Heap with min root
24
27
  While Heap initialize you can add any comparable object in it (not numbers only).
25
28
  Objects must have compare functions (>, >=, <, <=).
@@ -99,6 +102,21 @@ max_heap = Heap::BinaryHeap::MaxHeap.new [9, -1, 4]
99
102
  # Merge heaps
100
103
  min_heap.add max_heap
101
104
 
105
+ min_heap.count # 6
106
+ min_heap.sort # [-1, 1, 2, 3, 4, 9]
107
+ ```
108
+
109
+ ### Multiple Heaps
110
+
111
+ Multiple (d-ary) heaps have **same methods as binary**. But initialize differs:
112
+
113
+ ```ruby
114
+ require 'Heap'
115
+
116
+ # First param is "d" of heap
117
+ # Second param is optional and can contain first elements
118
+ min_heap = Heap::MultipleHeap::MinHeap.new(5, [10, 20, 30])
119
+ max_heap = Heap::MultipleHeap::MaxHeap.new(7)
102
120
  ```
103
121
 
104
122
  ## Development
@@ -3,4 +3,6 @@ require 'Heap/version'
3
3
  module Heap
4
4
  require 'Heap/binary_heap/binary_heap_min'
5
5
  require 'Heap/binary_heap/binary_heap_max'
6
+ require 'Heap/multiple_heap/multiple_heap_max'
7
+ require 'Heap/multiple_heap/multiple_heap_min'
6
8
  end
@@ -55,7 +55,7 @@ module Heap
55
55
 
56
56
  def swim_up(index)
57
57
  return if index == 1
58
- parent_index = (index / 2).floor
58
+ parent_index = index / 2
59
59
  return if @elements[parent_index - 1] >= @elements[index - 1]
60
60
  swap(parent_index, index)
61
61
  swim_up parent_index
@@ -55,7 +55,7 @@ module Heap
55
55
 
56
56
  def swim_up(index)
57
57
  return if index == 1
58
- parent_index = (index / 2).floor
58
+ parent_index = index / 2
59
59
  return if @elements[parent_index - 1] <= @elements[index - 1]
60
60
  swap(parent_index, index)
61
61
  swim_up parent_index
@@ -0,0 +1,82 @@
1
+ module Heap
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
+
31
+ def extract_max
32
+ @elements[0]
33
+ end
34
+
35
+ def extract_max!
36
+ swap(1, count)
37
+ el = @elements.pop
38
+ swim_down(1)
39
+ el
40
+ end
41
+
42
+ def sort
43
+ el_temp = @elements.clone
44
+ result = []
45
+ result.push extract_max! while count > 0
46
+ @elements = el_temp
47
+ result
48
+ end
49
+
50
+ private
51
+
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
+ def swim_up(index)
59
+ return if index == 1
60
+ parent_index = ((index.to_f - 1) / @d).ceil
61
+ return if @elements[parent_index - 1] >= @elements[index - 1]
62
+ swap(parent_index, index)
63
+ swim_up parent_index
64
+ end
65
+
66
+ 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 }
74
+ max_child = children.max
75
+
76
+ return if @elements[index - 1] >= max_child[0]
77
+ swap index, max_child[1]
78
+ swim_down max_child[1]
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,82 @@
1
+ module Heap
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
+
31
+ def extract_min
32
+ @elements[0]
33
+ end
34
+
35
+ def extract_min!
36
+ swap(1, count)
37
+ el = @elements.pop
38
+ swim_down(1)
39
+ el
40
+ end
41
+
42
+ def sort
43
+ el_temp = @elements.clone
44
+ result = []
45
+ result.push extract_min! while count > 0
46
+ @elements = el_temp
47
+ result
48
+ end
49
+
50
+ private
51
+
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
+ def swim_up(index)
59
+ return if index == 1
60
+ parent_index = ((index.to_f - 1) / @d).ceil
61
+ return if @elements[parent_index - 1] <= @elements[index - 1]
62
+ swap(parent_index, index)
63
+ swim_up parent_index
64
+ end
65
+
66
+ 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 }
74
+ min_child = children.min
75
+
76
+ return if @elements[index - 1] <= min_child[0]
77
+ swap index, min_child[1]
78
+ swim_down min_child[1]
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,3 +1,3 @@
1
1
  module Heap
2
- VERSION = '0.2.2'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-heap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Sysoev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-19 00:00:00.000000000 Z
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,8 +71,9 @@ files:
71
71
  - lib/Heap.rb
72
72
  - lib/Heap/binary_heap/binary_heap_max.rb
73
73
  - lib/Heap/binary_heap/binary_heap_min.rb
74
+ - lib/Heap/multiple_heap/multiple_heap_max.rb
75
+ - lib/Heap/multiple_heap/multiple_heap_min.rb
74
76
  - lib/Heap/version.rb
75
- - ruby-heap-0.2.1.gem
76
77
  homepage: https://github.com/pups3s/ruby-heap
77
78
  licenses:
78
79
  - MIT
Binary file