1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % Binomial Heap imlementation based on
4 % Functional Binomial Queues
6 % University of Glasgow
8 % Author
: Tom Schrijvers
9 % Email
: Tom
.Schrijvers
@cs.kuleuven
.ac
.be
10 % Copyright
: K
.U
.Leuven
2004
11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 :- module
(binomialheap
,
22 :- use_module
(library
(lists
)).
24 % data Tree a
= Node a
[Tree a
]
25 % type BinQueue a
= [Maybe
(Tree a
)]
26 % data Maybe a
= Zero
| One a
27 % type Item
= (Entry
,Key
)
37 meld_qc
([],Q
,zero
,Q
) :- !.
38 meld_qc
([],Q
,C
,R
) :- !,
40 meld_qc
(P
,[],C
,R
) :- !,
42 meld_qc
([zero
|Ps
],[zero
|Qs
],C
,R
) :- !,
45 meld_qc
([one
(node
(X
,Xs
))|Ps
],[one
(node
(Y
,Ys
))|Qs
],C
,R
) :- !,
49 T
= node
(X
,[node
(Y
,Ys
)|Xs
])
51 T
= node
(Y
,[node
(X
,Xs
)|Ys
])
54 meld_qc
(Ps
,Qs
,one
(T
),Rs
).
55 meld_qc
([P
|Ps
],[Q
|Qs
],C
,Rs
) :-
56 meld_qc
([Q
|Ps
],[C
|Qs
],P
,Rs
).
59 meld_q
([one
(node
(I
,[]))],Q
,NQ
).
61 insert_list_q
([],Q
,Q
).
62 insert_list_q
([I
|Is
],Q
,NQ
) :-
64 insert_list_q
(Is
,Q1
,NQ
).
66 min_tree
([T
|Ts
],MT
) :-
67 min_tree_acc
(Ts
,T
,MT
).
69 min_tree_acc
([],MT
,MT
).
70 min_tree_acc
([T
|Ts
],Acc
,MT
) :-
72 min_tree_acc
(Ts
,NAcc
,MT
).
76 least
(one
(node
(X
,Xs
)),one
(node
(Y
,Ys
)),T
) :-
86 remove_tree
([T
|Ts
],I
,[NT
|NTs
]) :-
97 remove_tree
(Ts
,I
,NTs
).
99 delete_min_q
(Q
,NQ
,Min
) :-
100 min_tree
(Q
,one
(node
(Min
,Ts
))),
101 remove_tree
(Q
,Min
,Q1
),
107 make_ones
([N
|Ns
],[one
(N
)|RQ
]) :-
111 min_tree
(Q
,one
(node
(I
,_
))).