Class: TreeNode

Inherits:
AbsNode show all
Defined in:
lib/treevisitor/tree_node.rb

Overview

TreeNode can contains other TreeNode (children) and can contains LeafNode (leves)

TreeNode @childs -1—n-> TreeNode

@leaves -1---n-> LeafNode

Instance Attribute Summary collapse

Attributes inherited from AbsNode

#name, #next, #parent, #prev

Instance Method Summary collapse

Methods inherited from AbsNode

#depth, #path, #path_from_root, #prefix_path, #prefix_path=, #root

Constructor Details

#initialize(name, parent = nil) ⇒ TreeNode

Returns a new instance of TreeNode.



15
16
17
18
19
20
21
22
# File 'lib/treevisitor/tree_node.rb', line 15

def initialize( name, parent = nil )
  @leaves = []
  @children = []
  super( name )
  if parent
    parent.add_child( self )
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



13
14
15
# File 'lib/treevisitor/tree_node.rb', line 13

def children
  @children
end

#leavesObject (readonly)

Returns the value of attribute leaves.



12
13
14
# File 'lib/treevisitor/tree_node.rb', line 12

def leaves
  @leaves
end

Instance Method Details

#accept(visitor) ⇒ Object

return the visitor



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/treevisitor/tree_node.rb', line 94

def accept( visitor )
  visitor.enter_tree_node( self )
  @leaves.each{ |leaf|
    leaf.accept( visitor )
  }
  @children.each { |child|
    child.accept( visitor )
  }
  visitor.exit_tree_node( self )
  visitor
end

#add_child(treeNode) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/treevisitor/tree_node.rb', line 66

def add_child( treeNode )
  return if treeNode.parent == self
  if not treeNode.parent.nil?
    treeNode.remove_from_parent()
  end  
  treeNode.invalidate
  treeNode.parent = self
  @children << treeNode
end

#add_leaf(leaf) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/treevisitor/tree_node.rb', line 51

def add_leaf( leaf )
  return if leaf.parent == self
  if not leaf.parent.nil?
    leaf.remove_from_parent
  end  
  leaf.parent = self
  if @leaves.length > 0
    @leaves.last.next = leaf
    leaf.prev = @leaves.last
    leaf.next = nil
  end
  leaf.invalidate
  @leaves << leaf
end

#find(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/treevisitor/tree_node.rb', line 76

def find( name )
  return self if self.name == name
  
  leaf = @leaves.find { |l| l.name == name }
  if leaf
    return leaf
  end
  
  @children.each {|c|
    node = c.find(name)
    return node if node
  }
  nil
end

#invalidateObject

invalidate cached info invalidate propagates form parent to children and leaves



32
33
34
35
36
# File 'lib/treevisitor/tree_node.rb', line 32

def invalidate
  super
  @children.each{ |c| c.invalidate }
  @leaves.each{ |l| l.invalidate }
end

#nr_childrenObject



47
48
49
# File 'lib/treevisitor/tree_node.rb', line 47

def nr_children
  @children.length + @children.inject(0) { |sum, child| sum + child.nr_children }
end

#nr_leavesObject



43
44
45
# File 'lib/treevisitor/tree_node.rb', line 43

def nr_leaves
  @leaves.length + @children.inject(0) { |sum, child| sum + child.nr_leaves }
end

#nr_nodesObject



38
39
40
41
# File 'lib/treevisitor/tree_node.rb', line 38

def nr_nodes
  nr = @leaves.length + @children.length
  @children.inject( nr ) { |nr,c| nr + c.nr_nodes }
end

#root?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/treevisitor/tree_node.rb', line 24

def root?
  @parent.nil?
end

#to_str(depth = 0) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/treevisitor/tree_node.rb', line 106

def to_str( depth = 0 )
  str = ""
  (0...depth).step {
    str << " |-"
  }

  str << @name
  str << "\n"

  if ! @leaves.empty?
    @leaves.each{ |l|
      (0...depth-1).step {
        str << " |-"
      }
      if @children.empty?
        str << " |    "
      else
        str << " |  | "
      end
      str << l.to_str
      str << "\n"
    }
  end

  @children.each { |tn|
    str << tn.to_str( depth + 1 )
  }
  str
end