Upcoming SlideShare
×

# PFDS 9.2.3 Lazy Representations

402 views

Published on

PFDS 9.2.3 La

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
402
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### PFDS 9.2.3 Lazy Representations

1. 1. 9.2.3 Lazy Representations
2. 2. 9.2.1 Binary Random-Access Lists
3. 3. 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) ;;
4. 4. 9.2.2 Zeroless Representations
5. 5. 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) ;;
6. 6. 9.2.3 Lazy RepresentationsBinomial Heapsに遅延評価を導入したら、挿入がAmortized timeでO(1)になった。 Binary Random Access List の cons 関数も O(1) Amortized Timeで実行可能なはず。
7. 7. Amortized Analysisする中で、データ構造をPersistentな使い方に対応させるには、遅延評価を使って関数をIncrementalに変換する。
8. 8. 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))) ;;
9. 9. 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))) ;;
10. 10. 最下位桁から1が連続するとき、その連続部分の数の表現は一種類だけ。つまり、すべてが1だけで構成される数のとき、表現は1つに収束する。例えば、111と1111の間の数は様々な表現をもつが、両端の111と1111はひとつずつしかない。