Untitled

mail@pastecode.io avatar
unknown
prolog
2 years ago
1.7 kB
5
Indexable
spoji([],L,L).
spoji(L,[],L).
spoji([G1|R1],[G2|R2],[G1,G2|R3]):-spoji(R1,R2,R3).
lkd(nil,[]).
lkd(s(L,K,D),LC):-lkd(L,L1), lkd(D,L2), spoji(L1,[K|L2],LC).
ubaci(X,[],[X]).
ubaci(X,[G|R],[G|R1]):-X>G, ubaci(X,R,R1).
ubaci(X,[G|R],[X,G|R]):-X=<G.
sortiraj([],[]).
sortiraj([G|R],NL):-sortiraj(R,R1), ubaci(G,R1,NL).
 
brEl([],0).
brEl([_|R],X):-brEl(R,X1), X is X1+1.
srednji(L,X):-brEl(L,N), X is (N+1)//2.
 
podeli([],_,_,[],[],_).
podeli([G|R],SR,BR,[G|R1],LD,VAL):-BR<SR, BR1 is BR+1, podeli(R,SR,BR1,R1,LD,VAL).
podeli([G|R],SR,BR,LL,LD,G):-BR==SR, BR1 is BR+1, podeli(R,SR,BR1,LL,LD,G).
podeli([G|R],SR,BR,LL,[G|R1],VAL):-BR>SR, BR1 is BR+1, podeli(R,SR,BR1,LL,R1,VAL).
 
struktura(A,B,C,s(A,B,C)).
stablo([],nil).
stablo(L,X):-srednji(L,SR), podeli(L,SR,1,LL,LD,VAL), stablo(LL,X1), stablo(LD,X2), struktura(X1,VAL,X2,X).

visina(L,X):-brEl(L,N), X is floor(log10(N)/log10(2)).
razmaci(0).
razmaci(X):-X1 is X-1, write('_'), razmaci(X1).
pow2(0,1).
pow2(N,S):-N1 is N-1, pow2(N1,S1), S is S1*2.

nivoi(nil,_,[]).
nivoi(s(A,B,C),N,[n(B,N)|R]):-N1 is N+1, nivoi(A,N1,L1), nivoi(C,N1,L2), spoji(L1,L2,R).

ispisiNivo(_,_,[]):-nl.
ispisiNivo(N,H,[n(_,X)|R]):-N=\=X, ispisiNivo(N,H,R).
ispisiNivo(N,H,[n(B,N)|R]):-P is H-N, pow2(P,P1), X is P1-1, razmaci(X), print(B), razmaci(X), write('_'), ispisiNivo(N,H,R).
ispisi(N,H,L):-ispisiNivo(N,H,L), N1 is N+1, ispisi(N1,H,L).
seminarski(X):-lkd(X,L), sortiraj(L,NL), stablo(NL,NX), visina(NL,H), nivoi(NX,0,NNL), ispisi(0,H,NNL).



/*
ispis(_,N,N).
ispis(s(A,B,C),N,H):-N=<H, P is H-N, pow2(P,P1), R is P1-1, razmaci(R), print(B), razmaci(R), write(' '), N1 is N+1,nl, ispis(A,N1,H), ispis(C,N1,H).
*/