data_structures_101 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/data_structures_101/heap.rb +36 -2
- data/lib/data_structures_101/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b165126a0a9d3d817393b985bbf32648adeebd9984ad51e82235859b38f19db
|
4
|
+
data.tar.gz: cb9311135096e0cbe4912cec09fae930b513f49044ad1e964c48e2f312f63581
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20288907bd1bc5ebde1eb98d6cd384b2a1a363b570988bc6ab81530406d7ec9a88a5e9931079d1f362bda7ab8381f5dda7781bf84883640f32e1f63532995709
|
7
|
+
data.tar.gz: 70895ffddb44aa604877bb910d97657b47c501de791239ed0c663e7c8812278b5f761476e521ca8f7defca7ee3c6eaf047e5f4debf5bff9849b6e0faf3d10ee6
|
@@ -6,11 +6,14 @@ module DataStructures101
|
|
6
6
|
# @since 0.3
|
7
7
|
class Heap
|
8
8
|
|
9
|
+
attr_reader :min_heap
|
10
|
+
|
9
11
|
def initialize(*args, min_heap: false)
|
10
12
|
@data = args
|
13
|
+
@min_heap = min_heap
|
11
14
|
@heap_check = ->(i, j) { return @data[i] >= @data[j] }
|
12
15
|
|
13
|
-
if min_heap
|
16
|
+
if @min_heap
|
14
17
|
@heap_check = ->(i, j) { return @data[i] <= @data[j] }
|
15
18
|
end
|
16
19
|
|
@@ -25,6 +28,27 @@ module DataStructures101
|
|
25
28
|
@data[i]
|
26
29
|
end
|
27
30
|
|
31
|
+
def push(*values)
|
32
|
+
values.each do |val|
|
33
|
+
@data.push(val)
|
34
|
+
upheap(@data.size - 1)
|
35
|
+
end
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def pop
|
40
|
+
result = @data.first
|
41
|
+
@data[0] = @data.pop
|
42
|
+
heapify(0)
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
def merge(heap)
|
47
|
+
new_array = @data + heap.instance_variable_get(:@data)
|
48
|
+
|
49
|
+
Heap.new(new_array, min_heap: self.min_heap)
|
50
|
+
end
|
51
|
+
|
28
52
|
def left(i)
|
29
53
|
2 * i + 1
|
30
54
|
end
|
@@ -40,7 +64,7 @@ module DataStructures101
|
|
40
64
|
private
|
41
65
|
|
42
66
|
def build_heap
|
43
|
-
start = @data.
|
67
|
+
start = @data.size / 2
|
44
68
|
|
45
69
|
start.downto(0) { |i| heapify(i) }
|
46
70
|
end
|
@@ -58,5 +82,15 @@ module DataStructures101
|
|
58
82
|
heapify(head)
|
59
83
|
end
|
60
84
|
end
|
85
|
+
|
86
|
+
def upheap(idx)
|
87
|
+
p = parent(idx)
|
88
|
+
|
89
|
+
if @heap_check.call(idx, p)
|
90
|
+
@data[idx], @data[p] = @data[p], @data[idx]
|
91
|
+
upheap(p)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
61
95
|
end
|
62
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_structures_101
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- renehernandez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|