I have a simular qwestion Haskell: Get path from every leaf node to root in a Bin tree structure. And there no answer.
(Find way). coords (2, 2) -> [2, 1, 0] I have tree where every level start with 0.
0
/ \
0 1
/ \ / \
0 1 2 3
........
I wrote some code, but it doesn't work (of cource...)
fromroot 0 _ _ = []
fromroot i j (Node t1 x t2)
= if (j div (2^(i-1)))
then x++ (fromroot (i-1) (j-2^(i-1)) t2)
else x++ (fromroot (i-1) j t1)
tree
i took from there thread.
data Tree a = Node (Tree a) a (Tree a)
myBuild :: Int -> Tree Int
myBuild n = (Node (myBuild (n*2)) n (myBuild (n*2+1)))
Hope, you'll help me.
UPD 1
Input > fromroot 3 2 (myBuild 0)
and error on myBuild
function.
Couldn't match expected type `[a0]' with actual type `Int'
Expected type: Tree [a0]
Actual type: Tree Int
In the return type of a call of `myBuild'
In the third argument of `fromroot', namely `(myBuild 0)'
Failed, modules loaded: none.
j div (2^(i-1))
to do? – dave4420j div (2^(i-1))
should choose go to left or right. I upd 1st message. – user1118043