adt_utilit 0.0.1 → 0.0.2
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/adt_utilit.rb +1 -0
- data/lib/adt_utilit/binary_tree_node.rb +125 -0
- data/lib/adt_utilit/graph_node.rb +96 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24a8c1885ed5a72b48d7407890a5ba8258eddb41
|
4
|
+
data.tar.gz: 91762ad975b58dd73ad3e817b832a39d9946f778
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 033d3b191e848a08d2aed9aee6880a23a8b85690d196b462a3f32ce0e28c79c871392d79a247dcbb8d89fbfc938923067e570a1e4c18401984878fcd1b9595a3
|
7
|
+
data.tar.gz: 5a962e15cdd3a06ab893465262e99a402ac5636b1a7f5c7c020de415f483426263d4b6aa229962ede029853cf68bdb76ed25ca7960bc1132d9a98969ad47ad1a
|
data/lib/adt_utilit.rb
CHANGED
@@ -0,0 +1,125 @@
|
|
1
|
+
require_relative "graph_node"
|
2
|
+
require 'byebug'
|
3
|
+
|
4
|
+
class BinaryTreeNode < GraphNode
|
5
|
+
attr_reader :parent
|
6
|
+
|
7
|
+
protected
|
8
|
+
attr_writer :parent
|
9
|
+
|
10
|
+
public
|
11
|
+
def initialize(value)
|
12
|
+
super(value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def left_child
|
16
|
+
@children[0]
|
17
|
+
end
|
18
|
+
|
19
|
+
def right_child
|
20
|
+
@children[1]
|
21
|
+
end
|
22
|
+
|
23
|
+
def connect(node)
|
24
|
+
if @children.length >= 2
|
25
|
+
raise "BinaryTreeNode can have only two children"
|
26
|
+
end
|
27
|
+
if node.children.include?(self)
|
28
|
+
raise "Undirected edges are not allowed in binary tree"
|
29
|
+
end
|
30
|
+
super(node)
|
31
|
+
end
|
32
|
+
|
33
|
+
# def switch(node)
|
34
|
+
# unless self.children.include?(node)
|
35
|
+
# raise "No parent-child relationship exists"
|
36
|
+
# end
|
37
|
+
# replace_idx = self.children.find_index(node)
|
38
|
+
# self.children, node.children = node.children, self.children
|
39
|
+
# node.children[replace_idx] = self
|
40
|
+
#
|
41
|
+
# if self.parent.nil?
|
42
|
+
# node.parent = nil
|
43
|
+
# else
|
44
|
+
# replace_idx = self.parent.children.find_index(self)
|
45
|
+
# self.parent, node.parent = node, self.parent
|
46
|
+
# node.parent.children[replace_idx] = node
|
47
|
+
# end
|
48
|
+
# node.children.each{|child| child.parent = node}
|
49
|
+
# # debugger
|
50
|
+
# end
|
51
|
+
|
52
|
+
def filled?
|
53
|
+
!!(left_child && right_child)
|
54
|
+
end
|
55
|
+
|
56
|
+
def trav_in_order(search_value = nil, &prc)
|
57
|
+
if (search_value.nil? && !block_given?) || (search_value && block_given?)
|
58
|
+
raise "Wrong number of argument"
|
59
|
+
end
|
60
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
61
|
+
|
62
|
+
result = left_child ? left_child.trav_in_order(nil, &prc) : nil
|
63
|
+
return result unless result.nil?
|
64
|
+
|
65
|
+
result = prc.call(self.value) ? self : nil
|
66
|
+
return result unless result.nil?
|
67
|
+
|
68
|
+
result = right_child ? right_child.trav_in_order(nil, &prc) : nil
|
69
|
+
return result unless result.nil?
|
70
|
+
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def trav_pre_order(search_value = nil, &prc)
|
75
|
+
if (search_value.nil? && !block_given?) || (search_value && block_given?)
|
76
|
+
raise "Wrong number of argument"
|
77
|
+
end
|
78
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
79
|
+
|
80
|
+
result = prc.call(self.value) ? self : nil
|
81
|
+
return result unless result.nil?
|
82
|
+
|
83
|
+
result = left_child ? left_child.trav_pre_order(nil, &prc) : nil
|
84
|
+
return result unless result.nil?
|
85
|
+
|
86
|
+
result = right_child ? right_child.trav_pre_order(nil, &prc) : nil
|
87
|
+
return result unless result.nil?
|
88
|
+
|
89
|
+
nil
|
90
|
+
end
|
91
|
+
|
92
|
+
def trav_post_order(search_value = nil, &prc)
|
93
|
+
if (search_value.nil? && !block_given?) || (search_value && block_given?)
|
94
|
+
raise "Wrong number of argument"
|
95
|
+
end
|
96
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
97
|
+
|
98
|
+
result = left_child ? left_child.trav_post_order(nil, &prc) : nil
|
99
|
+
return result unless result.nil?
|
100
|
+
|
101
|
+
result = right_child ? right_child.trav_post_order(nil, &prc) : nil
|
102
|
+
return result unless result.nil?
|
103
|
+
|
104
|
+
result = prc.call(self.value) ? self : nil
|
105
|
+
return result unless result.nil?
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
|
109
|
+
def _trav_right_first(search_value = nil, &prc)
|
110
|
+
if (search_value.nil? && !block_given?) || (search_value && block_given?)
|
111
|
+
raise "Wrong number of argument"
|
112
|
+
end
|
113
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
114
|
+
|
115
|
+
result = right_child ? right_child._trav_right_first(nil, &prc) : nil
|
116
|
+
return result unless result.nil?
|
117
|
+
|
118
|
+
result = left_child ? left_child._trav_right_first(nil, &prc) : nil
|
119
|
+
return result unless result.nil?
|
120
|
+
|
121
|
+
result = prc.call(self) ? self : nil
|
122
|
+
return result unless result.nil?
|
123
|
+
nil
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'byebug'
|
3
|
+
class GraphNode
|
4
|
+
attr_reader :children, :value
|
5
|
+
|
6
|
+
protected
|
7
|
+
attr_writer :children
|
8
|
+
|
9
|
+
public
|
10
|
+
def initialize(value)
|
11
|
+
@value = value
|
12
|
+
@children = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def connect(node)
|
16
|
+
@children << node
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove(node)
|
20
|
+
result = @children.delete(node){nil}
|
21
|
+
result.nil? ? nil : node
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_all
|
25
|
+
@children = []
|
26
|
+
end
|
27
|
+
|
28
|
+
def dfs(search_value = nil, searched = Set.new, &prc)
|
29
|
+
if (search_value && block_given?) || (search_value.nil? && !block_given?)
|
30
|
+
raise "Wrong number of argument"
|
31
|
+
end
|
32
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
33
|
+
|
34
|
+
if prc.call(self.value)
|
35
|
+
return self
|
36
|
+
else
|
37
|
+
searched.add(self)
|
38
|
+
end
|
39
|
+
|
40
|
+
return nil if @children.empty?
|
41
|
+
|
42
|
+
@children.each do |child_node|
|
43
|
+
return nil if searched.include?(child_node)
|
44
|
+
result = child_node.dfs(nil, searched, &prc)
|
45
|
+
return result if !result.nil?
|
46
|
+
searched.add(child_node)
|
47
|
+
end
|
48
|
+
return nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def bfs(search_value = nil, searched = Set.new, &prc)
|
52
|
+
if (search_value && block_given?) || (search_value.nil? && !block_given?)
|
53
|
+
raise "Wrong number of argument"
|
54
|
+
end
|
55
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
56
|
+
|
57
|
+
search_queue = [self]
|
58
|
+
until search_queue.empty?
|
59
|
+
node = search_queue.shift
|
60
|
+
return node if prc.call(node.value)
|
61
|
+
searched.add(node)
|
62
|
+
|
63
|
+
if node.children.length > 0
|
64
|
+
node.children.each do |child|
|
65
|
+
search_queue << child unless searched.include?(child)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
return nil
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
def _bfs(search_value = nil, searched = Set.new, &prc)
|
74
|
+
if (search_value && block_given?) || (search_value.nil? && !block_given?)
|
75
|
+
raise "Wrong number of argument"
|
76
|
+
end
|
77
|
+
prc = Proc.new{ |val| val == search_value } if search_value
|
78
|
+
|
79
|
+
search_queue = [self]
|
80
|
+
until search_queue.empty?
|
81
|
+
node = search_queue.shift
|
82
|
+
return node if prc.call(node)
|
83
|
+
searched.add(node)
|
84
|
+
|
85
|
+
if node.children.length > 0
|
86
|
+
node.children.each do |child|
|
87
|
+
search_queue << child unless searched.include?(child)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
return nil
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adt_utilit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hirosvk
|
@@ -19,6 +19,8 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/adt_utilit.rb
|
22
|
+
- lib/adt_utilit/binary_tree_node.rb
|
23
|
+
- lib/adt_utilit/graph_node.rb
|
22
24
|
- lib/adt_utilit/linked_list.rb
|
23
25
|
- lib/adt_utilit/linked_list_node.rb
|
24
26
|
- lib/adt_utilit/queue.rb
|