[英]How to remove an element from a multiway tree in Prolog
My friends and I are quite struggling with this task.我和我的朋友们正在努力完成这项任务。 In any other language this would be easy.在任何其他语言中,这很容易。
We tried converting the tree into a list, remove an element and then building a tree.我们尝试将树转换为列表,删除一个元素,然后构建一棵树。 The build from list to tree building part we have no clue how to do.从列表构建到树构建部分我们不知道该怎么做。
How would I go about it with this tree:我将如何使用这棵树 go :
tree(1, [tree(6, [tree(7, [])] ),
tree(3, []),
tree(2, [tree(4, []),
tree(5, [])])
])
Would be immensely grateful for any nudges or solutions.将非常感谢任何推动或解决方案。
Prolog variables are immutable - their value cannot be changed. Prolog 变量是不可变的——它们的值不能改变。 So, if you want to change a tree say 3-way tree, you have a predicate that state the desired change.所以,如果你想改变一棵树,比如三叉树,你有一个谓词 state 所需的改变。
A simple non-recursif case to replace specific nodes with new values:用新值替换特定节点的简单非递归情况:
replace_left_node(tree(_,B,C), NewNode, tree(NewNode, B, C).
replace_right_node(tree(A,B,_), NewNode, tree(A, B, NewNode).
replace_middle_node(tree(A,_,C), NewNode, tree(A, NewNode, C).
For a recursif case, you have to specify the change in greater detail.对于递归情况,您必须更详细地指定更改。 Say, in a tree with left & right sub trees and node value in the middle argument, replace node values having the value a by the value b, such as:比方说,在一个有左右子树和节点值在中间参数的树中,用值 b 替换具有值 a 的节点值,例如:
replace(a, b). % replace the value a by value b
replace(X, X) :- X \= a. % otherwise use the same value unchanged
The recursive situation is to do the same to the Left and Right sub-trees.递归的情况就是对Left和Right子树做同样的事情。
replace_recursively(terminal, terminal) % this is the tree leaf
replace_recursively(tree(L, V, R), tree(L1, V1, R1)) :-
replace(V, V1), % handle the node value
replace_recursively(L, L1), % handle left sub-tree
replace_recursively(R, R1). % handle right sub-tree
Does this answer your question?这回答了你的问题了吗? What else do you need?你还需要什么?
OK, with the example you gave in the comment, I understand better.好的,通过您在评论中给出的示例,我理解得更好。 I give you an example for removing the node with given value Elm.我给你一个例子,用于删除具有给定值 Elm 的节点。 This is a double recursion, one for handling the functor tree and another for handling lists.这是一个双重递归,一个用于处理函子树,另一个用于处理列表。
remove_elem_from_tree(Elm, tree(V, Nodes), tree(V,Nodes1) ):-
Elm \= V,
remove_elem_from_list(Elm, Nodes, Nodes1).
remove_elem_from_list(_, [], []).
remove_elem_from_list(E, [X|Xs], [Y|Ys]) :-
remove_elem_from_tree(E, X, Y), !,
remove_elem_from_list(E, Xs, Ys).
remove_elem_from_list(E, [_|Xs], Ys) :-
remove_elem_from_list(E, Xs, Ys).
So example run is: remove the node 4所以示例运行是:删除节点 4
?- remove_elem_from_tree(4, tree(1,
[ tree(6, [tree(7, [])]),
tree(3, []),
tree(2, [tree(4, []),tree(5, [])])
]
), Result).
Result = tree(1,[tree(6,[tree(7,[])]),tree(3,[]),tree(2,[tree(5,[])])])
The node 4 was under node 2 in the given tree.节点 4 在给定树中的节点 2 下。 It is removed and node 2 now has the only child node 5.它被删除,节点 2 现在有唯一的子节点 5。
Does this answer help you and your friends?这个答案对您和您的朋友有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.