Answer the question
In order to leave comments, you need to log in
Ternary functors in Prolog, how to declare them?
Binary trees are defined using the ternary functor tree(Left,Root,Right), where Root is the element at the top, and Left and Right are the left and right subtrees, respectively. The empty tree is represented by the nil atom. The following term is an example of a more complex tree tree(nil, 5n, tree(nil, 6, tree(tree(nil, 8, nil), 10, nil))).
Write a predicate p(+T,+X,+Y,–R) that, from a binary tree T, makes a binary tree R that matches T, except that the nodes of the tree that also contain X in the list are changed to the corresponding ones (by order) vertices from list Y.
Example,
<b>?– p(tree(nil, 5, tree(nil, 6, tree(tree(nil, 8, nil), 10, nil))),[5,8,7],[50,80,70],R).
R= tree(nil, 50, tree(nil, 6, tree(tree(nil, 80, nil), 10, nil)))
Yes</b>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question