Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% all the changes in this question were on the same issue - I did not
- %% remember how to divide numbers and I wrote "a/b" in the test because
- %% I wanted the teacher to understand my code
- build_balanced_tree(1,t(nil,x,nil)):-!.
- build_balanced_tree(0,nil):-!.
- build_balanced_tree(N,t(L,x,R)):-
- N1 is N-1,
- N2 is N-2,
- %% I added these variables, and put the "div"s here and not in the lines below
- N3 is div(N1,2),
- N4 is div(N2,2),
- N5 is N4+1,
- ( N6 is mod(N1,2),N6==0,!, %% I changed this line
- build_balanced_tree(N3,L),build_balanced_tree(N3,R)
- ;
- ( build_balanced_tree(N4,L),build_balanced_tree(N5,R)
- ;
- build_balanced_tree(N5,L),build_balanced_tree(N4,R)
- )).
- mirror(nil,nil).
- mirror(t(L,x,R),t(NewL,x,NewR)):-
- mirror(L,NewR),
- mirror(R,NewL).
- symetric_and_balanced(N,t(L,x,R)):-
- N1 is N-1,
- N2 is div(N1,2),%% I added this line
- build_balanced_tree(N2,L),
- mirror(L,R).
- symetric_balanced_trees(N,Ts):-
- N1 is mod(N,2),N1==1, %% I changed this line
- N>0,
- findall(T,symetric_and_balanced(N,T),Ts),
- Ts\=[].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement