Hi Ahmed, sorry for the late answer, but
it is a busy weekend... I am not sure I understand
your question, but the following for loop connects the leafs
to the children.
for i in 1:nChildren
root.child[i].child = all_leafs[i]
end
Here a full example code where some information is printed in the end. I
call leafs now grand-children, because it is just children of the children.
I hope this makes my attempt a little more clear.
type MyNode{T}
data::Vector{T}
level::Int
child :: Vector{MyNode}
nchild :: Int
end
MyNode() = MyNode(Float64[], 0, MyNode[], 0)
function add_children!(parentNode, nChildren)
data = Float64[] # do something with the data here, I dont know whats
needed..
level = parentNode.level + 1
for i in 1:nChildren
child = MyNode(data, level, MyNode[], 0)
push!(parentNode.child, child)
end
parentNode.nchild += nChildren
end
function node_info(node)
println("Level : ", node.level)
println("nChildren: ", node.nchild)
println("data : ", node.data)
end
nChildren = 2
# create root node
root = MyNode()
# add children to root
add_children!(root, nChildren)
# create an empty list for each child, in this list we can then
# later insert grand_children (grand-children = leafs)
# In the end this list will be used like this:
# g_children_list[1] = list of all grand-children of child 1.
# g_children_list[2] = list of all grand-children of child 2.
grand_children_list = Any[MyNode[] for i in 1:nChildren]
# set the total number of grand-children (= total number of leafs)
# then create all of them and isert them into the list.
nGrandChildren = 10
for i in 1:nGrandChildren
# create a grand_child (= leaf)
data = Float64[i] # here I just fill the grand-children-index into data.
level = 2
grand_child = MyNode(data, level, MyNode[], 0)
# randomly choose to which of the children it belongs
childIndex = rand(1:nChildren)
# add the grand_child to the list of grand_children of the randomly
picked child
push!(grand_children_list[childIndex], grand_child)
end
# now loop over all children and assign all the grand-children to them.
for i in 1:nChildren
root.child[i].child = grand_children_list[i]
root.child[i].nchild = length(grand_children_list[i])
end
node_info(root)
println("------------------------")
node_info(root.child[1])
println("------------------------")
for i in 1:root.child[1].nchild
node_info(root.child[1].child[i])
println(" - - - - - - - - - - - - ")
end
Maybe my attempts to be uber-clear are not helping. Someone else has a
better/clearer idea? :)