Class: CoreExtensions::Tree
- Inherits:
-
Object
- Object
- CoreExtensions::Tree
- Defined in:
- lib/core_extensions/tree.rb
Overview
N-ary tree data structure. Each node of the tree contains an Object
(the node value) and can be accessed from its parent node using an identifier Object
. A node can be identified uniquely by the succession of identifiers that lead from the tree root to itself.
Instance Attribute Summary collapse
-
#children_nodes ⇒ Hash
readonly
The child nodes of the current tree node.
-
#value ⇒ Object
The value of the current tree node.
Instance Method Summary collapse
-
#add_child_node(child_id, child_value = nil) ⇒ Object
Retrieves the value associated to a child node of the current tree node, inserting it first if it does not exist.
-
#child_exists?(child_id) ⇒ Boolean
Tests whether the current tree node has a specific child.
-
#child_node(child_id) ⇒ Object
Retrieves the child of the current tree node.
-
#children? ⇒ Boolean
Check if the current Tree node has children.
-
#initialize(node_value = nil) ⇒ Tree
constructor
Creates a new tree with a root element and no child nodes.
-
#to_h(separator = '/', id_prefix = '') ⇒ Hash
Returns a hash representation of the current tree node and all its children, recursively (depth-first).
-
#to_s(offset = '') ⇒ String
Returns the string representation of the current tree node and all its children, recursively (depth-first).
Constructor Details
#initialize(node_value = nil) ⇒ Tree
Creates a new tree with a root element and no child nodes.
18 19 20 21 |
# File 'lib/core_extensions/tree.rb', line 18 def initialize(node_value = nil) @value = node_value @children_nodes = {} end |
Instance Attribute Details
#children_nodes ⇒ Hash (readonly)
The child nodes of the current tree node.
14 15 16 |
# File 'lib/core_extensions/tree.rb', line 14 def children_nodes @children_nodes end |
#value ⇒ Object
The value of the current tree node.
10 11 12 |
# File 'lib/core_extensions/tree.rb', line 10 def value @value end |
Instance Method Details
#add_child_node(child_id, child_value = nil) ⇒ Object
Retrieves the value associated to a child node of the current tree node, inserting it first if it does not exist.
79 80 81 82 |
# File 'lib/core_extensions/tree.rb', line 79 def add_child_node(child_id, child_value = nil) @children_nodes[child_id] = Tree.new(child_value) if @children_nodes[child_id].nil? @children_nodes[child_id] end |
#child_exists?(child_id) ⇒ Boolean
Tests whether the current tree node has a specific child.
33 34 35 36 37 38 |
# File 'lib/core_extensions/tree.rb', line 33 def child_exists?(child_id) child_node(child_id) true rescue KeyError false end |
#child_node(child_id) ⇒ Object
Retrieves the child of the current tree node.
44 45 46 |
# File 'lib/core_extensions/tree.rb', line 44 def child_node(child_id) @children_nodes.fetch(child_id) end |
#children? ⇒ Boolean
Check if the current Tree node has children.
26 27 28 |
# File 'lib/core_extensions/tree.rb', line 26 def children? !@children_nodes.empty? end |
#to_h(separator = '/', id_prefix = '') ⇒ Hash
Returns a hash representation of the current tree node and all its children, recursively (depth-first).
52 53 54 55 56 57 58 59 |
# File 'lib/core_extensions/tree.rb', line 52 def to_h(separator = '/', id_prefix = '') hash = {} id_prefix.empty? ? hash[separator] = @value : hash[id_prefix] = @value @children_nodes.each do |id, node| hash.merge!(node.to_h(separator, id_prefix + separator + id.to_s)) end hash end |
#to_s(offset = '') ⇒ String
Returns the string representation of the current tree node and all its children, recursively (depth-first).
64 65 66 67 68 69 70 71 |
# File 'lib/core_extensions/tree.rb', line 64 def to_s(offset = '') str = offset + "VALUE: #{@value}\n" @children_nodes.each do |id, node| str += offset + " * ID: '#{id}'\n" str += node.to_s("#{offset} ") end str end |