Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.83 KB | None | 0 0
  1. pb(T, R):-
  2.     pbtNodes(T, T1),
  3.     pbtCheck(T1, R).
  4.  
  5. pbtNodes(t(Key, nil, nil), t(Key, nil, nil, [1])).
  6.  
  7. pbtNodes(t(Key, nil, R), t(Key, nil, R, [0])).
  8.  
  9. pbtNodes(t(Key, L, nil), t(Key, L, nil, [0])).
  10.  
  11. pbtNodes(t(Key, L, R), t(Key, t(LKey, LL, LR, LRes), t(RKey, RL, RR, RRes), Res)):-
  12.     pbtNodes(L, t(LKey, LL, LR, LRes)),
  13.     pbtNodes(R, t(RKey, RL, RR, RRes)),
  14.     append(LRes, RRes, Res).
  15.  
  16. pbtCheck(nil, []).
  17.  
  18. pbtCheck(t(_, nil, nil, _), []).
  19.  
  20. pbtCheck(t(_, nil, nil), []).
  21.  
  22. pbtCheck(t(Key, L, R, Kids), [Key|Res]):-
  23.     findZero(Kids), !,
  24.     pbtCheck(L, Res1),
  25.     pbtCheck(R, Res2),
  26.     append(Res1, Res2, Res).
  27.  
  28. pbtCheck(t(_, L, R, _), Res):-
  29.     pbtCheck(L, Res1),
  30.     pbtCheck(R, Res2),
  31.     append(Res1, Res2, Res).
  32.    
  33. findZero([0|_]):-!, fail.
  34.  
  35. findZero([]).
  36.  
  37. findZero([_|T]):- !,
  38.     findZero(T).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement