Class AdderTreeNode
In: lib/javaclass/adder_tree.rb
Parent: Object

A node in the AdderTree.

Author:Peter Kofler

Methods

add   children   contain?   debug_print   level   levels   new   root   size   to_a  

Attributes

data  [R] 
parent  [R] 

Public Class methods

[Source]

# File lib/javaclass/adder_tree.rb, line 8
  def initialize(data, parent)
    @data = data
    @parent = parent
    @children = []
  end

Public Instance methods

[Source]

# File lib/javaclass/adder_tree.rb, line 18
  def add(o)
    node = AdderTreeNode.new(o, self)
    @children << node
    node
  end

[Source]

# File lib/javaclass/adder_tree.rb, line 14
  def children
    @children.dup
  end

[Source]

# File lib/javaclass/adder_tree.rb, line 24
  def contain?(o)
    if @data == o
      self
    else
      @children.find { |child| child.contain?(o) }
    end
  end

Prints the tree to the console. Each level of the tree is intended by a blank.

[Source]

# File lib/javaclass/adder_tree.rb, line 64
  def debug_print
    puts ' ' * level + data
    @children.each { |child| child.debug_print }
  end

[Source]

# File lib/javaclass/adder_tree.rb, line 32
  def level
    @parent.level + 1
  end

[Source]

# File lib/javaclass/adder_tree.rb, line 45
  def levels
    ( [1] + @children.map { |child| 1 + child.levels } ).max
  end

[Source]

# File lib/javaclass/adder_tree.rb, line 36
  def root
    @parent.root
  end

Return the number of nodes this this (sub-)tree.

[Source]

# File lib/javaclass/adder_tree.rb, line 41
  def size
    @children.inject(1) { |sum, child| sum + child.size }
  end

Return an array of all children. Each child has its own array. For example

 tree = AdderTree.new(0)
 tree.add(1)
 tree.to_a               # => [0, [1]]

[Source]

# File lib/javaclass/adder_tree.rb, line 53
  def to_a
    if @children.size > 0
      sublist = []
      @children.each { |child| child.to_a.each { |c| sublist << c } } 
      [data, sublist]
    else
      [data]
    end
  end

[Validate]