Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. add(null,0):-!.
  2. add(tree(X,T1,T2),S):- add(T1,S1),add(T2,S2),S is X+S1+S2.
  3.  
  4. count_leaves(null,0):-!.
  5. count_leaves(tree(_,null,null),1):-!.
  6. count_leaves(tree(_,Left,Right),Count):-count_leaves(Left,Count1),
  7. count_leaves(Right,Count2),Count is Count1 + Count2.
  8.  
  9. compute(tree(V,null,null),V).
  10. compute(tree(+,Left,Right),V):-compute(Left,R1),compute(Right,R2),V is R1+R2.
  11. compute(tree(-,Left,Right),V):-compute(Left,R1),compute(Right,R2),V is R1-R2.
  12. compute(tree(*,Left,Right),V):-compute(Left,R1),compute(Right,R2),V is R1*R2.
  13. compute(tree(/,Left,Right),V):-compute(Left,R1),compute(Right,R2),V is R1/R2.
  14.  
  15. converting(V, tree(V,null,null)):-number(V).
  16. converting(X-Y,tree(-,Left,Right)):-converting(X,Left),converting(Y,Right).
  17. converting(X+Y,tree(+,Left,Right)):-converting(X,Left),converting(Y,Right).
  18. converting(X*Y,tree(*,Left,Right)):-converting(X,Left),converting(Y,Right).
  19. converting(X/Y,tree(/,Left,Right)):-converting(X,Left),converting(Y,Right).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement