1

我正在尝试定义一个简单的二叉搜索树。它存储在这样的列表中:[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).

任何和所有的见解都是值得赞赏的。

4

1 回答 1

2

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

right(Key, [TreeKey|TreeTail], [TreeKey|NewTree]) :- grabtail(KEY, TreeTail, NewTree]).
                                                                                   ^^^
于 2013-04-05T00:54:33.087 回答