Sunday 15 March 2015

How to find the node that holds the minimum element in a binary tree in Haskell? -



How to find the node that holds the minimum element in a binary tree in Haskell? -

i'm trying solve similar problem (find shortest list in tree of lists) , think solving problem start.

given info type like

data (ord a, eq a) => tree = nil | node (tree a) (tree a)

how find node holds minimum element in binary tree above? please not not binary search tree.

i'm trying think recursively: minimum minimum between left, right subtrees , current value. however, i'm struggling translate haskell code. 1 of problems facing want homecoming tree , not value.

here's hint:

you can start defining, auxiliary function, minimum between 2 trees. nodes compared according ther value, ignoring subtrees, , comparing nil tree t makes t minimum (in sense, think of nil "largest" tree). coding can done cases:

binarymin :: ord => tree -> tree -> tree binarymin nil t = t binarymin t nil = t binarymin (node l1 v1 r1) (node l2 v2 r2) = ???

then, minimum subtree follows recursion, exploiting binarymin:

treemin :: ord => tree -> tree treemin nil = nil treemin (node left value right) = ??? l = treemin left r = treemin right

haskell tree

No comments:

Post a Comment