growing binary trees
Simon Strandgaard
neoneye at gmail.com
Sun Nov 12 08:27:28 CET 2006
I made up an interesting sequence:
1, 2, 3, 4, 6, 8, 10, 13, 17, 21, 26, 33, 41,
50, 62, 77, 94, 115, 142, 174, 212, 260, 319,
389, 475, 582, 711, 867, 1060, 1296
Its a binary tree where leafs is getting
added when a nodes internal counter reaches 3.
below is my slow ruby program:
class Node
def initialize
@n = 1
@childs = []
end
def count
@childs.inject(@n){|n,child| n + child.count}
end
def grow
return @n += 1 if @n < 3
@childs.each{|child| child.grow }
@childs << Node.new if @childs.size < 2
end
end
result = []
node = Node.new
30.times do |i|
result << node.count
node.grow
end
p result
# btw: the grow method below outputs A000071: Fibonacci numbers - 1.
def grow
return @n += 1 if @n < 1
@childs.each{|child| child.grow }
@childs << Node.new if @childs.size < 2
end
# btw: the grow method below outputs A054405: Row sums of array T as in A055215.
def grow
return @n += 1 if @n < 2
@childs.each{|child| child.grow }
@childs << Node.new if @childs.size < 2
end
--
Simon Strandgaard
More information about the SeqFan
mailing list