% Author: Dr. Loizos Michael
% Date: 28/10/2008

% elegkse ean to deftero orisma einai fylo sto dentro.

is_leaf(tree(E, []), E).  % to dentro exei mono riza, h opoia epomenos einai fyllo.

is_leaf(tree(E, C), X) :-
        C = [_|_],   % to dentro exei riza me paidia, ara h riza den einai fyllo
        member(T, C),   % dialekse ena paidi-dentro,
        is_leaf(T, X).   % kai elegkse anadromika ean to X einai fyllo aftou tou dentrou.


% elegkse ean kapoio monopati exei dyo fores to idio stoixeio.

dup_path(T) :- dup_path_aux(T, []). % dimiourgise mia lista opou tha kratounte ta
% stoixeia pou exoun emfanistei se komvous metaksi tis rizas mexri tou komvou pou
% eksetazete ekeini tin stigmi.

dup_path_aux(tree(E, C), V) :- member(E, V). % To stoixeio exei ksana-emfanistei.

dup_path_aux(tree(E, C), V) :-
        \+ member(E, V),
        member(T, C), % dialekse ena paidi tou komvou
        dup_path_aux(T, [E | V]). % kai elekse anadromika ean to T periexei kapoio monopati sto
% opio na emfanizete stoixeio pou exoume idi entopisei.


% epestrepse ena monopati tou dentrou.

get_path(tree(E, []), [E]).
get_path(tree(E, C), [E | P]) :-
        member(T, C),
        get_path(T, P).