【问题标题】:prolog is tree balancedprolog 是树平衡的
【发布时间】:2015-08-18 23:31:29
【问题描述】:

如果T 是平衡树,我需要实现一个谓词isBalanced/1 使得isBalanced(T) 为真。

在这种情况下,二叉树由结构 node(left,right) 定义,其中 left 和 right 可以是另一个节点或任何 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).

预期输出:

?- isBalanced(1).
true.
?- isBalanced(node(1,2)).
true.
?- isBalanced(node(1,node(1,node(1,2)))).
false.

它不起作用,任何建议将不胜感激!

【问题讨论】:

  • 你有什么问题?
  • ...并添加 l/1 回来!相反,删除这个不正确的剪辑。
  • 在你的first question 中,你有size(empty, Size). 但现在你说height(_,1)。你的意思是说一切都是高度一的吗?你的意思只是height(l(_),1)`。
  • 另请注意,height 假定具有 node/2 术语的树,而 isBalanced/1 假定具有 b/1 术语的树。它们不兼容,因此您的查询将失败或循环(前提是height(_,1) 已更正)。
  • 对!不过b/2node/2 还是不能统一!

标签: tree prolog balance


【解决方案1】:

你如何代表你的树?在我看来

  • l(_) 代表空树,并且
  • node(L,R) 代表一棵非空树。

我怀疑您的height/2 有一个错误,您似乎已将空树的高度定义为 1(而不是 0)。

我可能会如下表示一棵二叉树:

  • nil — 空树
  • tree(D,L,R) — 一棵非空树,其中

    • D:有效载荷数据
    • L: 左子树
    • R: 右子树

这样可以代表树

    a
   / \
  b   c   
 /   / \
d   e   f

作为

tree( a ,
  tree( b ,
    tree( d , nil , nil ) ,
    nil
  ) ,
  tree( c ,
    tree( e , nil , nil ) ,
    tree( f , nil , nil ) 
) .

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

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?

我们只需要计算一棵树的高度。

高度计算

这样一棵树的高度可以计算如下:

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/2tree_height/2可疑的相似之处

因此,可以通过混合两者并即时计算深度来优化事物:

已编辑:添加了包装谓词is_balanced/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!)

【讨论】:

  • 谢谢,我特别喜欢优化版。但是,我需要的是一个只接受一个输入的谓词,还要注意你的答案对于isBalanced(1). 都不返回 true,这是我的任务的要求。
  • 在哪里或如何包含谓词 max()
  • 您不需要包含任何内容:max/2 是一个内置的 ISO 算术函数。您应该可以说X is max(3*2,4*5). 并回复X = 20。但是像( X &gt; Y -&gt; Z = X ; Z = Y ) 这样的东西应该将Z 与XY 中的较大者统一起来。
  • 另外,请参阅我修改后的答案,其中包括一个包装谓词 is_balanced/1
  • 我仍然从您的解决方案中得到错误的答案。它询问我是否想Correct to: "is_balanced(1)"?,如果我选择是,它会返回 false,这不是我需要它为 true 的答案。如果我选择否,是真的吐假人并导致异常! 1 ?- isBalanced(1). Correct to: "is_balanced(1)"? yes false. 2 ?- isBalanced(1). Correct to: "is_balanced(1)"? no ERROR: residue_vars/2: Undefined procedure: isBalanced/1 ERROR: However, there are definitions for: ERROR: is_balanced/1 ERROR: is_balanced/2 Exception: (7) isBalanced(1) ? abort % Execution Aborted 3 ?-
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多