ruby_structures 2.2.0 → 2.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
2
  SHA1:
3
- metadata.gz: 3f90580fdf848a8b70513b2756ea932be5913ca0
4
- data.tar.gz: 0bdf1a300665bfa031045f07ab5a01a9b4e17f34
3
+ metadata.gz: 17721c0cc0021cc392b05b4aa49d15832241a259
4
+ data.tar.gz: 970e93098011e762e426c10063ddb3d827588575
5
5
  SHA512:
6
- metadata.gz: c9b4072b567a40780c949177e91bf4a26b92af651038e2eb1d39ac04ac3adb88bf0d37bb4bcf7e88f379fa8b72d6893912234c5615b6f5da1b74a1ac7cb330b3
7
- data.tar.gz: fbac1d228a0a991f05506bbbca51177582c97612289e82015bd941dd67c9d258feab5fcb2c491c95756edf22ec608238d9d074094385af363e19000ebff4b6c1
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.send(:depth_first_search, &prc)
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
@@ -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
- raise ArgumentError.new('Heap is empty') if @store.empty?
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
- raise ArgumentError.new('Heap is empty') if @store.empty?
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.2.0
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-10 00:00:00.000000000 Z
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!