2D Binary Tree in SML -



2D Binary Tree in SML -

i having hard time figuring out how implement 2d binary tree using sml

this have far tycon mismatch.

datatype btree = empty | node of int * btree * btree; fun addnode (i:int, empty) = node(i, empty, empty) | addnode(i:int, node(j, left, right)) = if = j node(i, left, right) else if < j node(j, addnode(i, left), right) else node(j, left, addnode(i, right)); fun printinorder empty = () | printinorder (node(i,left,right)) = (printinorder left; print(int.tostring ^ " "); printinorder right); datatype twotree = empty | node of int * twotree * twotree * btree; fun add2node(int:i, int:j, empty) = node(i, btree, empty, empty) | add2node(int:i, int:j, node(k, btree, left, right)) = if = k node(i, addnode(j, root), left, right) else if < k node(k, root, add2node(i, j, left), right) else node(k, root, left, add2node(i, j, right)); val x : btree = addnode(50, empty); val x : btree = addnode(75, x); val x : btree = addnode(25, x); printinorder(x);

the val test first part of binary tree 1 time tried 2d part, created error original addnode

i guess, utilize btree datatype implement 2d-binary tree. there no need define twotree. have implement add2node function inserts 2d point btree comparing y coordinate or x coordinate in mutual recursive calls:

fun add2node (y, x, tree) = inserty (y, x, tree) , inserty (y, x, empty) = node (y, insertx (y, x, empty), empty) | inserty (y, x, node (k, left, right)) = if y < k node (k, insertx (y, x, left), right) else node (k, left, insertx (y, x, right)) , insertx (y, x, empty) = node (x, empty, empty) | insertx (y, x, node (k, left, right)) = if x < k node (k, inserty (y, x, left), right) else node (k, left, inserty (y, x, right))

the given code compiles not tested. code add2node not compile according typos (for instance: type annotations have placed behind variable , not before in c++ , java). furthermore using btree datatype in pattern matching position. create compile have utilize 1 of datatype's constructors empty or node here.

2d binary-tree sml smlnj

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -