Class: CoreExtensions::Tree

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(node_value = nil) ⇒ Tree

Creates a new tree with a root element and no child nodes.

Parameters:

  • node_value (Object) (defaults to: nil)

    The value associated to the root element



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_nodesHash (readonly)

The child nodes of the current tree node.

Returns:

  • (Hash)


14
15
16
# File 'lib/core_extensions/tree.rb', line 14

def children_nodes
  @children_nodes
end

#valueObject

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.

Parameters:

  • child_id (Object)

    The unique identifier for the child node to retrieve or insert.

  • child_value (Object) (defaults to: nil)

    The value associated to the child node to insert. Ignored if a child node identified by the given child_id already exists.

Returns:

  • (Object)

    The value associated to the child node identified by the given child_id.



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.

Parameters:

  • child_id (Object)

    The unique identifier of the child.

Returns:

  • (Boolean)

    True if the child node identified by child_id exists, False otherwise.



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.

Parameters:

  • child_id (Object)

    The unique identifier of the child.

Returns:

  • (Object)

    The node value of the child identified by child_id.

Raises:

  • (KeyError)

    If the child node identified by child_id does not exist.



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.

Returns:

  • (Boolean)


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).

Parameters:

  • separator (String) (defaults to: '/')

    The separator to be used between node identifiers.

Returns:

  • (Hash)


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).

Returns:

  • (String)


64
65
66
67
68
69
70
71
# File 'lib/core_extensions/tree.rb', line 64

def to_s(offset = '')
  str = offset + 'VALUE: ' + @value.to_s + "\n"
  @children_nodes.each do |id, node|
    str += offset + ' * ID:   \'' + id.to_s + '\'' + "\n"
    str += node.to_s(offset + '   ')
  end
  str
end