Class: SorobanRustBackend::LeftChildPreferentialBinaryTree
- Inherits:
-
Object
- Object
- SorobanRustBackend::LeftChildPreferentialBinaryTree
- Defined in:
- lib/left_child_preferential_binary_tree.rb
Instance Attribute Summary collapse
-
#left_child ⇒ Object
Returns the value of attribute left_child.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#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, metadata:, 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, metadata:, indentation: 0) ⇒ LeftChildPreferentialBinaryTree
Returns a new instance of LeftChildPreferentialBinaryTree.
7 8 9 10 11 12 13 14 |
# File 'lib/left_child_preferential_binary_tree.rb', line 7 def initialize(value, metadata:, 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.
5 6 7 |
# File 'lib/left_child_preferential_binary_tree.rb', line 5 def left_child @left_child end |
#metadata ⇒ Object
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/left_child_preferential_binary_tree.rb', line 5 def end |
#right_child ⇒ Object
Returns the value of attribute right_child.
5 6 7 |
# File 'lib/left_child_preferential_binary_tree.rb', line 5 def right_child @right_child end |
#tree_id ⇒ Object
Returns the value of attribute tree_id.
5 6 7 |
# File 'lib/left_child_preferential_binary_tree.rb', line 5 def tree_id @tree_id end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/left_child_preferential_binary_tree.rb', line 5 def value @value end |
Instance Method Details
#all_paths_to(instruction_id, cur_result: []) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/left_child_preferential_binary_tree.rb', line 44 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
16 17 18 19 20 |
# File 'lib/left_child_preferential_binary_tree.rb', line 16 def set_left_child(node) raise 'Left child already exists' if @left_child @left_child = node end |
#set_right_child(node) ⇒ Object
22 23 24 25 26 |
# File 'lib/left_child_preferential_binary_tree.rb', line 22 def set_right_child(node) raise 'Right child already exists' if @right_child @right_child = node end |
#traverse ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/left_child_preferential_binary_tree.rb', line 28 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
36 37 38 39 40 41 42 |
# File 'lib/left_child_preferential_binary_tree.rb', line 36 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 |