定义过程时未定义的过程

时间:2013-04-05 00:36:54

标签: prolog

我正在尝试定义一个简单的二叉搜索树。它存储在如下列表中:[Key,Left Tree,Right Tree]。 我相信我已经这样做但是当我尝试在现有树上使用bstadd时,我收到以下错误。

?- bstadd(19,[],T1), bstadd(9, T1, T2).
ERROR: bstadd/3: Undefined procedure: right/3
   Exception: (8) right(9, [[], []], _G3233) ?

我已经在第8行用三个语句定义了权利。接下来是我的代码:

% bstadd(Key, Tree, NewTree)
% add the element Key to the tree Tree and return an 
% new tree as NewTree. Element in the left subtree L must be less than Key and 
% elements in the right subtree R must be greater than Key.  This means duplicates 
% are not allowed in the binary search tree. Don’t put print statements in this 
% predicate.

right(Key, [TreeKey|TreeTail], [TreeKey|NewTree]) :- grabtail(KEY, TreeTail, NewTree]).
grabtail(KEY, [TreeKey|_], [TreeKey|NewTree]) :- bstadd(KEY, TreeKey, NewTree).
bstadd(KEY, [], [KEY,[],[]]).
bstadd(KEY, [TreeKey|TreeTail], [TreeKey|NewTree]) :- KEY > TreeKey, grabtail(KEY, TreeTail, NewTree).
bstadd(KEY, [TreeKey|TreeTail], [TreeKey|NewTree]) :- KEY < TreeKey, right(KEY, TreeTail, NewTree).


% inorder(Tree) 
% given a binary search tree Tree perform an inorder traversal of the 
% Tree printing (use print(X) ) the value of each vertex inorder.
inorder([TreeHead|TreeTail]) :- inright(TreeTail), print(TreeHead), intail(TreeTail).
inright([_|TreeTail]) :- intail(TreeTail).
intail([TreeHead|_]) :- inorder(TreeHead).

赞赏任何和所有见解。

1 个答案:

答案 0 :(得分:2)

在加载文件时始终查看系统所说的内容。我确信存在某种语法错误。在您的情况下,right/3最后包含语法错误。有一个无与伦比的结束]

right(Key, [TreeKey|TreeTail], [TreeKey|NewTree]) :- grabtail(KEY, TreeTail, NewTree]).
                                                                                   ^^^