data_structures_101 0.2.10 → 0.3.0

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
- SHA1:
3
- metadata.gz: 945c75811872128f5ffe2a8f2fc0ca830d4cb1e3
4
- data.tar.gz: d078c063554757bced94b767866a78695fa0fb59
2
+ SHA256:
3
+ metadata.gz: 58e7223d936a7a93cd7869b832d1dca06a070471eadbad905eae42fb5f2ce3ec
4
+ data.tar.gz: fc0679cc03d811202d4cce7bc66699e5e0c0351bb1f50e1040dd62665a4ba7bd
5
5
  SHA512:
6
- metadata.gz: ddf48b7c1932fc72698a8c221d5255b57e8f00c520ad1273991f692ff4540261fee4da244336a4da078200ae1db772f660d4ebf1e849f1a9d32594dff4a401a1
7
- data.tar.gz: 0a547e6c0711858c1d432023d0e57c2bb9e20de36fd0edf201101b5344ea27fde5efcfa70d0eca9af97d9edcbc167b08eb11cf5e5ec53501c11c90a03f74dde1
6
+ metadata.gz: 8737d83179843fcaacbc108361ac971e464b6744d5043f776fc0c91c69c5224a79bb33596092e219fb76d9a91bf3a9e16f67ad29e049cd5ed3ee59fd256d9c0e
7
+ data.tar.gz: cb95b2358f02b3ec1fdcbe8640ba5f6be6446bc0df8460389b925783340dec51337da89dfad93e6c184fbeac13d562526d0d4ef527449b7c3ff1e014e18d3090
data/Dockerfile ADDED
@@ -0,0 +1,11 @@
1
+ FROM ruby:2.5
2
+
3
+ RUN mkdir /datastructures
4
+
5
+ WORKDIR /datastructures
6
+
7
+ COPY . .
8
+
9
+ RUN bundle install
10
+
11
+ CMD ["rspec"]
data/README.md CHANGED
@@ -40,6 +40,14 @@ To create a hash table using *separate chaining* strategy:
40
40
  hash = DataStructures101::ChainedHashTable.new
41
41
  ```
42
42
 
43
+ ### Heap example
44
+
45
+ To create a *min heap* Heap object
46
+
47
+ ```
48
+ heap = DataStructures101::Heap.new(2,3,1, -10, min_heap: true)
49
+ ```
50
+
43
51
  ### Documentation
44
52
 
45
53
  The documentation lives at the [github pages](https://renehernandez.github.io/data_structures_101/). For the nitty gritty details of the implementation go to [http://www.rubydoc.info/gems/data_structures_101](http://www.rubydoc.info/gems/data_structures_101).
data/bin/console CHANGED
File without changes
data/bin/setup CHANGED
File without changes
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DataStructures101
4
+ # Heap implementation
5
+ # @author Rene Hernandez
6
+ # @since 0.3
7
+ class Heap
8
+
9
+ def initialize(*args, min_heap: false)
10
+ @data = args
11
+ @heap_check = ->(i, j) { return @data[i] >= @data[j] }
12
+
13
+ if min_heap
14
+ @heap_check = ->(i, j) { return @data[i] <= @data[j] }
15
+ end
16
+
17
+ build_heap
18
+ end
19
+
20
+ def size
21
+ @data.size
22
+ end
23
+
24
+ def [](i)
25
+ @data[i]
26
+ end
27
+
28
+ def left(i)
29
+ 2 * i + 1
30
+ end
31
+
32
+ def right(i)
33
+ 2 * (i + 1)
34
+ end
35
+
36
+ def parent(i)
37
+ (i - 1) / 2
38
+ end
39
+
40
+ private
41
+
42
+ def build_heap
43
+ start = @data.length / 2
44
+
45
+ start.downto(0) { |i| heapify(i) }
46
+ end
47
+
48
+ def heapify(i)
49
+ l = left(i)
50
+ r = right(i)
51
+
52
+ head = i
53
+ head = l if l < @data.size && @heap_check.call(l, head)
54
+ head = r if r < @data.size && @heap_check.call(r, head)
55
+
56
+ if head != i
57
+ @data[i], @data[head] = @data[head], @data[i]
58
+ heapify(head)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DataStructures101
4
- VERSION = '0.2.10'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -6,6 +6,7 @@ require 'data_structures_101/hash/base_hash_table'
6
6
  require 'data_structures_101/hash/bucket'
7
7
  require 'data_structures_101/chained_hash_table'
8
8
  require 'data_structures_101/probe_hash_table'
9
+ require 'data_structures_101/heap'
9
10
 
10
11
  # @author Rene Hernandez
11
12
  # @since 0.1
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.2.10
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - renehernandez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-01 00:00:00.000000000 Z
11
+ date: 2018-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -136,6 +136,7 @@ files:
136
136
  - ".rubocop.yml"
137
137
  - ".travis.yml"
138
138
  - ".yardopts"
139
+ - Dockerfile
139
140
  - Gemfile
140
141
  - LICENSE.txt
141
142
  - README.md
@@ -170,6 +171,7 @@ files:
170
171
  - lib/data_structures_101/chained_hash_table.rb
171
172
  - lib/data_structures_101/hash/base_hash_table.rb
172
173
  - lib/data_structures_101/hash/bucket.rb
174
+ - lib/data_structures_101/heap.rb
173
175
  - lib/data_structures_101/linked_list.rb
174
176
  - lib/data_structures_101/probe_hash_table.rb
175
177
  - lib/data_structures_101/version.rb
@@ -193,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
195
  version: '0'
194
196
  requirements: []
195
197
  rubyforge_project:
196
- rubygems_version: 2.6.13
198
+ rubygems_version: 2.7.6
197
199
  signing_key:
198
200
  specification_version: 4
199
201
  summary: DataStructures101 is a simple gem that groups several implementations of