Class: Tree::TreeNode
Overview
Instance Attribute Summary collapse
- #children_hash ⇒ Object readonly
Additions for rubytree collapse
-
#all_child_keys ⇒ Array
Returns an array of all child keys.
-
#find(node_name) ⇒ Object
As opposed to [] the entire tree is searched.
-
#print_tree(level = 0, breite = 8) ⇒ void
now it works.
-
#to_s ⇒ String
now handles symbols in the tree as well.
Instance Attribute Details
#children_hash ⇒ Object (readonly)
21 22 23 |
# File 'lib/kyanite/tree.rb', line 21 def children_hash @children_hash end |
Instance Method Details
#all_child_keys ⇒ Array
Returns an array of all child keys. The whole tree is searched.
83 84 85 86 87 88 89 90 91 |
# File 'lib/kyanite/tree.rb', line 83 def all_child_keys result = @children_hash.keys || [] children do |c| #puts "durchsuche #{c}" next if c.children_hash.empty? result += c.all_child_keys end result end |
#find(node_name) ⇒ Object
As opposed to [] the entire tree is searched. Returns the first hit.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kyanite/tree.rb', line 64 def find( node_name ) return self if self.name == node_name result = self[node_name] return result if result children do |c| next if c.children.empty? #puts "durchsuche #{c.name}" result = c.find(node_name) return result if result end nil end |
#print_tree(level = 0, breite = 8) ⇒ void
This method returns an undefined value.
now it works
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kyanite/tree.rb', line 39 def print_tree(level = 0, breite = 8) if is_root? print "*" elsif level == 0 print 'X' else print "|" unless parent.is_last_sibling? print(" " * (level - 1 ) * breite) print(is_last_sibling? ? "+" : "|") print "---" print(has_children? ? "+" : ">") end puts " #{name}" children { |child| child.print_tree(level + 1)} nil end |
#to_s ⇒ String
now handles symbols in the tree as well
27 28 29 30 31 32 33 34 |
# File 'lib/kyanite/tree.rb', line 27 def to_s ":#{@name}" # "Node Name: #{@name}" + # " Content: #{@content || '<Empty>'}" + # " Parent: #{(isRoot?() ? '<None>' : @parent.name)}" + # " Children: #{@children.length}" + # " Total Nodes: #{size()}" end |