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 +4 -4
- data/lib/Heap.rb +2 -1
- data/lib/Heap/binary_heap/binary_heap_max.rb +85 -0
- data/lib/Heap/binary_heap/{binary_heap.rb → binary_heap_min.rb} +0 -0
- data/lib/Heap/version.rb +1 -1
- data/ruby-heap-0.1.0.gem +0 -0
- metadata +5 -4
- data/ruby-heap-0.0.5.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67185670438a9077e5eec874db91d471ce687900
|
4
|
+
data.tar.gz: efbfb03e0365fbff555a2e94b7d2c6873a6d3515
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 842e05fb7274881285c6a090c1e4abf03275c78544d9d52515d13e0a6e7eddc8b0d9622228b9f184a136668aaff5e8b14196912554979ef0f9579d09946b7ad1
|
7
|
+
data.tar.gz: 11d8a860de8756728694120d5d3bdf53c232764656505a929a7f74514b09c879242733fcd534ef2cd709cd3e7fd1809f8a01ee5614d4e6d1fb1515896b5ebafb
|
data/lib/Heap.rb
CHANGED
@@ -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
|
File without changes
|
data/lib/Heap/version.rb
CHANGED
data/ruby-heap-0.1.0.gem
ADDED
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.
|
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-
|
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/
|
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.
|
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
|