Class: SorobanRustBackend::LeftChildPreferentialBinaryTree

Inherits:
Object
  • Object
show all
Defined in:
lib/left_child_preferential_binary_tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_childObject

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

#metadataObject

Returns the value of attribute metadata.



5
6
7
# File 'lib/left_child_preferential_binary_tree.rb', line 5

def 
  
end

#right_childObject

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_idObject

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

#valueObject

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

#traverseObject



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_indentationObject



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