class AdderTreeNode

A node in the AdderTree.

Author

Peter Kofler

Attributes

data[R]
parent[R]

Public Class Methods

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

Public Instance Methods

add(o) click to toggle source
# File lib/javaclass/adder_tree.rb, line 18
def add(o)
  node = AdderTreeNode.new(o, self)
  @children << node
  node
end
children() click to toggle source
# File lib/javaclass/adder_tree.rb, line 14
def children
  @children.dup
end
contain?(o) click to toggle 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
debug_print() click to toggle source

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

# File lib/javaclass/adder_tree.rb, line 64
def debug_print
  puts ' ' * level + data
  @children.each { |child| child.debug_print }
end
level() click to toggle source
# File lib/javaclass/adder_tree.rb, line 32
def level
  @parent.level + 1
end
levels() click to toggle source
# File lib/javaclass/adder_tree.rb, line 45
def levels
  ( [1] + @children.map { |child| 1 + child.levels } ).max
end
root() click to toggle source
# File lib/javaclass/adder_tree.rb, line 36
def root
  @parent.root
end
size() click to toggle source

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

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

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]]
# 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