Class: FpGrowth::FpTreeNode
- Inherits:
-
Object
- Object
- FpGrowth::FpTreeNode
- Defined in:
- lib/fp_growth/fp_tree_node.rb
Overview
knows nothing about header list
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #add_child(child) ⇒ Object
- #clone_children(new_parent) ⇒ Object
-
#each(&block) ⇒ Object
walks recursive through whole tree, without self.
- #find_child(key) ⇒ Object
- #increase_count(value = 1) ⇒ Object
-
#initialize(key, count = 1) ⇒ FpTreeNode
constructor
A new instance of FpTreeNode.
- #is_root? ⇒ Boolean
- #prefix_path ⇒ Object
-
#to_s(depth = 0) ⇒ Object
prints tree structure.
Constructor Details
#initialize(key, count = 1) ⇒ FpTreeNode
Returns a new instance of FpTreeNode.
11 12 13 14 15 16 |
# File 'lib/fp_growth/fp_tree_node.rb', line 11 def initialize(key, count = 1) @key = key @count = count @parent = nil @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
6 7 8 |
# File 'lib/fp_growth/fp_tree_node.rb', line 6 def children @children end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
6 7 8 |
# File 'lib/fp_growth/fp_tree_node.rb', line 6 def count @count end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/fp_growth/fp_tree_node.rb', line 6 def key @key end |
#parent ⇒ Object
Returns the value of attribute parent.
7 8 9 |
# File 'lib/fp_growth/fp_tree_node.rb', line 7 def parent @parent end |
Instance Method Details
#add_child(child) ⇒ Object
18 19 20 21 |
# File 'lib/fp_growth/fp_tree_node.rb', line 18 def add_child(child) @children << child child.parent = self end |
#clone_children(new_parent) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/fp_growth/fp_tree_node.rb', line 53 def clone_children(new_parent) @children.each do |child| cloned_child = child.clone cloned_child.parent = new_parent new_parent.children << cloned_child end end |
#each(&block) ⇒ Object
walks recursive through whole tree, without self
62 63 64 65 66 67 |
# File 'lib/fp_growth/fp_tree_node.rb', line 62 def each &block children.each do |child| block.call(child) child.each(&block) end end |
#find_child(key) ⇒ Object
23 24 25 |
# File 'lib/fp_growth/fp_tree_node.rb', line 23 def find_child(key) children.find { |child| child.key == key } end |
#increase_count(value = 1) ⇒ Object
39 40 41 |
# File 'lib/fp_growth/fp_tree_node.rb', line 39 def increase_count(value = 1) @count += value end |
#is_root? ⇒ Boolean
69 70 71 |
# File 'lib/fp_growth/fp_tree_node.rb', line 69 def is_root? !parent end |
#prefix_path ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/fp_growth/fp_tree_node.rb', line 43 def prefix_path result = FpGrowth::PrefixPath.new(count) node = parent while !node.is_root? result << node.key node = node.parent end result end |
#to_s(depth = 0) ⇒ Object
prints tree structure
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fp_growth/fp_tree_node.rb', line 28 def to_s(depth = 0) if false title = @key ? "#{@key}:#{count}" : 'root' "( #{title} #{@children.collect { |child| child.to_s }.join(' ')})" else result = key ? "#{@key}:#{count} " : '' result << @children.collect { |child| child.to_s(depth + 1) }.join("\n" << ' ' * 4 * depth) result end end |