data_structures_101 0.2.10 → 0.3.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 +5 -5
- data/Dockerfile +11 -0
- data/README.md +8 -0
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/lib/data_structures_101/heap.rb +62 -0
- data/lib/data_structures_101/version.rb +1 -1
- data/lib/data_structures_101.rb +1 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 58e7223d936a7a93cd7869b832d1dca06a070471eadbad905eae42fb5f2ce3ec
|
4
|
+
data.tar.gz: fc0679cc03d811202d4cce7bc66699e5e0c0351bb1f50e1040dd62665a4ba7bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8737d83179843fcaacbc108361ac971e464b6744d5043f776fc0c91c69c5224a79bb33596092e219fb76d9a91bf3a9e16f67ad29e049cd5ed3ee59fd256d9c0e
|
7
|
+
data.tar.gz: cb95b2358f02b3ec1fdcbe8640ba5f6be6446bc0df8460389b925783340dec51337da89dfad93e6c184fbeac13d562526d0d4ef527449b7c3ff1e014e18d3090
|
data/Dockerfile
ADDED
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
|
data/lib/data_structures_101.rb
CHANGED
@@ -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.
|
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:
|
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
|
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
|