Advertisement
szilard-dobai

BIA - L4

Mar 30th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. % tree(5, tree(3, tree(1, null, null), tree(4, null, null)), tree(7, null, null))
  2.  
  3. present(Key,tree(Key,_,_)).
  4. present(Key,tree(_,LeftSubTree,_)) :-
  5. present(Key,LeftSubTree).
  6. present(Key,tree(_,_,RightSubTree)) :-
  7. present(Key,RightSubTree).
  8.  
  9. inserting(Key,null,tree(Key,null,null)).
  10. inserting(Key,tree(Root,LeftSubTree,RightSubTree), tree(Root,NewLeftSubTree,RightSubTree)) :-
  11. Key<Root,
  12. inserting(Key,LeftSubTree,NewLeftSubTree).
  13. inserting(Key,tree(Root,LeftSubTree,RightSubTree), tree(Root,LeftSubTree,NewRightSubTree)) :-
  14. Key>Root,
  15. inserting(Key,RightSubTree,NewRightSubTree).
  16.  
  17. % 1
  18. add(null, 0).
  19. add(tree(Root, Left, Right), Sum) :-
  20. add(Left, S1),
  21. add(Right, S2),
  22. Sum is Root+S1+S2.
  23.  
  24. % 2
  25. count_leaves(null, 0).
  26. count_leaves(tree(_,null,null), 1) :- !.
  27. count_leaves(tree(_,Left,Right),Count) :-
  28. count_leaves(Left,C1),
  29. count_leaves(Right,C2),
  30. Count is C1+C2.
  31.  
  32. % 3
  33. compute(tree(N,null,null), N).
  34. compute(tree(+,L,R), Rez) :-
  35. compute(L,R1),
  36. compute(R,R2),
  37. Rez is R1+R2.
  38. compute(tree(-,L,R), Rez) :-
  39. compute(L,R1),
  40. compute(R,R2),
  41. Rez is R1-R2.
  42. compute(tree(*,L,R), Rez) :-
  43. compute(L,R1),
  44. compute(R,R2),
  45. Rez is R1*R2.
  46. compute(tree(/,L,R), Rez) :-
  47. compute(L,R1),
  48. compute(R,R2),
  49. Rez is R1/R2.
  50.  
  51. % 4
  52. converting(N, tree(N,null,null)) :-
  53. number(N).
  54. converting(A+B, tree(+,L,R)) :-
  55. converting(A,L),
  56. converting(B,R).
  57. converting(A-B, tree(-,L,R)) :-
  58. converting(A,L),
  59. converting(B,R).
  60. converting(A*B, tree(*,L,R)) :-
  61. converting(A,L),
  62. converting(B,R).
  63. converting(A/B, tree(/,L,R)) :-
  64. converting(A,L),
  65. converting(B,R).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement