Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % tree(5, tree(3, tree(1, null, null), tree(4, null, null)), tree(7, null, null))
- present(Key,tree(Key,_,_)).
- present(Key,tree(_,LeftSubTree,_)) :-
- present(Key,LeftSubTree).
- present(Key,tree(_,_,RightSubTree)) :-
- present(Key,RightSubTree).
- inserting(Key,null,tree(Key,null,null)).
- inserting(Key,tree(Root,LeftSubTree,RightSubTree), tree(Root,NewLeftSubTree,RightSubTree)) :-
- Key<Root,
- inserting(Key,LeftSubTree,NewLeftSubTree).
- inserting(Key,tree(Root,LeftSubTree,RightSubTree), tree(Root,LeftSubTree,NewRightSubTree)) :-
- Key>Root,
- inserting(Key,RightSubTree,NewRightSubTree).
- % 1
- add(null, 0).
- add(tree(Root, Left, Right), Sum) :-
- add(Left, S1),
- add(Right, S2),
- Sum is Root+S1+S2.
- % 2
- count_leaves(null, 0).
- count_leaves(tree(_,null,null), 1) :- !.
- count_leaves(tree(_,Left,Right),Count) :-
- count_leaves(Left,C1),
- count_leaves(Right,C2),
- Count is C1+C2.
- % 3
- compute(tree(N,null,null), N).
- compute(tree(+,L,R), Rez) :-
- compute(L,R1),
- compute(R,R2),
- Rez is R1+R2.
- compute(tree(-,L,R), Rez) :-
- compute(L,R1),
- compute(R,R2),
- Rez is R1-R2.
- compute(tree(*,L,R), Rez) :-
- compute(L,R1),
- compute(R,R2),
- Rez is R1*R2.
- compute(tree(/,L,R), Rez) :-
- compute(L,R1),
- compute(R,R2),
- Rez is R1/R2.
- % 4
- converting(N, tree(N,null,null)) :-
- number(N).
- converting(A+B, tree(+,L,R)) :-
- converting(A,L),
- converting(B,R).
- converting(A-B, tree(-,L,R)) :-
- converting(A,L),
- converting(B,R).
- converting(A*B, tree(*,L,R)) :-
- converting(A,L),
- converting(B,R).
- converting(A/B, tree(/,L,R)) :-
- converting(A,L),
- converting(B,R).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement