Prolog - 检查二叉树是否有序
我想在 Prolog 中编写一个程序来确认整数 b 树是否有序。顺序是从小到大。 这是我到目前为止所写的,但我还没有达成任何扎实的工作。有人知道该怎么做吗?
Domains
element=integer
tree=a(tree,element,tree);void
Predicates
nondeterm ordre(tree)
Clauses
order(a(_,Node,a(_,Node2,_))):-Node<Node2.
order(a(Esq,Node,Dre)) :-
order(Esq),
write(Node),nl,
order(Dre).
Goal
order(a(a(void,1,void),2,a(a(void,3,void),4,a(void,6,void)))).
非常感谢。
I want to write a program in Prolog that confirms if a b-tree of integers is ordered or not. The order goes from smaller to greater.
This is what I've written so far but I do not reach any solid work. Does someone know how to do that?
Domains
element=integer
tree=a(tree,element,tree);void
Predicates
nondeterm ordre(tree)
Clauses
order(a(_,Node,a(_,Node2,_))):-Node<Node2.
order(a(Esq,Node,Dre)) :-
order(Esq),
write(Node),nl,
order(Dre).
Goal
order(a(a(void,1,void),2,a(a(void,3,void),4,a(void,6,void)))).
Huge Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果:是
Result: yes
使用与之前相同的树结构(原子
btree
表示空树;结构btree(Key,Left,Right)
表示非空树,类似于这应该对你有用:空树按定义排序
非空树是有序的,如果
它的右子节点是有序的,并且它们的键大于当前节点的键
根据定义,空树小于任何指定的键值,也大于任何指定的键值。< /p>
非空树小于指定的键值,如果
它本身是有序的
非空树大于指定的键值,如果
它本身是有序的
Using the same same tree structure as before (the atom
btree
denoting an empty tree; the structurebtree(Key,Left,Right)
denoting a non-empty tree, something like this should do you:An empty tree is ordered by definition
A non-empty tree is ordered if
its right children are ordered and their keys are greater than that of the current node
By definition, an empty tree is less than any specified key value and also greater than any specified key value.
A non-empty tree is less than a specified key value if
it is itself ordered
A non-empty tree is greater than a specified key value if
it is itself ordered