Binary Search Tree to list in Haskell -
i've been given bst in haskell capable of adding, removing, lookup, findingmax, , removingmax. have make function converts data list structure.
bsttolist:: (bst k v) -> [string] bsttolist emptybst = ["hi"] bsttolist (bstnode k v nl nr) = bsttolist nl ++ bsttolist nr
the reason have emptybst = ["hi"] in there check returning. when given input of
bsttolist (bstadd 1 "phil" (bstadd 2 "ip" emptybst))
returns list of ["hi","hi","hi"] , i'm unclear why returning empty list. assume functions other bsttolist correct , working properly. appreciated!
the line
bsttolist (bstnode k v nl nr) = bsttolist nl ++ bsttolist nr
doesn't use value @ node, why ever data emptybst bits.
you need
bsttolist :: bst k v -> [v] bsttolist emptybst = [] bsttolist (bstnode k v nl nr) = bsttolist nl ++ [v] ++ bsttolist nr
so value @ node inserted between values on left , right of it. (this called in order traversal.)
if want list both keys , values need
bsttolist :: bst k v -> [(k,v)] bsttolist emptybst = [] bsttolist (bstnode k v nl nr) = bsttolist nl ++ [(k,v)] ++ bsttolist nr
notice brackets around (k, v) - turn 2 items pair. same goes type signature. (the missing pair syntax why got kind error.)
[k,v] can work data if k , v same type, , [k,v] can't work type.
Comments
Post a Comment