Class: DTRCore::Graph::LeftChildPreferentialBinaryTree
- Inherits:
-
Object
- Object
- DTRCore::Graph::LeftChildPreferentialBinaryTree
- Defined in:
- lib/dtr_core/graph/left_child_preferential_binary_tree.rb
Instance Attribute Summary collapse
-
#left_child ⇒ Object
Returns the value of attribute left_child.
-
#right_child ⇒ Object
Returns the value of attribute right_child.
-
#tree_id ⇒ Object
Returns the value of attribute tree_id.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #all_paths_to(instruction_id, cur_result: []) ⇒ Object
-
#initialize(value, indentation: 0) ⇒ LeftChildPreferentialBinaryTree
constructor
A new instance of LeftChildPreferentialBinaryTree.
- #set_left_child(node) ⇒ Object
- #set_right_child(node) ⇒ Object
- #traverse ⇒ Object
- #traverse_with_indentation ⇒ Object
Constructor Details
#initialize(value, indentation: 0) ⇒ LeftChildPreferentialBinaryTree
Returns a new instance of LeftChildPreferentialBinaryTree.
10 11 12 13 14 15 16 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 10 def initialize(value, indentation: 0) @tree_id = SecureRandom.uuid @indentation = indentation @value = value @left_child = nil @right_child = nil end |
Instance Attribute Details
#left_child ⇒ Object
Returns the value of attribute left_child.
8 9 10 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 8 def left_child @left_child end |
#right_child ⇒ Object
Returns the value of attribute right_child.
8 9 10 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 8 def right_child @right_child end |
#tree_id ⇒ Object
Returns the value of attribute tree_id.
8 9 10 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 8 def tree_id @tree_id end |
#value ⇒ Object
Returns the value of attribute value.
8 9 10 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 8 def value @value end |
Instance Method Details
#all_paths_to(instruction_id, cur_result: []) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 46 def all_paths_to(instruction_id, cur_result: []) if value.id == instruction_id return cur_result.empty? ? [[instruction_id]] : cur_result + [instruction_id] end result = nil if @left_child left_child_traverse = @left_child.all_paths_to(instruction_id, cur_result: cur_result + [value.id]) result = left_child_traverse unless left_child_traverse.nil? end if @right_child right_child_traverse = @right_child.all_paths_to(instruction_id, cur_result: cur_result + [value.id]) result = result.nil? ? right_child_traverse : [result, right_child_traverse] end result end |
#set_left_child(node) ⇒ Object
18 19 20 21 22 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 18 def set_left_child(node) raise 'Left child already exists' if @left_child @left_child = node end |
#set_right_child(node) ⇒ Object
24 25 26 27 28 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 24 def set_right_child(node) raise 'Right child already exists' if @right_child @right_child = node end |
#traverse ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 30 def traverse result = [] result << @value result += @left_child.traverse if @left_child result += @right_child.traverse if @right_child result end |
#traverse_with_indentation ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/dtr_core/graph/left_child_preferential_binary_tree.rb', line 38 def traverse_with_indentation result = [] result << [@value, @indentation] result += @left_child.traverse_with_indentation if @left_child result += @right_child.traverse_with_indentation if @right_child result end |