Skip to content

Commit e55f680

Browse files
committed
feat: Year 2013 mergeHeaps
1 parent dcdd9d2 commit e55f680

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Year2013/Exam.hs

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ extractMin :: Ord a => BinHeap a -> a
3131
extractMin = minimum . map key
3232

3333
mergeHeaps :: Ord a => BinHeap a -> BinHeap a -> BinHeap a
34-
mergeHeaps
35-
= undefined
34+
mergeHeaps h1@(t1 : ts1) h2@(t2 : ts2)
35+
| r1 < r2 = t1 : mergeHeaps ts1 h2
36+
| r1 > r2 = t2 : mergeHeaps h1 ts2
37+
| otherwise = mergeHeaps [combineTrees t1 t2] (mergeHeaps ts1 ts2)
38+
where
39+
(r1, r2) = (rank t1, rank t2)
40+
mergeHeaps h1 h2 = h1 <> h2 -- > To reach here, one of the heaps must be empty
3641

3742
insert :: Ord a => a -> BinHeap a -> BinHeap a
3843
insert

0 commit comments

Comments
 (0)