I need to implement a predicate trav(Tree,List)
that performs a left to
right tree traversal;
Where: Tree is defined by the structure node(left,right), where left and right can be either another node or any Prolog data item. List is the list of leaf node values in the tree.
For example:
?- trav(x,L).
L = [x].
?- trav(node(1,node(2,node(a,b))),L).
L = [1, 2, a, b] .
What I have so far:
trav(tree,[ ]).
trav(node(Left,Rigth), L) :-
trav(Left, L),
trav(Right, L),
append(Left,[Left|Right],L).
is_tree/1
that states what a tree is in you opinion. It is very uncommon, but certainly possible to have nodes without elements. Such nodes typically occur for expression-trees. But otherwise they do not occur. – false