ruby_structures 2.2.0 → 2.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 +4 -4
- data/lib/data_structures/binary_tree.rb +12 -14
- data/lib/data_structures/heap.rb +54 -5
- data/lib/data_structures/linked_list.rb +11 -0
- data/lib/data_structures/lru_cache.rb +12 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17721c0cc0021cc392b05b4aa49d15832241a259
|
4
|
+
data.tar.gz: 970e93098011e762e426c10063ddb3d827588575
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34c672dd7800d5f384e642213075776adbf203dbd9c365b8bfdd7b7dac50e28c0f11edb6ad9ec5eec2e7f23d6758b0532a14dfd9cf663424a0d15e767d51c2e4
|
7
|
+
data.tar.gz: c78d182ee08c21f9d5d328d549bb2a5bb073b9f4a94fdcec4013686f5d41d5d9566c529290e9a457452e644ea465e3d457d5f16a8f10be0e83512a2423901f38
|
@@ -26,12 +26,22 @@ class BinaryTree
|
|
26
26
|
@leaves = []
|
27
27
|
end
|
28
28
|
|
29
|
-
def depth_first_search(target=nil, &prc)
|
29
|
+
def depth_first_search(target=nil, start_node=nil, &prc)
|
30
30
|
raise ArgumentError.new('Must pass either a target value or a block') unless (target || prc) && !(target && prc)
|
31
31
|
prc ||= Proc.new { |node| node.send(:val) == target }
|
32
32
|
|
33
33
|
return nil if @head.nil?
|
34
|
-
@head
|
34
|
+
start_node ||= @head
|
35
|
+
|
36
|
+
return start_node if prc.call(start_node)
|
37
|
+
|
38
|
+
[start_node.send(:left_child), start_node.send(:right_child)].each do |child|
|
39
|
+
next if child.nil?
|
40
|
+
result = depth_first_search(nil, child, &prc)
|
41
|
+
return result unless result.nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
nil
|
35
45
|
end
|
36
46
|
|
37
47
|
def breadth_first_search(target=nil, &prc)
|
@@ -73,17 +83,5 @@ class BinaryTreeNode
|
|
73
83
|
|
74
84
|
private
|
75
85
|
|
76
|
-
def depth_first_search(&prc)
|
77
|
-
return self if prc.call(self)
|
78
|
-
|
79
|
-
[@left_child, @right_child].each do |child|
|
80
|
-
next if child.nil?
|
81
|
-
result = child.send(:depth_first_search, &prc)
|
82
|
-
return result unless result.nil?
|
83
|
-
end
|
84
|
-
|
85
|
-
nil
|
86
|
-
end
|
87
|
-
|
88
86
|
attr_accessor :val, :parent, :left_child, :right_child
|
89
87
|
end
|
data/lib/data_structures/heap.rb
CHANGED
@@ -1,10 +1,38 @@
|
|
1
1
|
class Heap
|
2
|
+
def self.from_array(array)
|
3
|
+
heap = Heap.new
|
4
|
+
heap.instance_variable_set(:@store, array)
|
5
|
+
|
6
|
+
heap.send(:store).length.downto(0).each do |idx|
|
7
|
+
children_indices = heap.send(:children_indices, idx)
|
8
|
+
heap.send(:heapify_down, idx, children_indices) unless children_indices.empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
heap
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"Heap: head=#{self.peek || 'nil'}, length=#{self.length}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def inspect
|
19
|
+
"Heap: head=#{self.peek || 'nil'}, length=#{self.length}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def empty?
|
23
|
+
@store.empty?
|
24
|
+
end
|
25
|
+
|
26
|
+
def length
|
27
|
+
@store.length
|
28
|
+
end
|
29
|
+
|
2
30
|
def initialize
|
3
31
|
@store = []
|
4
32
|
end
|
5
33
|
|
6
34
|
def peek
|
7
|
-
|
35
|
+
return nil if @store.empty?
|
8
36
|
@store.first
|
9
37
|
end
|
10
38
|
|
@@ -19,8 +47,14 @@ class Heap
|
|
19
47
|
el
|
20
48
|
end
|
21
49
|
|
50
|
+
def insert_mutliple(arr)
|
51
|
+
raise ArgumentError.new("Can only insert multiple elements via an Array. You passed a #{arr.class}.") unless arr.is_a?(Array)
|
52
|
+
array = self.send(:store) + arr
|
53
|
+
self.class.from_array(array)
|
54
|
+
end
|
55
|
+
|
22
56
|
def extract
|
23
|
-
|
57
|
+
return nil if @store.empty?
|
24
58
|
return @store.shift if @store.length <= 2
|
25
59
|
|
26
60
|
@store[0], @store[-1] = @store[-1], @store[0]
|
@@ -29,11 +63,27 @@ class Heap
|
|
29
63
|
el_idx = 0
|
30
64
|
children_indices = children_indices(el_idx)
|
31
65
|
|
32
|
-
heapify_down(el_idx, children_indices)
|
66
|
+
heapify_down(el_idx, children_indices) unless children_indices.empty?
|
33
67
|
|
34
68
|
head
|
35
69
|
end
|
36
70
|
|
71
|
+
def find(el)
|
72
|
+
return nil if @store.empty? || el < @store.first
|
73
|
+
@store.each { |store_el| return store_el if store_el == el }
|
74
|
+
nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def include?(el)
|
78
|
+
@store.include?(el)
|
79
|
+
end
|
80
|
+
|
81
|
+
def merge(other_heap)
|
82
|
+
raise ArgumentError.new("May only merge with a Heap. You passed a #{other_heap.class}.") unless other_heap.is_a?(Heap)
|
83
|
+
array = self.send(:store) + other_heap.send(:store)
|
84
|
+
self.class.from_array(array)
|
85
|
+
end
|
86
|
+
|
37
87
|
private
|
38
88
|
|
39
89
|
def heapify_up(el_idx, parent_idx)
|
@@ -45,7 +95,6 @@ class Heap
|
|
45
95
|
end
|
46
96
|
|
47
97
|
def heapify_down(el_idx, children_indices)
|
48
|
-
return if children_indices.empty?
|
49
98
|
if children_indices.length == 1
|
50
99
|
child_idx = children_indices.first
|
51
100
|
if @store[el_idx] > @store[child_idx]
|
@@ -58,7 +107,7 @@ class Heap
|
|
58
107
|
@store[el_idx], @store[lowest_child_idx] = @store[lowest_child_idx], @store[el_idx]
|
59
108
|
|
60
109
|
children_indices = children_indices(lowest_child_idx)
|
61
|
-
heapify_down(lowest_child_idx, children_indices)
|
110
|
+
heapify_down(lowest_child_idx, children_indices) unless children_indices.empty?
|
62
111
|
end
|
63
112
|
end
|
64
113
|
|
@@ -30,6 +30,17 @@ class LinkedList
|
|
30
30
|
@head.send(:next) == @tail
|
31
31
|
end
|
32
32
|
|
33
|
+
def length
|
34
|
+
return 0 if self.empty?
|
35
|
+
count = 0
|
36
|
+
node = self.first
|
37
|
+
until node == @tail
|
38
|
+
count += 1
|
39
|
+
node = node.send(:next)
|
40
|
+
end
|
41
|
+
count
|
42
|
+
end
|
43
|
+
|
33
44
|
def first
|
34
45
|
first = @head.send(:next)
|
35
46
|
first == @tail ? nil : first
|
@@ -12,10 +12,22 @@ class LRUCache
|
|
12
12
|
@linked_list.to_a
|
13
13
|
end
|
14
14
|
|
15
|
+
def to_s
|
16
|
+
@linked_list.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def inspect
|
20
|
+
@linked_list.inspect
|
21
|
+
end
|
22
|
+
|
15
23
|
def empty?
|
16
24
|
@size == 0
|
17
25
|
end
|
18
26
|
|
27
|
+
def length
|
28
|
+
@size
|
29
|
+
end
|
30
|
+
|
19
31
|
def first
|
20
32
|
@linked_list.first
|
21
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_structures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Numeroff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby implementations of a Stack, Queue, Linked List, Binary Tree, LRU
|
14
14
|
Cache and Heap. More to come!
|