【问题标题】:Height of a tree - PROLOG树的高度 - PROLOG
【发布时间】:2016-01-04 21:06:04
【问题描述】:

我尝试在 Prolog 中编写一个谓词来查找树的高度。

我的树是这样表示的:

             A
           /    \
          B      C
         / \    / \
        D   E  F   G

[a,[b,[d,e],c[f,g]]] ([Root,[Children]])

我的谓词是:

height(Tr,0) :- 
   atomic(Tr).
height([L|R],N) :- 
   height(L,N1),
   height(R,N2), 
   N is max(N1,N2)+1. 

但我的代码不起作用。当我写:

height([a,[b,[d,e],c,[f,g]]],N).

N 等于 8。

我可以帮忙吗?

注意:根的高度从 0 开始。

【问题讨论】:

  • 不适用于哪个维度?
  • 您的树表示看起来不一致,并且您拥有的代码不会匹配有效的树(无论它看起来像什么 - 很难分辨它是什么)。例如,[c,e,f] 看起来不像基于您的图片的有效子树。如果这不是您的树的样子,请展示您测试的实际树并解释您的意思,代码不起作用
  • 我认为,首先,您需要定义一致的树表示。 [a,[b,d],[c,e,f]](3 个元素的列表)不能完全描述您所描绘的树,并且与您给出的 [Root, [Children]](2 个元素的列表)的一般模式不匹配。一旦有了一致的表示,代码就会变得简单明了。
  • 对不起,我犯了一个错误......现在我有一个一致的树表示,但高度不好:/
  • 您的新树表示仍然不一致。如果[a,[b,[d,e],c,[f,g]]] 是二叉树,那么内部列表[b, [d,e], c, [f,g]] 的元素太多。表示应该有一个清晰的头部,左,右结构,然后谓词可以遵循该结构。现在,您的谓词有[L|R],其中L 是列表headRtail(列表的其余部分),它没有t 似乎是对的。人们会期望在实现中左右对称。

标签: tree prolog height binary-tree


【解决方案1】:

这有助于找到正确的抽象。

给定一个使用这些约定表示的二叉树

  • 空树用原子nil表示。
  • 非空树由结构tree/3表示,其中
    • 第一个参数是节点的负载,
    • 第二个参数是左子树(有效负载排序为小于当前节点的节点),
    • 第三个参数是右子树(其有效负载排序为大于当前节点的节点)

解决方法很简单:

tree_depth( nil         , 0 ) .   % the depth of an empty tree is 0.
tree_depth( tree(_,L,R) , D ) :-  % the depth of a non-empty tree is computed thus:
  tree_depth(L,L1) ,              % - compute the depth of the left subtree
  tree_depth(R,R1) ,              % - compute the depth of the right subtree
  D is 1 + max(L1,R1)             % - the overall depth is 1 more than the maximum depth in both subtrees.
  .                               %

计算 n 叉树 的深度,其中每个节点可以有任意数量的子节点,并不复杂。我们将这样表示我们的 n-ary 树

  • 空树再次用原子nil表示。
  • 非空树由结构 tree/2 表示,其中
    • 第一个参数是节点的负载
    • 第二个参数是一个包含节点子树的列表(其中任何一个都可能是nil)。

解决方案也很简单:

tree_depth( nil       , 0 ) .   % the depth of the empty tree is 0.
tree_depth( tree(_,C) , D ) :-  % the depth of a non-empty tree is computed thus:
  subtree_depth( C , 0 , T ) ,  % - compute the depth of the subtrees of the current node
  D is 1+T                      % - add 1 to that
  .                             %

subtree_depth( []     , D , D ) .   % child subtrees exhausted? unify the accumulator with the result
subtree_depth( [X|Xs] , T , D ) :-  % otherwise...
  tree_depth(X,X1) ,                % - compute the depth of the current subtree
  T1 is max(T,X1) ,                 % - set the accumulator the max value
  subtree_depth( Xs , T1 , D )      % - recurse down on the tail.
  .

【讨论】:

    【解决方案2】:

    您的查询似乎不代表一棵有效的树,它应该总是具有[_, SubList] 的形状。这个 sn-p 假设了这样的表示,以及库的可用性(aggregate):

    height([_,Sub], Height) :- !,
        aggregate(max(H + 1), S^(member(S, Sub), height(S, H)), Height).
    height(_, 0).
    

    产量

    ?- height([a,[ [b,[d,e]], [c,[f,g]] ]],N).
    N = 2.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多