recursion - Stack_overflow with tree traversal -


my tree defined below: (it's better understanding, data type more complicated.)

  type tree = {     mutable value : int;     mutable nodes : tree list   } 

i need find sequence of 0 , 1, shown below:

    1       |   0 0   \ /    1    |    1 

the output root , sequence of 0 , 1. here code that: (i assumed sequence appear when nodes (tree list) of tree have value 0 has 1 element, need change because it's not necessary.)

let rec getsequence tree =    match tree.value   | 0 -> if (list.length tree.nodes) = 1   let nexttree = list.hd tree.nodes in   match nexttree.value   | 1 ->      nexttree.nodes <- [];     tree.nodes <- [nexttree];     [tree]   | 0 -> list.concat (list.map (fun t -> getsequence t) nexttree.nodes) else list.concat (list.map (fun t -> getsequence t) tree.nodes)   | 1 -> list.concat (list.map (fun t -> getsequence t) tree.nodes) 

for reason when execute code exception stack_overflow raised. me?

let nexttree = list.hd tree.nodes in nexttree.nodes <- []; 

is not equivalent as:

tree.nodes<-(list.tl tree.nodes); 

with first, tree doesn't change of list contains same operation , have stack_overflow


Comments