Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 29.25 KB | None | 0 0
  1. fact(0,1).
  2. fact(X,R) :- X1 is X-1, fact(X1,R1), R is R1*X.
  3.  
  4. fact1(X,R) :- fact1(X,1,R).
  5. fact1(0, Acc, Acc).
  6. fact1(X,Acc,R) :- X>0, Acc1 is Acc*X, X1 is X-1, fact1(X1,Acc1,R).
  7.  
  8. minim([H|L],R):- minim(L,R),R<H,!. 
  9. minim([H|_],H).
  10.  
  11. deletef(H,[H|T],T).
  12. deletef(X,[H|T],[H|R]) :- deletef(X,T,R).
  13. deletef(_,[],[]).
  14.  
  15. deleteall(X,[X|T],R) :- delteall(X,T,R).
  16. deleteall(X,[H|T],[H|R]) :- deleteall(X,T,R).
  17. deleteall(_,[],[]).
  18.  
  19. add_first(X,L,[X|L]).
  20.  
  21. member(X,[X|_]).
  22. member(X,[_|T]) :- member(X,T).
  23.  
  24. remove_duplicates([],[]).
  25. remove_duplicates([H|T],R) :- member(H,T), remove_duplicates(T,R).
  26. remove_duplicates([H|T],[H|R]) :- remove_duplicates(T,R).
  27.  
  28. inters([],_,[]).
  29. inters([H|T], L2, [H|R]) :- member(H,L2), !, inters(T,L2,R).
  30. inters([_|T], L2, R) :- inters(T,L2,R).
  31.  
  32. sel_sort(L, [M|R]) :- minim(L,M), deletef(M,L,L1), sel_sort(L1,R).
  33. sel_sort([],[]).
  34.  
  35. insert_ord(X, [H|T], [H|R]):- X>H, !, insert_ord(X,T,R).
  36. insert_ord(X,T,[X|T]).
  37.  
  38. ins_sort([H|T], R) :- ins_sort(T,R1), insert_ord(H,R1,R).
  39. ins_sort([],[]).
  40.  
  41. max(X,Y,X) :- X>Y.
  42. max(_,Y,Y).
  43.  
  44. depth([],1).
  45. depth([H|T],R) :- atomic(H), !, depth(T,R).
  46. depth([H|T],R) :- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
  47.  
  48. append2([],L2,L2).
  49. append2([H|T],L2,[H|R]):- append2(T,L2,R).
  50.  
  51. append3(L1,L2,L3,R) :- append2(L2,L3,R1), append2(L1,R1,R).
  52.  
  53. memberdeep(H,[H|_]).
  54. memberdeep(X,[H|_]) :- memberdeep(X,H).
  55. memberdeep(X,[_|T]) :- memberdeep(X,T).
  56.  
  57. flatten([],[]).
  58. flatten([H|T],[H|R]) :- atomic(H), !, flatten(T,R).
  59. flatten([H|T],R):- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
  60.  
  61. tree1(t(6, t(4,t(2,nil,nil),t(5,nil,nil)), t(9,t(7,nil,nil),nil))).
  62. tree2(t(8, t(5, nil, t(7, nil, nil)), t(9, nil, t(11, nil, nil)))).
  63.  
  64. inorder(t(K,L,R), List) :- inorder(L,LL), inorder(R,LR), append2(LL, [K|LR], List).
  65. inorder(nil, []).
  66.  
  67. preorder(t(K,L,R),List) :- preorder(L,LL), preorder(R,LR), append2([K|LL], LR, List).
  68. preorder(nil, []).
  69.  
  70. postorder(t(K,L,R),List) :- postorder(L,LL), postorder(R,LR), append2(LL,LR,R1), append2(R1,[K],List).
  71. postorder(nil, []).
  72.  
  73. searchkey(Key, t(Key,_,_)) :- !.
  74. searchkey(Key, t(K,L,_)) :- Key<K, !, searchkey(Key,L).
  75. searchkey(Key, t(K,_,R)) :- searchkey(Key,R).
  76.  
  77. insertkey(Key,nil,t(Key,nil,nil)).
  78. insertkey(Key,t(Key,L,R),t(Key,L,R)):-!.
  79. insertkey(Key,t(Key,L,R),t(Key,NL,R)) :- Key<K, !, insertkey(Key,L,NL).
  80. insertkey(Key,t(Key,L,R),t(Key,L,NR)) :- insertkey(Key,R,NR).
  81.  
  82. height(nil,0).
  83. height(t(_,L,R),H) :- height(L,H1), height(R,H2), max(H1,H2,H3), H is H3+1;
  84.  
  85. memberil(_,L) ;- var(L), !, fail.
  86. memberil(X,[X|_]) :- !.
  87. memberil(X,[_|T]) :- memberil(X,T).
  88.  
  89. insertil(X,L) :- var(L), !, L=[X|_].
  90. insertil(X, [X|_]) :- !.
  91. insertil(X, [_|T]) :- inseril(X,T).
  92.  
  93. deleteil(X,L,L) :- var(L), !.
  94. deleteil(X,[X|T],R) :- !.
  95. deleteil(X,[H|T],[H|R]) :- deleteil(X,T,R).
  96.  
  97. appendil(L1,L2,L2) :- var(L1), !.
  98. appendil([H|L1],L2,[H|R]) :- appendil(L1,L2,R).
  99.  
  100. convertI2C(L1,[]) :- var(L1), !.
  101. convertI2C([H|L1],[H|R]) :- convertI2C(L1,R).
  102.  
  103. convertC2I([],_) :- !.
  104. convertC2I([H|T],[H|R]) :- convertC2I(T,R).
  105.  
  106.  
  107. % 1. Stergerea ultimului elem. dintr-o lista incompleta
  108. sterg_ultim_il([X|T],T):-var(T),!.
  109. sterg_ultim_il([X|T],[X|R]):- sterg_ultim_il(T,R).
  110.          % Apel: sterg_ultim_il([1,2,3|_],R).  
  111.  
  112. % 2. Stergerea ultimului elem. dintr-o lista completa
  113. sterg_ultim([X|[]],[]):-!.
  114. sterg_ultim([X|T],[X|R]):- sterg_ultim(T,R).
  115.  
  116. sterg_ultim1([X],[]):-!.
  117. sterg_ultim1([X|T],[X|R]):- sterg_ultim1(T,R).
  118.          % Apel: sterg_ultim1([1,2,3],R).
  119.  
  120. % 3.Stergerea ultimului element dintr-o lista diferenta
  121. sterg_ultim_dl([F],L,RL,RL):-!.
  122. sterg_ultim_dl([X|T],L,[X|RF],RL):- sterg_ultim_dl(T,L,RF,RL).
  123.          % Apel: sterg_ultim_dl([1,2,3|LE],LE,RF,RL).
  124.          
  125. % 4. Concatenarea a doua liste diferenta
  126. append_dl([],L1,F2,L2,F2,L2):-!.
  127. append_dl([H|L],L1,F2,L2,[H|RF],RL):-append_dl(L,L1,F2,L2,RF,RL).
  128.          % Apel: append_dl([1,2,3|L1],L1,[4,5|L2],L2,RF,RL).
  129.  
  130. % 5. Concatenare liste incomplete
  131. append_il(L1,L2,L2):-var(L1),!.
  132. append_il([H|T],L2,[H|R]):-append_il(T,L2,R).
  133.          % Apel: append_il([1,2,3|_],[4,5|_],R).
  134.          
  135. % 6. Lista completa in lista diferenta
  136. cl_dl([],Last,Last):-!.
  137. cl_dl([H|T],[H|First],Last):-cl_dl(T,First,Last).
  138.          % Apel: cl_dl([1,2,3,4],F,L).
  139.        
  140. % 7. Lista completa in lista incompleta
  141. cl_il([],_):-!.
  142. cl_il([H|T],[H|R]):-cl_il(T,R).
  143.          % Apel: cl_il([1,2,3],R).
  144.          
  145. % 8. Lista diferenta in lista completa
  146. dl_cl([],L,[]):-!.
  147. dl_cl([X|T],L,[X|R]):- dl_cl(T,L,R).
  148.          % Apel: dl_cl([1,2,3|L],L,R).
  149.  
  150. % 9. Lista incompleta in lista completa
  151. il_cl(T,[]):-var(T),!.
  152. il_cl([X|T],[X|R]):- il_cl(T,R).
  153.          % Apel: il_cl([1,2,3|_],R).
  154.          
  155. % 10. Lista incompleta in lista diferenta
  156. il_dl(T,L,L):-var(T),!.
  157. il_dl([X|T],[X|F],L):- il_dl(T,F,L).
  158.          % Apel: il_dl([1,2,3|_],F,L).
  159.  
  160. % 11. Lista diferenta in lista incompleta
  161. dl_il([],L,_):-!.
  162. dl_il([X|T],L,[X|R]):- dl_il(T,L,R).
  163.          % Apel: dl_il([1,2,3|L],L,R).
  164.          
  165. % 12. Inversarea unei liste incomplete.
  166. invers_il(T,_):-var(T),!.
  167. invers_il([H|T],R):-invers_il(T,R1),append_il(R1,[H|_],R).
  168.  
  169. % 13. Inversarea unei liste diferenta.
  170. invers_dl([],L,RL,RL):-!.
  171. invers_dl([H|T],L,RF,RL):-invers_dl(T,L,F1,L1),append_dl(F1,L1,[H|PL],PL,RF,RL).
  172.          % Apel: invers_dl([1,2,3|L],L,RF,RL).
  173.  
  174. % 14. Inversarea unei liste complete.
  175. invers([],[]).
  176. invers([H|T],R):-invers(T,R1),append(R1,[H],R).
  177.  
  178. % 15. Concatenarea a doua liste complete
  179. append_cl([],L2,L2):-!.
  180. append_cl([H|T],L2,[H|R]):-append_cl(T,L2,R).
  181.  
  182. % 16. Gasirea elementului max dintr-o lista cu recursivitate inapoi
  183. max_bw([H],H).
  184. max_bw([H|T],H):-max_bw(T,M),H>M,!.
  185. max_bw([H|T],M):-max_bw(T,M).
  186.  
  187. % 17. Gasirea elementului max dintr-o lista cu recursivitate inainte
  188. max_fw([],A,A).
  189. max_fw([H|T],A,R):-H>A,!,max_fw(T,H,R).
  190. max_fw([H|T],A,R):-max_fw(T,A,R).
  191.  
  192. max_fw([H|T],R):-max_fw(T,H,R).
  193.  
  194. % 18. Se da o lista. Sa se descomp in 2 liste dupa un pivot P dat.
  195. pivot([],_,[],[]).
  196. pivot([H|T],P,[H|Mic],Mare):-H<P,!, pivot(T,P,Mic,Mare).
  197. pivot([H|T],P,Mic,[H|Mare]):-pivot(T,P,Mic,Mare).
  198.  
  199. % 19. Folosind for cu decrementare sa se genereze o lista de genul [N,...,1] cu N dat
  200. for_dec(0,[]).
  201. for_dec(Index,[Index|Out]):-Index>0, NewIndex is Index -1, for_dec(NewIndex,Out).
  202.  
  203. % 20. append3 - versiune eficienta
  204. append3(L1,L2,L3,R):-append(L2,L3,I), append(L1,I,R).
  205.  
  206. % 21. Inlocuirea primei aparitii a unui elelemnt U cu un alt element N dintr-o lista completa
  207. replace_first(U,N,[U|T],[N|T]).
  208. replace_first(U,N,[H|T],[H|R]):-replace_first(U,N,T,R).
  209.  
  210. % 22. Inlocuirea primei aparitii a unui elelemnt U cu un alt element N dintr-o lista termina in variabila
  211. replace_first_il(_,_,T,T):-var(T),!.
  212. replace_first_il(U,N,[U|T],[N|T]).
  213. replace_first_il(U,N,[H|T],[H|R]):-replace_first_il(U,N,T,R).
  214.  
  215. % 23. Inlocuirea aparitiilor a unui elelemnt U cu un alt element N dintr-o lista completa
  216. replace_all(_,_,[],[]).
  217. replace_all(U,N,[U|T],[N|R]):-replace_all(U,N,T,R).
  218. replace_all(U,N,[H|T],[H|R]):-replace_all(U,N,T,R).
  219.  
  220. % 24. Inlocuirea aparitiilor a unui elelemnt U cu un alt element N dintr-o lista incompleta
  221. replace_all_il(_,_,T,T):-var(T),!.
  222. replace_all_il(U,N,[U|T],[N|R]):-replace_all_il(U,N,T,R).
  223. replace_all_il(U,N,[H|T],[H|R]):-replace_all_il(U,N,T,R).
  224.  
  225. %1. Count the number of lists in a deep list.
  226. count_lists([],0).
  227. count_lists([H|T],R):-atomic(H),count_lists(T,R).
  228. count_lists([H|T], R):-count_lists(H, RH),count_lists(T, RT),R is RH + RT + 1.
  229.  
  230. append1([], L, L).
  231. append1([H|T], L, [H|R]):-append(T, L, R).
  232. %2. Double the odd numbers and square the even.
  233. numbers([],[]).
  234. numbers([H|T],[H*H|R]):- 0 is H mod 2,numbers(T,R).
  235. numbers([H|T],[2*H|R]):- 1 is H mod 2,numbers(T,R).
  236. %3. Convert a number to binary
  237. dec_bin(0,'0').
  238. dec_bin(1,'1').
  239. dec_bin(N,B):-N>1,X is N mod 2,Y is N//2,dec_bin(Y,B1),atom_concat(B1, X, B).
  240.  
  241. %5. Delete the occurrences of x on even positions
  242. del_pos_even([],_,_,[]).
  243. del_pos_even([H|T],C,H,R):-0 is C mod 2,!,C1 is C+1,del_pos_even(T,C1,H,R).
  244. del_pos_even([H|T],C,Nr,[H|R]):-C1 is C+1,del_pos_even(T,C1,Nr,R).
  245.  
  246.  
  247. %6 Compute the divisors of a natural number.
  248. divisor(NR,NR,DIVS,[NR|DIVS]):-!.
  249. divisor(NR,ACC,DIVS,R):-0 is NR mod ACC,!,ACC1 is ACC + 1,append([ACC],DIVS,DIVS1),divisor(NR,ACC1,DIVS1,R).
  250. divisor(NR,ACC,DIVS,R):-ACC1 is ACC+1,divisor(NR,ACC1,DIVS,R).
  251.  
  252.  
  253. %7. Reverse a natural number.
  254. reverse_n(0,0).
  255. reverse_n(Nr,R1):-M is Nr mod 10, D is Nr div 10,reverse_n(D,R),atom_concat(M,R,R1).
  256.  
  257. %8. Delete each kth element from the end of the list.
  258. delete_k([],_,_,[]).
  259. delete_k([H|T],K,Count,R):-Count is K-1,!,delete_k(T,K,0,R).
  260. delete_k([H|T],K,Count,[H|R]):-Count1 is Count + 1, delete_k(T,K,Count1,R).
  261. reverse_list([],[]).
  262. reverse_list([H|T],R):-reverse(T,R1),append(R1,[H],R).
  263. delete_kend(L,K,Count,R):-reverse_list(L,NewL),delete_k(NewL,K,Count,R1),reverse_list(R1,R).
  264.  
  265. % 9. Separate the even elements on odd positions from the rest
  266. separate1([],_,[],[]).
  267. separate1([H|T],Count,[H|Even],Rest):-0 is H mod 2,1 is Count mod 2,!,Count1 is Count + 1,separate1(T,Count1,Even,Rest).
  268. separate1([H|T],Count,Even,[H|Rest]):-Count1 is Count + 1,separate1(T,Count1,Even,Rest).
  269.  
  270. separate11(L,Even,Rest):-separate1(L,1,Even,Rest).
  271.  
  272.  
  273. %10. Binary incomplete tree. Collect odd nodes with 1 child in an incomplete list.
  274. collect_odd_from_1child(t(_, L, R), _):-
  275.     var(L),
  276.     var(R), !.
  277. collect_odd_from_1child(t(K, N, R), [K|List]):-
  278.     var(N),
  279.     1 is K mod 2, !,
  280.     collect_odd_from_1child(R, List).
  281. collect_odd_from_1child(t(K, L, N), List):-
  282.     var(N),
  283.     1 is K mod 2, !,
  284.     collect_odd_from_1child(L, List),
  285.     insert_il(K, List).
  286. collect_odd_from_1child(t(_, L, R), List):-
  287.     collect_odd_from_1child(L, LL),
  288.     collect_odd_from_1child(R, RL),
  289.     append_il(LL, RL),
  290.     List = LL.
  291.  
  292. insert_il(X, L):-
  293.     var(L), !,
  294.     L = [X|_].
  295. insert_il(X, [_|T]):-
  296.     insert_il(X, T).
  297.  
  298. append_il(L1, L2):-
  299.     var(L1), !,
  300.     L1 = L2.
  301. append_il([_|T1], L2):-
  302.     append_il(T1, L2).
  303.  
  304. %12. Binary Tree. Collect even keys from leaves in a difference list.
  305.  
  306. collect_even_from_leaf(nil,L,L).
  307. collect_even_from_leaf(t(K,nil,nil),[K|LS],LS):-0 is K mod 2,!.
  308. collect_even_from_leaf(t(K,L,R),LS,LE):-collect_even_from_leaf(L,LS,LM),collect_even_from_leaf(R,LM,LE).
  309.  
  310. %14. Collect all the nodes at odd depth from a binary incomplete tree (the root has depth 0).
  311. collect_all_odd_depth(N, _, []):-
  312.     var(N),!.
  313. collect_all_odd_depth(t(K, L, R), N, [K|List]):-
  314.     1 is N mod 2, !,
  315.     N1 is N + 1,
  316.     collect_all_odd_depth(L, N1, LL),
  317.     collect_all_odd_depth(R, N1, LR),
  318.     append(LL, LR, List).
  319. collect_all_odd_depth(t(_, L, R), N, List):-
  320.     N1 is N + 1,
  321.     collect_all_odd_depth(L, N1, LL),
  322.     collect_all_odd_depth(R, N1, LR),
  323.     append(LL, LR, List).
  324.  
  325.   %15. Flatten only the elements at depth X from a deep list.
  326. max(A,B,A):-A>B,!.
  327. max(A,B,B).
  328.  
  329. depth([],1).
  330. depth([H|T],R):-atomic(H),!,depth(T,R).
  331. depth([H|T],R):- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
  332.  
  333. flatten([],[]).
  334. flatten([H|T], [H|R]) :- atomic(H),!, flatten(T,R).
  335. flatten([H|T], R) :- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
  336.  
  337. flatten_depth([],_,_,[]).
  338. flatten_depth([H|T],Depth,Count,[H|R]):-atomic(H),!,Count is Depth,flatten_depth(T,Depth,Count,R).
  339. flatten_depth([H|T],Depth,Count,R):-atomic(H),!,flatten_depth(T,Depth,Count,R).
  340. flatten_depth([H|T],Depth,Count,R):-Count1 is Count +1,flatten_depth(H,Depth,Count1,R1),flatten_depth(T,Depth,Count,R2),append(R1,R2,R).
  341.  
  342. %16. Delete duplicate elements that are on an odd position in a list
  343. %count_dups(List,Elemen,Count,R)
  344. count_dups([],El,Count,[Count]).
  345. count_dups([H|T],H,Count,R):-Count1 is Count+1,count_dups(T,H,Count1,R).
  346. count_dups([H|T],El,Count,R):-count_dups(T,El,Count,R).
  347.  
  348. %remove_odd(List,Elem,Count,R)
  349. remove_odd([],_,_,[]).
  350. remove_odd([H|T],H,Count,R):-1 is Count mod 2,!,Count1 is Count + 1, remove_odd(T,H,Count1,R).
  351. remove_odd([H|T],Elem,Count,[H|R]):-Count1 is Count + 1, remove_odd(T,Elem,Count1,R).
  352.  
  353. %remove_odd_dups(List,R)
  354. remove_odd_dups([],[]).
  355. remove_odd_dups([H|T],[H|R]):-count_dups(T,H,0,C),C>1,remove_odd(T,H,0,NewT),remove_odd_dups(NewT,R).
  356. remove_odd_dups([H|T],[H|R]):-remove_odd_dups(T,R).
  357.  
  358. %11. Ternary incomplete tree. Collect the keys between X and Y (closed interval) in a difference list.
  359.  
  360. %collect_ternary(T,X,Y,R).
  361. collect_ternary(L,_,_,LS,LS):-var(L),!.
  362. collect_ternary(t(K,L,M,R),X,Y,[K|LS],LE):-K>=X,K=<Y,!,collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
  363. collect_ternary(t(K,L,M,R),X,Y,LS,LE):-collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
  364.  
  365. tree(t(2,t(8,_,_,_),t(3,_,_,t(4,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  366.  
  367.  
  368. %13. Replace the min element from a ternary incomplete tree with the root.
  369. inorder_ternary(T,_):-var(T),!.
  370. inorder_ternary(t(K,L,M,R), List):-inorder_ternary(L,LL), inorder_ternary(M,LM), inorder_ternary(R,RL),
  371. append(LL, [K|LM],List1), append(List1,RL,List).
  372.  
  373.  
  374. minim([],MIN,MIN).
  375. minim([H|T],MIN,R):-H<MIN,!,MINP is H,minim(T,MINP,R).
  376. minim([H|T],MIN,R):-minim(T,MIN,R).
  377.  
  378. min_ternary(T,List):-inorder_ternary(T,L),minim(L,100,List).
  379.  
  380. %copac(t(2,t(8,_,_,_),t(3,_,_,t(1,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  381.  
  382. replacemin(t(K,L,M,R),t(NK,NL,NM,NR)):-min_ternary(t(K,L,M,R),Min),replace_min_tree(t(K,L,M,R),Min,K,t(NK,NL,NM,NR)).
  383.  
  384. replace_min_tree(L,_,_,_):-var(L),!.
  385. replace_min_tree(t(K,L,M,R),Min,Root,t(Root,NL,NM,NR)):-Min=K,!,
  386.                                                         replace_min_tree(L,Min,Root,NL),
  387.                                                         replace_min_tree(M,Min,Root,NM),
  388.                                                         replace_min_tree(R,Min,Root,NR).
  389. replace_min_tree(t(K,L,M,R),Min,Root,t(K,NL,NM,NR)):-
  390.                             replace_min_tree(L,Min,Root,NL),
  391.                                                      replace_min_tree(M,Min,Root,NM),
  392.                                                      replace_min_tree(R,Min,Root,NR).
  393. %18. Replace each node with its height in a binary incomplete tree
  394. % predicate which computes the maximum between 2 numbers
  395. max(A, B, A):-A>B, !.
  396. max(_, B, B).
  397. % predicate which computes the height of a binary tree
  398. height(L, 0):-var(L),!.
  399. height(t(_, L, R), H):-height(L, H1), height(R, H2), max(H1, H2, H3), H is H3+1.
  400.  
  401. replaceheight(T,R):-height(T,H),replace_height(T,H,R).
  402.  
  403. replace_height(L,_,L):-var(L).
  404. replace_height(t(K,L,R),Height,t(Height,NL,NR)):-H1 is Height-1,replace_height(L,H1,NL),replace_height(R,H1,NR).
  405. altcopac(t(2,t(4,t(5,_,_),t(7,_,_)),t(3,t(0,t(4,_,_),_),t(8,_,t(5,_,_))))).
  406.  
  407. %21. Encode a list with RLE.
  408. encode([],X,Count,[[X,Count]]).
  409. encode([H|T],H,Count,R):-Count1 is Count + 1,encode(T,H,Count1,R).
  410. encode([H|T],X,Count,[[X,Count]|R]):-encode(T,H,1,R).
  411.  
  412. rle_encode([H|T],R):-encode(T,H,1,R).
  413.  
  414. %20. Decode a list encoded with RLE.
  415. printn([Nr,Count],Count,List,[List]).
  416. printn([Nr,Count],Acc,List,R):-Acc<Count,Acc1 is Acc + 1, append([Nr],List,List1),printn([Nr,Count],Acc1,List1,R).
  417. decode([],List,[List]).
  418. decode([[Nr,Count]|T],List,R):-printn([Nr,Count],0,[],R1),append(List,R1,R2),decode(T,R2,R).
  419.  
  420.  
  421.  
  422.  
  423.  
  424. %[1,2,3,4]
  425. %[5,6,4,6]
  426. %[4,5,6,3]
  427. %[1,2,3,3]
  428.  
  429. %[1,5,4,1]
  430. %[2,6,5,2]
  431. %[3,4,6,3]
  432. %[4,6,3,3]
  433.  
  434.  
  435. extractk([],_,_,[]).
  436. extractk([H|T],Count,K,[H|R]):-Count is K,!,Count1 is Count + 1,extractk(T,Count1,K,R).
  437. extractk([H|T],Count,K,R):-Count1 is Count +1,extractk(T,Count1,K,R).
  438.  
  439.  
  440. %%%%%%%%%%%
  441. count_dups1(L,El,Count,[Count]):-var(L),!.
  442. count_dups1([H|T],H,Count,R):-Count1 is Count+1,count_dups1(T,H,Count1,R).
  443. count_dups1([H|T],El,Count,R):-count_dups1(T,El,Count,R).
  444. %%%%%%%%%%%%%%
  445. postorder(t(K,L,R),List):-postorder(L,LL),postorder(R,LR),append(LL,LR,Rez1),append(Rez1,[K],List).
  446. postorder(nil,[]).
  447.  
  448. sum([],0).
  449. sum([H|T],R):-sum(T,T1),R is T1 + H.
  450.  
  451. suma(L,Partial,Partial):-var(L),!.
  452. suma([H|T],Partial,R):-sum(H,Suma),append(Partial,[Suma],Partial1),suma(T,Partial1,R).
  453.  
  454. sumaminim(L,M):-suma(L,[],R),minim(R,100,M).
  455.  
  456. %In lista incompleta, nodurile de la o anumita inaltime
  457.  
  458.  
  459. heightC(nil, 0).
  460. heightC(t(_, L, R), H):-heightC(L, H1), heightC(R, H2), max(H1, H2, H3), H is H3+1.
  461.  
  462. collect(nil,_,_,_).
  463. collect(t(K,L,R),WantedHeight,LocalHeight,[K|List]):-LocalHeight is WantedHeight,!,LocalHeight1 is LocalHeight - 1,
  464.                                                         collect(L,WantedHeight,LocalHeight1,List1),
  465.                                                         collect(R,WantedHeight,LocalHeight1,List2),
  466.                                                         append(List1,List2,List).
  467. collect(t(K,L,R),WantedHeight,LocalHeight,List):-LocalHeight1 is LocalHeight - 1,
  468.                                                         collect(L,WantedHeight,LocalHeight1,List1),
  469.                                                         collect(R,WantedHeight,LocalHeight1,Lis2),
  470.                                                         append(List1,List2,List).
  471.  
  472. collectFinal(T,WantedHeight,List):-heightC(T,Height),collect(T,WantedHeight,Height,List).                                                      
  473.  
  474. copacel(t(6, t(4, t(2, nil, nil), t(5, nil, nil)), t(9, t(7, nil, nil), nil))).
  475.  
  476. %%%%%%%% Sum from all elems from root to leaf
  477. rootleaf(nil,_,[]).
  478. rootleaf(t(K,nil,nil),S,[Res]):-
  479.     Res is K+S.
  480. rootleaf(t(K,L,R),S,Res):-
  481.     S1 is S+K,
  482.     rootleaf(L,S1,R1),
  483.     S2 is S+K,
  484.     rootleaf(R,S2,R2),
  485.     append(R1,R2,Res).
  486. %%%%%%%%%%%%%%
  487.  
  488. rootleaf(nil,_,LS,LS).
  489. rootleaf(t(K,nil,nil),S,[Res|LS],LS):-
  490.     Res is K+S.
  491. rootleaf(t(K,L,R),S,LS,LE):-
  492.     S1 is S+K,
  493.     rootleaf(L,S1,LS,LM),
  494.     S2 is S+K,
  495.     rootleaf(R,S2,LM,LE).
  496.  
  497. %%%%%%%prime factors
  498.  
  499. decompose(Nr,Nr,Count,[[Nr,Count1]]):-Count1 is Count + 1.
  500. decompose(Nr,Divider,Count,R):-0 is Nr mod Divider,!,Nr1 is Nr//Divider,Count1 is Count + 1,decompose(Nr1,Divider,Count1,R).
  501. decompose(Nr,Divider,Count,[[Divider,Count]|R]):- Divider1 is Divider + 1,decompose(Nr,Divider1,0,R).
  502.  
  503. decomposee(Nr,R):-decompose(Nr,2,0,R).
  504. %%%%%%%%%%%%%
  505. maxim([],MIN,MIN).
  506. maxim([H|T],MIN,R):-H>MIN,!,MINP is H,maxim(T,MINP,R).
  507. maxim([H|T],MIN,R):-maxim(T,MIN,R).
  508.  
  509. find_maxi(L,M,M):-var(L),!.
  510. find_maxi([H|T],MP,M):-maxim(H,0,MPP),MPP>MP,!,MP1 is MPP,find_maxi(T,MP1,M).
  511. find_maxi([H|T],MP,M):-find_maxi(T,MP,M).
  512.  
  513. member_il(_, L):-var(L), !, fail.
  514. % these 2 clauses are the same as for the member1 predicate
  515. member_il(X, [X|_]):-!.
  516. member_il(X, [_|T]):-member_il(X, T).
  517.  
  518. maxfinal(L,_,Partial,Partial):-var(L),!.
  519. maxfinal([H|T],M,Partial,R):-member_il(M,H),!,append(H,Partial,Partial1),maxfinal(T,M,Partial1,R).
  520. maxfinal([H|T],M,Partial,R):-maxfinal(T,M,Partial,R).
  521.  
  522. maxfinalCall(L,R):-find_maxi(L,0,M),maxfinal(L,M,[],R).
  523. %%%%%%%%%%%%%%%%%%%%%%%%
  524.  
  525. collect_tmiddle(nil,[],LS).
  526. collect_tmiddle(t(K,L,nil,R),LS,LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(R,LM,LE).
  527. collect_tmiddle(t(K,L,M,R),[K|LS],LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(M,LM,LX),collect_tmiddle(R,LX,LE).
  528.  
  529. copacelul(t(2,t(8,nil,nil,nil),t(3,nil,nil,t(1,nil,1,nil)),t(5,t(7,nil,nil,nil),t(6,nil,nil,nil),t(1,nil,nil,t(9,nil,10,11))))).
  530.  
  531. %1. Count the number of lists in a deep list.
  532. count_lists([],0).
  533. count_lists([H|T],R):-atomic(H),count_lists(T,R).
  534. count_lists([H|T], R):-count_lists(H, RH),count_lists(T, RT),R is RH + RT + 1.
  535.  
  536. append1([], L, L).
  537. append1([H|T], L, [H|R]):-append(T, L, R).
  538. %2. Double the odd numbers and square the even.
  539. numbers([],[]).
  540. numbers([H|T],[H*H|R]):- 0 is H mod 2,numbers(T,R).
  541. numbers([H|T],[2*H|R]):- 1 is H mod 2,numbers(T,R).
  542. %3. Convert a number to binary
  543. dec_bin(0,'0').
  544. dec_bin(1,'1').
  545. dec_bin(N,B):-N>1,X is N mod 2,Y is N//2,dec_bin(Y,B1),atom_concat(B1, X, B).
  546.  
  547. %5. Delete the occurrences of x on even positions
  548. del_pos_even([],_,_,[]).
  549. del_pos_even([H|T],C,H,R):-0 is C mod 2,!,C1 is C+1,del_pos_even(T,C1,H,R).
  550. del_pos_even([H|T],C,Nr,[H|R]):-C1 is C+1,del_pos_even(T,C1,Nr,R).
  551.  
  552.  
  553. %6 Compute the divisors of a natural number.
  554. divisor(NR,NR,DIVS,[NR|DIVS]):-!.
  555. divisor(NR,ACC,DIVS,R):-0 is NR mod ACC,!,ACC1 is ACC + 1,append([ACC],DIVS,DIVS1),divisor(NR,ACC1,DIVS1,R).
  556. divisor(NR,ACC,DIVS,R):-ACC1 is ACC+1,divisor(NR,ACC1,DIVS,R).
  557.  
  558.  
  559. %7. Reverse a natural number.
  560. reverse_n(0,0).
  561. reverse_n(Nr,R1):-M is Nr mod 10, D is Nr div 10,reverse_n(D,R),atom_concat(M,R,R1).
  562.  
  563. %8. Delete each kth element from the end of the list.
  564. delete_k([],_,_,[]).
  565. delete_k([H|T],K,Count,R):-Count is K-1,!,delete_k(T,K,0,R).
  566. delete_k([H|T],K,Count,[H|R]):-Count1 is Count + 1, delete_k(T,K,Count1,R).
  567. reverse_list([],[]).
  568. reverse_list([H|T],R):-reverse(T,R1),append(R1,[H],R).
  569. delete_kend(L,K,Count,R):-reverse_list(L,NewL),delete_k(NewL,K,Count,R1),reverse_list(R1,R).
  570.  
  571. % 9. Separate the even elements on odd positions from the rest
  572. separate1([],_,[],[]).
  573. separate1([H|T],Count,[H|Even],Rest):-0 is H mod 2,1 is Count mod 2,!,Count1 is Count + 1,separate1(T,Count1,Even,Rest).
  574. separate1([H|T],Count,Even,[H|Rest]):-Count1 is Count + 1,separate1(T,Count1,Even,Rest).
  575.  
  576. separate11(L,Even,Rest):-separate1(L,1,Even,Rest).
  577.  
  578.  
  579. %10. Binary incomplete tree. Collect odd nodes with 1 child in an incomplete list.
  580. collect_odd_from_1child(t(_, L, R), _):-
  581.     var(L),
  582.     var(R), !.
  583. collect_odd_from_1child(t(K, N, R), [K|List]):-
  584.     var(N),
  585.     1 is K mod 2, !,
  586.     collect_odd_from_1child(R, List).
  587. collect_odd_from_1child(t(K, L, N), List):-
  588.     var(N),
  589.     1 is K mod 2, !,
  590.     collect_odd_from_1child(L, List),
  591.     insert_il(K, List).
  592. collect_odd_from_1child(t(_, L, R), List):-
  593.     collect_odd_from_1child(L, LL),
  594.     collect_odd_from_1child(R, RL),
  595.     append_il(LL, RL),
  596.     List = LL.
  597.  
  598. insert_il(X, L):-
  599.     var(L), !,
  600.     L = [X|_].
  601. insert_il(X, [_|T]):-
  602.     insert_il(X, T).
  603.  
  604. append_il(L1, L2):-
  605.     var(L1), !,
  606.     L1 = L2.
  607. append_il([_|T1], L2):-
  608.     append_il(T1, L2).
  609.  
  610. %12. Binary Tree. Collect even keys from leaves in a difference list.
  611.  
  612. collect_even_from_leaf(nil,L,L).
  613. collect_even_from_leaf(t(K,nil,nil),[K|LS],LS):-0 is K mod 2,!.
  614. collect_even_from_leaf(t(K,L,R),LS,LE):-collect_even_from_leaf(L,LS,LM),collect_even_from_leaf(R,LM,LE).
  615.  
  616. %14. Collect all the nodes at odd depth from a binary incomplete tree (the root has depth 0).
  617. collect_all_odd_depth(N, _, []):-
  618.     var(N),!.
  619. collect_all_odd_depth(t(K, L, R), N, [K|List]):-
  620.     1 is N mod 2, !,
  621.     N1 is N + 1,
  622.     collect_all_odd_depth(L, N1, LL),
  623.     collect_all_odd_depth(R, N1, LR),
  624.     append(LL, LR, List).
  625. collect_all_odd_depth(t(_, L, R), N, List):-
  626.     N1 is N + 1,
  627.     collect_all_odd_depth(L, N1, LL),
  628.     collect_all_odd_depth(R, N1, LR),
  629.     append(LL, LR, List).
  630.  
  631.   %15. Flatten only the elements at depth X from a deep list.
  632. max(A,B,A):-A>B,!.
  633. max(A,B,B).
  634.  
  635. depth([],1).
  636. depth([H|T],R):-atomic(H),!,depth(T,R).
  637. depth([H|T],R):- depth(H,R1), depth(T,R2), R3 is R1+1, max(R3,R2,R).
  638.  
  639. flatten([],[]).
  640. flatten([H|T], [H|R]) :- atomic(H),!, flatten(T,R).
  641. flatten([H|T], R) :- flatten(H,R1), flatten(T,R2), append(R1,R2,R).
  642.  
  643. flatten_depth([],_,_,[]).
  644. flatten_depth([H|T],Depth,Count,[H|R]):-atomic(H),!,Count is Depth,flatten_depth(T,Depth,Count,R).
  645. flatten_depth([H|T],Depth,Count,R):-atomic(H),!,flatten_depth(T,Depth,Count,R).
  646. flatten_depth([H|T],Depth,Count,R):-Count1 is Count +1,flatten_depth(H,Depth,Count1,R1),flatten_depth(T,Depth,Count,R2),append(R1,R2,R).
  647.  
  648. %16. Delete duplicate elements that are on an odd position in a list
  649. %count_dups(List,Elemen,Count,R)
  650. count_dups([],El,Count,[Count]).
  651. count_dups([H|T],H,Count,R):-Count1 is Count+1,count_dups(T,H,Count1,R).
  652. count_dups([H|T],El,Count,R):-count_dups(T,El,Count,R).
  653.  
  654. %remove_odd(List,Elem,Count,R)
  655. remove_odd([],_,_,[]).
  656. remove_odd([H|T],H,Count,R):-1 is Count mod 2,!,Count1 is Count + 1, remove_odd(T,H,Count1,R).
  657. remove_odd([H|T],Elem,Count,[H|R]):-Count1 is Count + 1, remove_odd(T,Elem,Count1,R).
  658.  
  659. %remove_odd_dups(List,R)
  660. remove_odd_dups([],[]).
  661. remove_odd_dups([H|T],[H|R]):-count_dups(T,H,0,C),C>1,remove_odd(T,H,0,NewT),remove_odd_dups(NewT,R).
  662. remove_odd_dups([H|T],[H|R]):-remove_odd_dups(T,R).
  663.  
  664. %11. Ternary incomplete tree. Collect the keys between X and Y (closed interval) in a difference list.
  665.  
  666. %collect_ternary(T,X,Y,R).
  667. collect_ternary(L,_,_,LS,LS):-var(L),!.
  668. collect_ternary(t(K,L,M,R),X,Y,[K|LS],LE):-K>=X,K=<Y,!,collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
  669. collect_ternary(t(K,L,M,R),X,Y,LS,LE):-collect_ternary(L,X,Y,LS,RE1),collect_ternary(M,X,Y,RE1,RE2),collect_ternary(R,X,Y,RE2,LE).
  670.  
  671. tree(t(2,t(8,_,_,_),t(3,_,_,t(4,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  672.  
  673.  
  674. %13. Replace the min element from a ternary incomplete tree with the root.
  675. inorder_ternary(T,_):-var(T),!.
  676. inorder_ternary(t(K,L,M,R), List):-inorder_ternary(L,LL), inorder_ternary(M,LM), inorder_ternary(R,RL),
  677. append(LL, [K|LM],List1), append(List1,RL,List).
  678.  
  679.  
  680. minim([],MIN,MIN).
  681. minim([H|T],MIN,R):-H<MIN,!,MINP is H,minim(T,MINP,R).
  682. minim([H|T],MIN,R):-minim(T,MIN,R).
  683.  
  684. min_ternary(T,List):-inorder_ternary(T,L),minim(L,100,List).
  685.  
  686. %copac(t(2,t(8,_,_,_),t(3,_,_,t(1,_,_,_)),t(5,t(7,_,_,_),t(6,_,_,_),t(1,_,_,t(9,_,_,_))))).
  687.  
  688. replacemin(t(K,L,M,R),t(NK,NL,NM,NR)):-min_ternary(t(K,L,M,R),Min),replace_min_tree(t(K,L,M,R),Min,K,t(NK,NL,NM,NR)).
  689.  
  690. replace_min_tree(L,_,_,_):-var(L),!.
  691. replace_min_tree(t(K,L,M,R),Min,Root,t(Root,NL,NM,NR)):-Min=K,!,
  692.                                                         replace_min_tree(L,Min,Root,NL),
  693.                                                         replace_min_tree(M,Min,Root,NM),
  694.                                                         replace_min_tree(R,Min,Root,NR).
  695. replace_min_tree(t(K,L,M,R),Min,Root,t(K,NL,NM,NR)):-
  696.                             replace_min_tree(L,Min,Root,NL),
  697.                                                      replace_min_tree(M,Min,Root,NM),
  698.                                                      replace_min_tree(R,Min,Root,NR).
  699. %18. Replace each node with its height in a binary incomplete tree
  700. % predicate which computes the maximum between 2 numbers
  701. max(A, B, A):-A>B, !.
  702. max(_, B, B).
  703. % predicate which computes the height of a binary tree
  704. height(L, 0):-var(L),!.
  705. height(t(_, L, R), H):-height(L, H1), height(R, H2), max(H1, H2, H3), H is H3+1.
  706.  
  707. replaceheight(T,R):-height(T,H),replace_height(T,H,R).
  708.  
  709. replace_height(L,_,L):-var(L).
  710. replace_height(t(K,L,R),Height,t(Height,NL,NR)):-H1 is Height-1,replace_height(L,H1,NL),replace_height(R,H1,NR).
  711. altcopac(t(2,t(4,t(5,_,_),t(7,_,_)),t(3,t(0,t(4,_,_),_),t(8,_,t(5,_,_))))).
  712.  
  713. %21. Encode a list with RLE.
  714. encode([],X,Count,[[X,Count]]).
  715. encode([H|T],H,Count,R):-Count1 is Count + 1,encode(T,H,Count1,R).
  716. encode([H|T],X,Count,[[X,Count]|R]):-encode(T,H,1,R).
  717.  
  718. rle_encode([H|T],R):-encode(T,H,1,R).
  719.  
  720. %20. Decode a list encoded with RLE.
  721. printn([Nr,Count],Count,List,[List]).
  722. printn([Nr,Count],Acc,List,R):-Acc<Count,Acc1 is Acc + 1, append([Nr],List,List1),printn([Nr,Count],Acc1,List1,R).
  723. decode([],List,[List]).
  724. decode([[Nr,Count]|T],List,R):-printn([Nr,Count],0,[],R1),append(List,R1,R2),decode(T,R2,R).
  725.  
  726.  
  727.  
  728.  
  729.  
  730. %[1,2,3,4]
  731. %[5,6,4,6]
  732. %[4,5,6,3]
  733. %[1,2,3,3]
  734.  
  735. %[1,5,4,1]
  736. %[2,6,5,2]
  737. %[3,4,6,3]
  738. %[4,6,3,3]
  739.  
  740.  
  741. extractk([],_,_,[]).
  742. extractk([H|T],Count,K,[H|R]):-Count is K,!,Count1 is Count + 1,extractk(T,Count1,K,R).
  743. extractk([H|T],Count,K,R):-Count1 is Count +1,extractk(T,Count1,K,R).
  744.  
  745.  
  746. %%%%%%%%%%%
  747. count_dups1(L,El,Count,[Count]):-var(L),!.
  748. count_dups1([H|T],H,Count,R):-Count1 is Count+1,count_dups1(T,H,Count1,R).
  749. count_dups1([H|T],El,Count,R):-count_dups1(T,El,Count,R).
  750. %%%%%%%%%%%%%%
  751. postorder(t(K,L,R),List):-postorder(L,LL),postorder(R,LR),append(LL,LR,Rez1),append(Rez1,[K],List).
  752. postorder(nil,[]).
  753.  
  754. sum([],0).
  755. sum([H|T],R):-sum(T,T1),R is T1 + H.
  756.  
  757. suma(L,Partial,Partial):-var(L),!.
  758. suma([H|T],Partial,R):-sum(H,Suma),append(Partial,[Suma],Partial1),suma(T,Partial1,R).
  759.  
  760. sumaminim(L,M):-suma(L,[],R),minim(R,100,M).
  761.  
  762. %In lista incompleta, nodurile de la o anumita inaltime
  763.  
  764.  
  765. heightC(nil, 0).
  766. heightC(t(_, L, R), H):-heightC(L, H1), heightC(R, H2), max(H1, H2, H3), H is H3+1.
  767.  
  768. collect(nil,_,_,_).
  769. collect(t(K,L,R),WantedHeight,LocalHeight,[K|List]):-LocalHeight is WantedHeight,!,LocalHeight1 is LocalHeight - 1,
  770.                                                         collect(L,WantedHeight,LocalHeight1,List1),
  771.                                                         collect(R,WantedHeight,LocalHeight1,List2),
  772.                                                         append(List1,List2,List).
  773. collect(t(K,L,R),WantedHeight,LocalHeight,List):-LocalHeight1 is LocalHeight - 1,
  774.                                                         collect(L,WantedHeight,LocalHeight1,List1),
  775.                                                         collect(R,WantedHeight,LocalHeight1,Lis2),
  776.                                                         append(List1,List2,List).
  777.  
  778. collectFinal(T,WantedHeight,List):-heightC(T,Height),collect(T,WantedHeight,Height,List).                                                      
  779.  
  780. copacel(t(6, t(4, t(2, nil, nil), t(5, nil, nil)), t(9, t(7, nil, nil), nil))).
  781.  
  782. %%%%%%%% Sum from all elems from root to leaf
  783. rootleaf(nil,_,[]).
  784. rootleaf(t(K,nil,nil),S,[Res]):-
  785.     Res is K+S.
  786. rootleaf(t(K,L,R),S,Res):-
  787.     S1 is S+K,
  788.     rootleaf(L,S1,R1),
  789.     S2 is S+K,
  790.     rootleaf(R,S2,R2),
  791.     append(R1,R2,Res).
  792. %%%%%%%%%%%%%%
  793.  
  794. rootleaf(nil,_,LS,LS).
  795. rootleaf(t(K,nil,nil),S,[Res|LS],LS):-
  796.     Res is K+S.
  797. rootleaf(t(K,L,R),S,LS,LE):-
  798.     S1 is S+K,
  799.     rootleaf(L,S1,LS,LM),
  800.     S2 is S+K,
  801.     rootleaf(R,S2,LM,LE).
  802.  
  803. %%%%%%%prime factors
  804.  
  805. decompose(Nr,Nr,Count,[[Nr,Count1]]):-Count1 is Count + 1.
  806. decompose(Nr,Divider,Count,R):-0 is Nr mod Divider,!,Nr1 is Nr//Divider,Count1 is Count + 1,decompose(Nr1,Divider,Count1,R).
  807. decompose(Nr,Divider,Count,[[Divider,Count]|R]):- Divider1 is Divider + 1,decompose(Nr,Divider1,0,R).
  808.  
  809. decomposee(Nr,R):-decompose(Nr,2,0,R).
  810. %%%%%%%%%%%%%
  811. maxim([],MIN,MIN).
  812. maxim([H|T],MIN,R):-H>MIN,!,MINP is H,maxim(T,MINP,R).
  813. maxim([H|T],MIN,R):-maxim(T,MIN,R).
  814.  
  815. find_maxi(L,M,M):-var(L),!.
  816. find_maxi([H|T],MP,M):-maxim(H,0,MPP),MPP>MP,!,MP1 is MPP,find_maxi(T,MP1,M).
  817. find_maxi([H|T],MP,M):-find_maxi(T,MP,M).
  818.  
  819. member_il(_, L):-var(L), !, fail.
  820. % these 2 clauses are the same as for the member1 predicate
  821. member_il(X, [X|_]):-!.
  822. member_il(X, [_|T]):-member_il(X, T).
  823.  
  824. maxfinal(L,_,Partial,Partial):-var(L),!.
  825. maxfinal([H|T],M,Partial,R):-member_il(M,H),!,append(H,Partial,Partial1),maxfinal(T,M,Partial1,R).
  826. maxfinal([H|T],M,Partial,R):-maxfinal(T,M,Partial,R).
  827.  
  828. maxfinalCall(L,R):-find_maxi(L,0,M),maxfinal(L,M,[],R).
  829. %%%%%%%%%%%%%%%%%%%%%%%%
  830.  
  831. collect_tmiddle(nil,[],LS).
  832. collect_tmiddle(t(K,L,nil,R),LS,LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(R,LM,LE).
  833. collect_tmiddle(t(K,L,M,R),[K|LS],LE):-collect_tmiddle(L,LS,LM),collect_tmiddle(M,LM,LX),collect_tmiddle(R,LX,LE).
  834.  
  835. copacelul(t(2,t(8,nil,nil,nil),t(3,nil,nil,t(1,nil,1,nil)),t(5,t(7,nil,nil,nil),t(6,nil,nil,nil),t(1,nil,nil,t(9,nil,10,11))))).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement