17
9.2.3 Lazy Representations

PFDS 9.2.3 Lazy Representations

Embed Size (px)

DESCRIPTION

PFDS 9.2.3 La

Citation preview

Page 1: PFDS 9.2.3 Lazy Representations

9.2.3 Lazy Representations

Page 2: PFDS 9.2.3 Lazy Representations

9.2.1 Binary Random-Access Lists

Page 3: PFDS 9.2.3 Lazy Representations

type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree type rlist = Element.t digit list let rec consTree = function | (t, []) -> [ONE t] | (t, ZERO :: ts) -> ONE t :: ts | (t1, ONE t2 :: ts) -> ZERO :: consTree (link (t1, t2), ts) ;; let rec unconsTree = function | [] -> raise Empty | [ONE t] -> (t, []) | (ONE t :: ts) -> (t, ZERO :: ts) | (ZERO :: ts) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, ONE t2 :: ts') ;;

Page 4: PFDS 9.2.3 Lazy Representations

9.2.2 Zeroless Representations

Page 5: PFDS 9.2.3 Lazy Representations

type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ONE of 'a tree | TWO of 'a tree * 'a tree type rlist = Elem.t digit list let rec consTree = function | (t, []) -> [ONE t] | (t1, ONE t2 :: ts) -> TWO (t1, t2) :: ts | (t1, TWO (t2, t3) :: ts) -> ONE t1 :: consTree (link (t2, t2), ts) ;; let rec unconsTree = function | [] -> raise Empty | [ONE t] -> (t, []) | (ONE t1 :: ts) -> let (NODE (_, t2, t3), ts') = unconsTree ts in (t1, TWO (t2, t3) :: ts') | (TWO (t1, t2) :: ts) -> (t1, ONE t2 :: ts) ;;

Page 6: PFDS 9.2.3 Lazy Representations

9.2.3 Lazy Representations

Binomial Heapsに遅延評価を導入したら、 挿入がAmortized timeでO(1)になった。

Binary Random Access List の cons 関数も O(1) Amortized Timeで実行可能なはず。

Page 7: PFDS 9.2.3 Lazy Representations

Amortized Analysisする中で、 データ構造をPersistentな使い方に対応させるには、 遅延評価を使って関数をIncrementalに変換する。

Page 8: PFDS 9.2.3 Lazy Representations

type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree type rlist = Elem.t digit S.stream let rec consTree = function | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil)) | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, ts)))) | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts)) ;; let rec unconsTree = function | (lazy S.Nil) -> raise Empty | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil) | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts)))) | (lazy (S.Cons (ZERO, ts))) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, lazy (S.Cons (ONE t2, ts'))) ;;

Page 9: PFDS 9.2.3 Lazy Representations
Page 10: PFDS 9.2.3 Lazy Representations
Page 11: PFDS 9.2.3 Lazy Representations
Page 12: PFDS 9.2.3 Lazy Representations
Page 13: PFDS 9.2.3 Lazy Representations

type 'a tree = LEAF of 'a | NODE of int * 'a tree * 'a tree type 'a digit = ZERO | ONE of 'a tree | TWO of 'a tree * 'a tree type rlist = Elem.t digit S.stream let rec consTree = function | (t, lazy S.Nil) -> lazy (S.Cons (ONE t, lazy S.Nil)) | (t1, lazy (S.Cons (TWO (t2, t3), ts))) -> lazy (S.Cons (ONE t1, consTree (link (t2, t3), ts))) | (t1, lazy (S.Cons (ONE t2, ts))) -> lazy (S.Cons (TWO (t1, t2), ts)) | (t, lazy (S.Cons (ZERO, ts))) -> lazy (S.Cons (ONE t, ts)) ;; let rec unconsTree = function | (lazy S.Nil) -> raise Empty | (lazy (S.Cons (TWO (t1, t2), ts))) -> (t1, lazy (S.Cons (ONE t2, ts))) | (lazy (S.Cons (ONE t, lazy S.Nil))) -> (t, lazy S.Nil) | (lazy (S.Cons (ONE t, ts))) -> (t, (lazy (S.Cons (ZERO, ts)))) | (lazy (S.Cons (ZERO, ts))) -> let (NODE (_, t1, t2), ts') = unconsTree ts in (t1, lazy (S.Cons (ONE t2, ts'))) ;;

Page 14: PFDS 9.2.3 Lazy Representations
Page 15: PFDS 9.2.3 Lazy Representations
Page 16: PFDS 9.2.3 Lazy Representations

最下位桁から1が連続するとき、その連続部分の数の表現は一種類だけ。 つまり、すべてが1だけで構成される数のとき、表現は1つに収束する。 例えば、111と1111の間の数は様々な表現をもつが、 両端の111と1111はひとつずつしかない。

Page 17: PFDS 9.2.3 Lazy Representations