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.



50
51
52
53
54
55
56
57
# File 'lib/treevisitor/tree_node.rb', line 50

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.



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

def children
  @children
end

#leavesObject (readonly)

Returns the value of attribute leaves.



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

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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/treevisitor/tree_node.rb', line 140

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/treevisitor/tree_node.rb', line 103

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/treevisitor/tree_node.rb', line 86

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/treevisitor/tree_node.rb', line 122

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



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

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

#nr_childrenObject



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

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

#nr_leavesObject



78
79
80
# File 'lib/treevisitor/tree_node.rb', line 78

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

#nr_nodesObject



73
74
75
76
# File 'lib/treevisitor/tree_node.rb', line 73

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

#root?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/treevisitor/tree_node.rb', line 59

def root?
  @parent.nil?
end

#to_str(prefix = "") ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/treevisitor/tree_node.rb', line 152

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