在指定深度修剪树.在序言中 [英] Trim the tree at the specified depth. In Prolog
本文介绍了在指定深度修剪树.在序言中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
参数:任意二叉树;必要的深度;结果树.
Arguments: arbitrary binary tree; necessary depth; result tree.
结果:
?- pred(s(f(b(m,k),a),t(a,g)),2,X).
X = s(f,t) yes
?- pred(s(f(b(m,k),a),t(a,g)),3,X).
X = s(f(b,a),t(a,g)) yes
?-
有人可以帮我吗?
推荐答案
让我们重新排列您的示例:
Let's rearrange your examples:
?- pred( s( f(b(m,k),a), t(a,g)), 3, X).
X = s(f(b,a),t(a,g)) yes
?- pred( s( f(b(m,k),a), t(a,g)), 2, X).
X = s(f,t) yes
?- pred( s( f(b(m,k),a), t(a,g)), 1, X).
X = s yes
?- pred( s( f(b(m,k),a), t(a,g)), 0, X).
no
现在很清楚需要做什么,不是吗?
Now it's clear what needs to be done, isn't it?
此难题的另一个组成部分是所谓的"univ"谓词, =..
,
Another piece to this puzzle is the so-called "univ" predicate, =..
,
9 ?- s( f(b(m,k),a), t(a,g)) =.. [A, B, C].
A = s,
B = f(b(m, k), a),
C = t(a, g).
10 ?- X =.. [s, f(b(m,k),a), t(a,g)].
X = s(f(b(m, k), a), t(a, g)).
11 ?- X =.. [s, f(b,a), t(a,g)].
X = s(f(b, a), t(a, g)).
12 ?- X =.. [s, f, t].
X = s(f, t).
13 ?- X =.. [s].
X = s.
14 ?- s =.. X.
X = [s].
这就是您可以拆分数据并再次构建的方法.
That's how you can take apart your data, and build it up again.
最后,您将需要使用递归:
Lastly, you will need to use recursion:
recursion( In, Out) :-
base_relation( In, Out).
recursion( In, Out) :-
constituents( In, SelfSimilarParts, LeftOvers),
maplist( recursion, SelfSimilarParts, InterimResults),
constituents( Out, InterimResults, LeftOvers).
这篇关于在指定深度修剪树.在序言中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文