Tree prolog是树平衡的

Tree prolog是树平衡的,tree,prolog,balance,Tree,Prolog,Balance,我需要实现一个谓词isBalanced/1,如果T是一个平衡的树,那么isBalanced(T)就是true 在这种情况下,二叉树由结构节点(左、右)定义,其中左、右可以是另一个节点或任何Prolog数据项 到目前为止,我所拥有的: height(node(L,R), Size) :- height(L, Left_Size), height(R, Right_Size), Size is Left_Size + Right_Size + 1 . height(l(_),

我需要实现一个谓词
isBalanced/1
,如果
T
是一个平衡的树,那么
isBalanced(T)
就是true

在这种情况下,二叉树由结构节点(左、右)定义,其中左、右可以是另一个节点或任何Prolog数据项

到目前为止,我所拥有的:

height(node(L,R), Size) :-
    height(L, Left_Size),
    height(R, Right_Size),
    Size is Left_Size + Right_Size + 1 .
height(l(_),1).

isBalanced(l(_)).
isBalanced(node(B1,B2)):-
    height(B1,H1),
    height(B2,H2),
    abs(H1-H2) =< 1,
    isBalanced(B1),
    isBalanced(B2).
它不起作用,任何建议都将不胜感激

您如何表示您的树?在我看来

  • l()
    表示空树,并且
  • 节点(L,R)
    表示非空树
我怀疑您的
height/2
有一个缺陷,因为您似乎将空树的高度定义为1(而不是0)

我可能会表示一个二叉树,如下所示:

tree_height( nil         , 0 ) .  % the depth of an empty tree is zero.
tree_height( tree(_,L,R) , H ) :- % for a non-empty tree...
  tree_height( L , LH ) ,         % - we compute the height of the left subtree
  tree_height( R , RH ) ,         % - we compute the height of the right subtree
  H is 1 + max( LH , RH )         % - the overall height is 1 more than the higher of the two values thus obtained.
  .                               % Right?
  • nil
    -空树
  • 树(D,L,R)
    -非空树,其中

    • D
      :有效负载数据
    • L
      :左子树
    • R
      :右子树
这样一个人就可以代表这棵树

    a
   / \
  b   c   
 /   / \
d   e   f
作为

叶节点(没有子树的树)看起来像

tree( data , nil , nil )
余额的确定

因此,从这个表示和定义出发

二叉树是平衡的,如果:

  • 它的左子树是平衡的
  • 它的右子树是平衡的
  • 各子树的高度相差不超过1
我们可以很容易地为这个问题编写一个描述性的解决方案:

is_balanced( nil         ) .  % the empty tree is balanced
is_balanced( tree(_,L,R) ) :- % a non-empty tree is balanced IF ...
  is_balanced(L) ,            % - the left sub-tree is balanced
  is_balanced(R) ,            % - the right sub-tree is balanced
  tree_height(L,LH) ,         % - the height of the left sub-tree
  tree_height(R,RH) ,         % - the height of the right sub-tree
  abs( LH - RH ) < 2          % - differ by no more than 1
  .                           % Right?
效率

有人可能会注意到

  • 似乎发生了很多树遍历,并且
  • 是否平衡/2
    树高/2
    有可疑的相似之处
因此,可以通过将两者混合并动态计算深度来优化事物:

编辑:添加的包装谓词
是平衡的/1

is_balanced( T ) :- is_balanced( T, _ ) .

is_balanced( nil         , 0 ) .   % the empty tree is balanced and has a height of zero.
is_balanced( tree(_,L,R) , H ) :-  % a non-empty tree is balanced IF ...
  is_balanced( L , LH ) ,          % - its left subtree is balanced, and
  is_balanced( R , RH ) ,          % - its right subtree is balanced, and 
  abs( LH - RH) < 2 ,              % - their respective heights differ by no more than 1, and
  H is 1 + max( LH , RH )          % - the current tree's height is 1 more than the larger of the heights of the two subtrees.
  .                                % Easy! (And elegant!)
is_balanced(T):-is_balanced(T,T)。
是平衡的(零,0)。%空树是平衡的,高度为零。
是否平衡(树(u,L,R),H):-%如果。。。
_平衡(L,LH),%-其左子树平衡,且
_平衡(R,RH),%-其右子树平衡,且
abs(左-右)<2,%-它们各自的高度相差不超过1,以及
H为1+最大值(左侧,右侧)%-当前树的高度比两个子树的高度中较大者高1。
.                                % 容易的!(而且优雅!)

您的问题是什么?。。。再加上1号信用证!取而代之的是,删除这个不正确的切口。在您的示例中,您有
大小(空,大小)。
但现在您说的是
高度(\u1)
。你的意思是说所有的东西都有一个高度吗?你的意思只是
height(l(_),1
)`。还要注意
height
假设一棵树具有
node/2
术语,而
isBalanced/1
假设一棵树具有
b/1
术语。它们是不兼容的,因此您的查询将失败或循环(前提是
height(\u1)
已更正)。对!但是
b/2
node/2
只是没有统一!谢谢,我特别喜欢优化版。但是,我需要的是一个只接受一个输入的谓词,还要注意,对于
isBalanced(1)。
,这是我的任务的要求。我在哪里或如何包含谓词max(),您不需要包含任何内容:是一个内置的ISO算术函数。您应该能够说
X是最大值(3*2,4*5)。
然后返回
X=20
。但是像
(X>Y->Z=X;Z=Y)
之类的东西应该将Z与
X
Y
中的较大者统一起来。另外,请参见我的修订答案,其中包括一个包装谓词
是平衡的/1
。我仍然从您的解决方案中得到了错误的答案。它询问我是否希望
更正为:“is_balanced(1)”?
,如果我选择yes,它将返回false,这不是我需要它为true的答案。如果我选择“否”,它会吐出虚拟对象并导致异常<代码>1?-不平衡(1)。更正为:“U平衡(1)”吗?是的,错了。2?-不平衡(1)。更正为:“U平衡(1)”吗?无错误:剩余值\u变量/2:未定义的过程:isBalanced/1错误:但是,有以下定义:错误:is\u balanced/1错误:is\u balanced/2异常:(7)isBalanced(1)?中止%执行中止3?-
tree_height( nil         , 0 ) .  % the depth of an empty tree is zero.
tree_height( tree(_,L,R) , H ) :- % for a non-empty tree...
  tree_height( L , LH ) ,         % - we compute the height of the left subtree
  tree_height( R , RH ) ,         % - we compute the height of the right subtree
  H is 1 + max( LH , RH )         % - the overall height is 1 more than the higher of the two values thus obtained.
  .                               % Right?
is_balanced( T ) :- is_balanced( T, _ ) .

is_balanced( nil         , 0 ) .   % the empty tree is balanced and has a height of zero.
is_balanced( tree(_,L,R) , H ) :-  % a non-empty tree is balanced IF ...
  is_balanced( L , LH ) ,          % - its left subtree is balanced, and
  is_balanced( R , RH ) ,          % - its right subtree is balanced, and 
  abs( LH - RH) < 2 ,              % - their respective heights differ by no more than 1, and
  H is 1 + max( LH , RH )          % - the current tree's height is 1 more than the larger of the heights of the two subtrees.
  .                                % Easy! (And elegant!)