ruby-heap 0.1.0 → 0.2.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: b49b7d675703a399ead164dfd7cd837975253c61
4
- data.tar.gz: ac4a978badfa483cadb5445bdf308283af462ee7
3
+ metadata.gz: 67185670438a9077e5eec874db91d471ce687900
4
+ data.tar.gz: efbfb03e0365fbff555a2e94b7d2c6873a6d3515
5
5
  SHA512:
6
- metadata.gz: 604bab2bb181daf0cbc411c5fae7788ab5424bd517497f1f6bb1b80bea7473eafe72f0ddfa4d0dc56cbc6ea7feecb73eedede1a12e4c0b6f488ca6e3057ee7bd
7
- data.tar.gz: 0e725440379858b4f3dbe03c0686b36c800775ab3ad5a33726edff9aa6ef30fb316c1f4aaacf11c649a5dca78a06c9cf07f84143801dece3a2064e1a8eb98b9e
6
+ metadata.gz: 842e05fb7274881285c6a090c1e4abf03275c78544d9d52515d13e0a6e7eddc8b0d9622228b9f184a136668aaff5e8b14196912554979ef0f9579d09946b7ad1
7
+ data.tar.gz: 11d8a860de8756728694120d5d3bdf53c232764656505a929a7f74514b09c879242733fcd534ef2cd709cd3e7fd1809f8a01ee5614d4e6d1fb1515896b5ebafb
data/lib/Heap.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'Heap/version'
2
2
 
3
3
  module Heap
4
- require 'Heap/binary_heap/binary_heap'
4
+ require 'Heap/binary_heap/binary_heap_min'
5
+ require 'Heap/binary_heap/binary_heap_max'
5
6
  end
@@ -0,0 +1,85 @@
1
+ module Heap
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
+ @elements.push element
13
+ swim_up(count)
14
+ end
15
+
16
+ def count
17
+ @elements.length
18
+ end
19
+
20
+ def extract_max
21
+ @elements[0]
22
+ end
23
+
24
+ def extract_max!
25
+ swap(1, count)
26
+ el = @elements.pop
27
+ swim_down(1)
28
+ el
29
+ end
30
+
31
+ def sort
32
+ el_temp = @elements.clone
33
+ result = []
34
+ result.push extract_max! while count > 0
35
+ @elements = el_temp
36
+ result
37
+ end
38
+
39
+ private
40
+
41
+ def swap(index1, index2)
42
+ temp = @elements[index1 - 1]
43
+ @elements[index1 - 1] = @elements[index2 - 1]
44
+ @elements[index2 - 1] = temp
45
+ end
46
+
47
+ def swim_up(index)
48
+ return if index == 1
49
+ parent_index = (index / 2).floor
50
+ return if @elements[parent_index - 1] >= @elements[index - 1]
51
+ swap(parent_index, index)
52
+ swim_up parent_index
53
+ end
54
+
55
+ def swim_down(index)
56
+ child1_index = 2 * index
57
+ child2_index = 2 * index + 1
58
+ return if @elements[child1_index - 1].nil? && @elements[child2_index - 1].nil?
59
+ if @elements[child2_index - 1].nil?
60
+ return if @elements[child1_index - 1] <= @elements[index - 1]
61
+ swap(index, child1_index)
62
+ swim_down(child1_index)
63
+ else
64
+ if @elements[child2_index - 1] <= @elements[index - 1] && @elements[child1_index - 1] <= @elements[index - 1]
65
+ return
66
+ elsif @elements[child2_index - 1] > @elements[index - 1] && @elements[child1_index - 1] > @elements[index - 1]
67
+ if @elements[child2_index - 1] > @elements[child1_index - 1]
68
+ swap(child2_index, index)
69
+ swim_down(child2_index)
70
+ else
71
+ swap(child1_index, index)
72
+ swim_down(child1_index)
73
+ end
74
+ elsif @elements[child2_index - 1] > @elements[index - 1]
75
+ swap(child2_index, index)
76
+ swim_down(child2_index)
77
+ elsif @elements[child1_index - 1] > @elements[index - 1]
78
+ swap(child1_index, index)
79
+ swim_down(child1_index)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
data/lib/Heap/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Heap
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
Binary file
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.1.0
4
+ version: 0.2.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-07 00:00:00.000000000 Z
11
+ date: 2017-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,9 +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
+ - lib/Heap/binary_heap/binary_heap_max.rb
73
+ - lib/Heap/binary_heap/binary_heap_min.rb
73
74
  - lib/Heap/version.rb
74
- - ruby-heap-0.0.5.gem
75
+ - ruby-heap-0.1.0.gem
75
76
  homepage: https://github.com/pups3s/ruby-heap
76
77
  licenses:
77
78
  - MIT
data/ruby-heap-0.0.5.gem DELETED
Binary file