将列表解析为ML中的bst

时间:2014-07-21 04:18:44

标签: list binary-search-tree ml

注意类似的问题可以在以下位置找到: standard ml make bst out of a list

我有一个列表,我从该部分加载,其中的变量称为测试,我测试了它,它的工作原理。

use "mllistr.txt";
datatype 'a BST = Nil | Node of 'a * 'a BST * 'a BST;

fun leftchild (Node(data,left,right)) = left
 | leftchild Nil = raise Empty;
fun rightchild (Node(data,left,right)) = right
 | rightchild Nil = raise Empty;
fun insert (x, Nil) = Node(x, Nil, Nil)
 |insert (x, Node(y, left, right) ) =
        if x<y then Node(y, insert(x,left), right)
        else Node(y, left, insert(x, right) );

(*test this var is the tree unmade but as a list*)
fun makeTree ([], bst) = Empty
 | makeTree ((x::xs), bst) = insert (x) (makeTree xs bst);

我已经为上面的代码行尝试了一堆格式,这就是当前迭代尝试修复问题的失败。

在我尝试输入代码之前:

makeTree test Empty

我知道它会因为这个错误而失败:

Error: operator and operand don't agree [tycon mismatch]
operator domain: (int * int BST) list * 'Z
operand:         (int * int BST) list
in expression:
    makeTree xs
stdIn:19.29-19.57 Error: operator is not a function [tycon mismatch]
  operator: int BST
  in expression:
    (insert x) ((makeTree xs) bst)

任何帮助都会非常感激,我仍然不太了解那个

0 个答案:

没有答案