Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. add(null,0).
  2. add(tree(R,LT,RT),S) :- add(LT,S1),add(RT,S2),S is R+S1+S2.
  3.  
  4. count_leaves(null,0) :- !.
  5. count_leaves(tree(_,null,null),1) :- !.
  6. count_leaves(tree(_,LT,RT),Count) :- count_leaves(LT,Count1),count_leaves(RT,Count2),Count is Count1 +Count2.
  7.  
  8.  
  9. compute(tree(R,null,null),R).
  10. compute(tree(+,LT,RT),S) :-
  11. compute(LT,S1),
  12. compute(RT,S2),
  13. S is S1 + S2.
  14. compute(tree(-,LT,RT),S) :-
  15. compute(LT,S1),
  16. compute(RT,S2),
  17. S is S1 - S2.
  18. compute(tree(*,LT,RT),S) :-
  19. compute(LT,S1),
  20. compute(RT,S2),
  21. S is S1 * S2.
  22. compute(tree(/,LT,RT),S) :-
  23. compute(LT,S1),
  24. compute(RT,S2),
  25. S is S1 / S2.
  26.  
  27.  
  28.  
  29. converting(A+B,tree(+,LT,RT)) :-
  30. converting(A,LT),
  31. converting(B,RT).
  32. converting(A-B,tree(-,LT,RT)) :-
  33. converting(A,LT),
  34. converting(B,RT).
  35. converting(A*B,tree(*,LT,RT)) :-
  36. converting(A,LT),
  37. converting(B,RT).
  38. converting(A/B,tree(/,LT,RT)) :-
  39. converting(A,LT),
  40. converting(B,RT).
  41. converting(E,tree(E,null,null)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement