prolog need to compute the tree size -
i need size of tree using: size(tree,size)
what have far wrong, please advise!
size(empty, size). size(tree(l, _, r), size) :- size(l, left_size), size(r, right_size), size left_size + right_size + 1.
output should produce:
?- size(node(1,2),x). x = 2. ?- size(node(1,[2,3,4]),x). x = 2. ?- size(node(node(a,b),[2,3,4]),x). x = 3.
prolog declarative language, must state correctly patterns:
size(node(l,r), size) :- ... % why add 1 left+right sizes ?
from samples, suggest stop recursion size = 1 when not node seen:
size(node(l,r), size) :- !, ... size(_, 1).
Comments
Post a Comment