Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pb(T, R):-
- pbtNodes(T, T1),
- pbtCheck(T1, R).
- pbtNodes(t(Key, nil, nil), t(Key, nil, nil, [1])).
- pbtNodes(t(Key, nil, R), t(Key, nil, R, [0])).
- pbtNodes(t(Key, L, nil), t(Key, L, nil, [0])).
- pbtNodes(t(Key, L, R), t(Key, t(LKey, LL, LR, LRes), t(RKey, RL, RR, RRes), Res)):-
- pbtNodes(L, t(LKey, LL, LR, LRes)),
- pbtNodes(R, t(RKey, RL, RR, RRes)),
- append(LRes, RRes, Res).
- pbtCheck(nil, []).
- pbtCheck(t(_, nil, nil, _), []).
- pbtCheck(t(_, nil, nil), []).
- pbtCheck(t(Key, L, R, Kids), [Key|Res]):-
- findZero(Kids), !,
- pbtCheck(L, Res1),
- pbtCheck(R, Res2),
- append(Res1, Res2, Res).
- pbtCheck(t(_, L, R, _), Res):-
- pbtCheck(L, Res1),
- pbtCheck(R, Res2),
- append(Res1, Res2, Res).
- findZero([0|_]):-!, fail.
- findZero([]).
- findZero([_|T]):- !,
- findZero(T).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement