Advertisement
esterfarbstein

Question 1

Dec 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.13 KB | None | 0 0
  1. %% all the changes in this question were on the same issue - I did not
  2. %% remember how to divide numbers and I wrote "a/b" in the test because
  3. %% I wanted the teacher to understand my code
  4. build_balanced_tree(1,t(nil,x,nil)):-!.
  5. build_balanced_tree(0,nil):-!.
  6. build_balanced_tree(N,t(L,x,R)):-
  7.     N1 is N-1,
  8.     N2 is N-2,
  9.     %% I added these variables, and put the "div"s here and not in the lines below
  10.     N3 is div(N1,2),
  11.     N4 is div(N2,2),
  12.     N5 is N4+1,
  13.     (   N6 is mod(N1,2),N6==0,!, %% I changed this line
  14.         build_balanced_tree(N3,L),build_balanced_tree(N3,R)
  15.     ;
  16.        (   build_balanced_tree(N4,L),build_balanced_tree(N5,R)
  17.        ;
  18.            build_balanced_tree(N5,L),build_balanced_tree(N4,R)
  19.     )).
  20.  
  21. mirror(nil,nil).
  22. mirror(t(L,x,R),t(NewL,x,NewR)):-
  23.        mirror(L,NewR),
  24.        mirror(R,NewL).
  25.  
  26. symetric_and_balanced(N,t(L,x,R)):-
  27.     N1 is N-1,
  28.     N2 is div(N1,2),%% I added this line
  29.     build_balanced_tree(N2,L),
  30.     mirror(L,R).
  31.  
  32. symetric_balanced_trees(N,Ts):-
  33.     N1 is mod(N,2),N1==1, %% I changed this line
  34.     N>0,
  35.     findall(T,symetric_and_balanced(N,T),Ts),
  36.     Ts\=[].
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement