Class: TreeNode

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

Overview

Un treeNode e’ come un AbsNode in piu’ ha la possibilita’ di contenere altri treeNode e LeafNode

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.



17
18
19
20
21
22
23
24
# File 'lib/tree_visitor/tree_node.rb', line 17

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

Instance Attribute Details

#childsObject (readonly)

Returns the value of attribute childs.



15
16
17
# File 'lib/tree_visitor/tree_node.rb', line 15

def childs
  @childs
end

#leavesObject (readonly)

Returns the value of attribute leaves.



14
15
16
# File 'lib/tree_visitor/tree_node.rb', line 14

def leaves
  @leaves
end

Instance Method Details

#accept(visitor) ⇒ Object



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

def accept( visitor )
  visitor.enter_treeNode( self )
  @leaves.each{ |l|
    l.accept( visitor )
  }
  @childs.each { |tn|
    tn.accept( visitor )
  }
  visitor.exit_treeNode( self )
end

#add_child(treeNode) ⇒ Object



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

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

#add_leaf(leaf) ⇒ Object



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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tree_visitor/tree_node.rb', line 77

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

#invalidateObject

invalidate cached info



33
34
35
36
37
# File 'lib/tree_visitor/tree_node.rb', line 33

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

#nr_childsObject



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

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

#nr_leavesObject



44
45
46
# File 'lib/tree_visitor/tree_node.rb', line 44

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

#nr_nodesObject



39
40
41
42
# File 'lib/tree_visitor/tree_node.rb', line 39

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

#root?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tree_visitor/tree_node.rb', line 26

def root?
  @parent.nil?
end

#to_str(depth = 0) ⇒ Object



105
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
# File 'lib/tree_visitor/tree_node.rb', line 105

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 @childs.empty?
        str << " |    "
      else
        str << " |  | "
      end
      str << l.to_str
      str << "\n"
    }
  end

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