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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58e7223d936a7a93cd7869b832d1dca06a070471eadbad905eae42fb5f2ce3ec
4
- data.tar.gz: fc0679cc03d811202d4cce7bc66699e5e0c0351bb1f50e1040dd62665a4ba7bd
3
+ metadata.gz: 7b165126a0a9d3d817393b985bbf32648adeebd9984ad51e82235859b38f19db
4
+ data.tar.gz: cb9311135096e0cbe4912cec09fae930b513f49044ad1e964c48e2f312f63581
5
5
  SHA512:
6
- metadata.gz: 8737d83179843fcaacbc108361ac971e464b6744d5043f776fc0c91c69c5224a79bb33596092e219fb76d9a91bf3a9e16f67ad29e049cd5ed3ee59fd256d9c0e
7
- data.tar.gz: cb95b2358f02b3ec1fdcbe8640ba5f6be6446bc0df8460389b925783340dec51337da89dfad93e6c184fbeac13d562526d0d4ef527449b7c3ff1e014e18d3090
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.length / 2
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DataStructures101
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  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.0
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-04-23 00:00:00.000000000 Z
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler