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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbsNode

#depth, #path, #path_with_prefix, #prefix_path, #prefix_path=, #root, #to_s

Constructor Details

#initialize(name, parent = nil) ⇒ TreeNode

Returns a new instance of TreeNode.



72
73
74
75
76
77
78
79
# File 'lib/treevisitor/tree_node.rb', line 72

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.



70
71
72
# File 'lib/treevisitor/tree_node.rb', line 70

def children
  @children
end

#leavesObject (readonly)

Returns the value of attribute leaves.



69
70
71
# File 'lib/treevisitor/tree_node.rb', line 69

def leaves
  @leaves
end

Class Method Details

.create(class1 = TreeNode, class2 = LeafNode, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/treevisitor/tree_node.rb', line 14

def create(class1 = TreeNode, class2 = LeafNode, &block)
  if class1.ancestors.include?(TreeNode) and class2.ancestors.include?(LeafNode)
    @tree_node_class = class1
    @leaf_node_class = class2
  elsif class1.ancestors.include?(LeafNode) and class2 == LeafNode
    @tree_node_class = self
    @leaf_node_class = class1
  end

  if @tree_node_class.nil? || @leaf_node_class.nil?
    raise "Must be specified class derived from TreeNode and LeafNode"
  end

  @scope_stack = []
  class_eval(&block)
end

Instance Method Details

#accept(visitor) ⇒ Object

return the visitor



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/treevisitor/tree_node.rb', line 162

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(tree_node) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/treevisitor/tree_node.rb', line 125

def add_child( tree_node )
  return if tree_node.parent == self
  if not tree_node.parent.nil?
    tree_node.remove_from_parent
  else
    tree_node.prefix_path = nil
  end
  tree_node.invalidate
  tree_node.parent = self
  if @children.length > 0
    @children.last.next = tree_node
    tree_node.prev = @children.last
  else
    tree_node.prev = nil
  end
  tree_node.next = nil
  @children << tree_node
end

#add_leaf(leaf) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/treevisitor/tree_node.rb', line 108

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
  else
    leaf.prev = nil
  end
  leaf.next = nil
  leaf.invalidate
  @leaves << leaf
end

#find(name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/treevisitor/tree_node.rb', line 144

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



89
90
91
92
93
# File 'lib/treevisitor/tree_node.rb', line 89

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

#nr_childrenObject



104
105
106
# File 'lib/treevisitor/tree_node.rb', line 104

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

#nr_leavesObject



100
101
102
# File 'lib/treevisitor/tree_node.rb', line 100

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

#nr_nodesObject



95
96
97
98
# File 'lib/treevisitor/tree_node.rb', line 95

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

#root?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/treevisitor/tree_node.rb', line 81

def root?
  @parent.nil?
end

#to_str(prefix = "") ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/treevisitor/tree_node.rb', line 174

def to_str( prefix= "" )
  str = ""

  if root?
    str << to_s << "\n"
  else
    str << prefix
    if self.next
      str << "|-- "
    else
      str << "\`-- "
    end
    str << to_s << "\n"
    prefix += self.next ? "|   " : "    "
  end
  
  @leaves.each do |leaf|
    str << prefix
    if !leaf.next.nil? or !@children.empty?
      str << "|-- "
    else
      str << "\`-- "
    end
    str <<  leaf.to_s << "\n"
  end

  @children.each do |child|
    str << child.to_str( prefix )
  end
  str
end