Created
August 3, 2016 08:57
-
-
Save nikibobi/06dc26cff46e05eb554b8256eac284ce to your computer and use it in GitHub Desktop.
F# pairing heap implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module PairHeap | |
| type Heap<'a> = | |
| | Empty | |
| | Heap of 'a * (Heap<'a> list) | |
| let top = | |
| function | |
| | Empty -> failwith "empty" | |
| | Heap (c,_) -> c | |
| let merge heap1 heap2 = | |
| match heap1,heap2 with | |
| | Empty,_ -> heap2 | |
| | _,Empty -> heap1 | |
| | Heap (c1,sub1),Heap (c2,sub2) -> | |
| if c1 > c2 then | |
| Heap (c1,heap2::sub1) | |
| else | |
| Heap (c2,heap1::sub2) | |
| let insert c heap = | |
| merge (Heap (c,[])) heap | |
| let rec mergePairs = | |
| function | |
| | [] -> Empty | |
| | [sub] -> sub | |
| | sub1::sub2::rest -> merge (merge sub1 sub2) (mergePairs rest) | |
| let popTop = | |
| function | |
| | Empty -> failwith "empty" | |
| | Heap (_,sub) -> mergePairs sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment