Class: TreeBranch::Node

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

Overview

Main class the outlines the basic operations and structure of a node in the tree.

Direct Known Subclasses

SimpleNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Node

Returns a new instance of Node.



15
16
17
18
# File 'lib/tree_branch/node.rb', line 15

def initialize(data)
  @data     = data
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/tree_branch/node.rb', line 13

def children
  @children
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/tree_branch/node.rb', line 13

def data
  @data
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/tree_branch/node.rb', line 34

def ==(other)
  eql?(other)
end

#add(*children_to_add) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/tree_branch/node.rb', line 20

def add(*children_to_add)
  children_to_add.flatten.each do |child|
    raise ArgumentError, "Improper class: #{child.class.name}" unless child.is_a?(self.class)

    @children << child
  end

  self
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tree_branch/node.rb', line 30

def eql?(other)
  data == other.data && children == other.children
end

#to_sObject



38
39
40
# File 'lib/tree_branch/node.rb', line 38

def to_s
  "[#{self.class.name}] Data: #{data}, Child Count: #{children.length}"
end