0

I am having a difficult time figuring out how to implement a 2d binary tree using sml

This is what I have so far but I get a 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 i = j then Node(i, left, right) 
        else if i < j then 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 i ^ " "); 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 i = k then Node(i, Addnode(j, root), left, right)
        else if i < k then 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 were originally to test the first part of the binary tree but once I tried to do the 2d part, It created an error with the original AddNode

  • Did you know that `Empty` is already the name of an exception in the `List` Structure? http://sml-family.org/Basis/list.html#SIG:LIST.Empty:EXN Also, why does your `AddNode` Only take `btree`'s that are `Empty` rather than any `btree`? – ben rudgers Oct 10 '14 at 16:15
  • I am kinda new at SML, I could easily program it in c++ or java. I am just having trouble transitioning from OOP to functional programming @benrudgers – UnhinderedLimpidity Oct 10 '14 at 16:18
  • Where did you define `Empty`? Where is `node` defined? What error are you getting? What is the minimum code required to reproduce it? – ben rudgers Oct 10 '14 at 16:21
  • Things that jump out: `btree` is not a value, it's a type. The name `root` appears magically out of nowhere. Also, I would avoid reusing names of type constructors. It gets very confusing. – molbdnilo Oct 14 '14 at 14:59

1 Answers1

0

I guess, You could probably just use the btree datatype to implement the 2d-binary tree. There is no need to define twotree. You would only have to implement the Add2Node function that inserts a 2d point into your btree by comparing the y coordinate or the x coordinate in mutual recursive calls:

fun Add2Node (y, x, tree) = insertY (y, x, tree)

and insertY (y, x, Empty) = Node (y, insertX (y, x, Empty), Empty) 
  | insertY (y, x, Node (k, left, right)) = 
    if y < k then 
      Node (k, insertX (y, x, left), right)
    else
      Node (k, left, insertX (y, x, right))

and insertX (y, x, Empty) = Node (x, Empty, Empty) 
  | insertX (y, x, Node (k, left, right)) =
    if x < k then
      Node (k, insertY (y, x, left), right)
    else
      Node (k, left, insertY (y, x, right))

The given code compiles but it is not tested. Your code for Add2Node does not compile according to some typos (for instance: type annotations have to be placed behind the variable and not before it like in C++ and Java). Furthermore you are using the btree datatype in a pattern matching position. To make it compile you would have to use one of the datatype's constructors Empty or Node here.

gruenewa
  • 1,666
  • 11
  • 16