Purely Functional Data Structures ex3.3 leftist heap
1. Leftist Heap
Definition
A Leftist (min)Heap is a binary tree that satisfies the follow-ing conditions.
If X is a node and L and R are its left and right children, then:
– X.value ≤ L.value
– X.value ≤ R.value
– null path length of L ≥ null path length of R
where the null path length of a node is the shortest distance between
from that node to a descendant with 0 or 1 child.
If a node is null, its null path length is −1.
4. FromListの実装
(言語ゲーム 2009-11-23 Leftist Heap (http://d.hatena.ne.jp/propella/20091123/p1) のサンプルプログラムに追加して使って下さい)
-- Copied from CS231 Algorithm Prof. Lyn Turbak Wellesley College
-- make a leftist heap with one node.
singleton x = T 1 x E E
-- make a leftist heap from list
fromList :: Ord a => [a] -> Heap a
fromList [] = E
fromList xs = mergeLoop (map singleton xs)
where
mergeLoop [] = error "shouldnt: mergeLoop []"
mergeLoop [t] = t
mergeLoop ts = mergeLoop(mergePairs ts)
mergePairs [] = []
mergePairs [t] = [t]
mergePairs (t1:t2:ts) = (merge t1 t2):(mergePairs ts)
prop_Heap2 :: [Int] -> Bool
prop_Heap2 xs = isSorted (heapToList (fromList xs))
test2 = verboseCheck prop_Heap2