Presentation is loading. Please wait.

Presentation is loading. Please wait.

מבוא מורחב 1 Representing lists as binary trees 7 3 1 5 9 12 3 1 5 7 9 1 9 7 3 5.

Similar presentations


Presentation on theme: "מבוא מורחב 1 Representing lists as binary trees 7 3 1 5 9 12 3 1 5 7 9 1 9 7 3 5."— Presentation transcript:

1 מבוא מורחב 1 Representing lists as binary trees 7 3 1 5 9 12 3 1 5 7 9 1 9 7 3 5

2 מבוא מורחב 2 Representing lists as binary trees (Cont.) 7 3 1 5 9 12 (define (entry tree) (car tree)) (define (left-branch tree) (cadr tree)) (define (right-branch tree) (caddr tree)) (define (make-tree entry left right) (list entry left right))

3 מבוא מורחב 3 Representing lists as binary trees (Cont.) (define (adjoin-set x set) (cond ((null? set) (make-tree x '() '())) ((= x (entry set)) set) ((< x (entry set)) (make-tree (entry set) (adjoin-set x (left-branch set)) (right-branch set))) ((> x (entry set)) (make-tree (entry set) (left-branch set) (adjoin-set x (right-branch set))))))

4 מבוא מורחב 4 Complexity Element-of-set Adjoin-set Intersection-set Union-set  (n) (n2)(n2) (n2)(n2) unordered ordered  (n) trees  (log(n))  (nlog(n))

5 מבוא מורחב 5 Huffman encoding trees

6 מבוא מורחב 6 fixed length codes Want to represent data as a sequence of 0’s and 1’s for example: BACADAEAFABBAAAGAH A 000 B 001 C 010 D 011 E 100 F 101 G 110 H 111 00100001000001100010000010100000100100000000011 0000111 This is a fixed length code. Can we make the sequence of 0’s and 1’s shorter ?

7 מבוא מורחב 7 variable length prefix codes A 0 B 100 C 1010 D 1011 E 1100 F 1101 G 1110 H 1111 100010100101101100011010100100000111001111 This is a variable length code. How do we decode ? Use prefix codes: No codeword is a prefix of the other

8 8 prefix code binary tree A 0 B 100 C 1010 D 1011 E 1100 F 1101 G 1110 H 1111 A prefix code corresponds to a binary tree with the symbols at the leaves and vice versa. A B CDE FG H

9 9 Representation 17 2 2 9 4 5 2 A B CDE FG H 8 3 1 1 1 1 1 1 {G,H} {E,F} {C,D} {B,C,D} {E,F,G,H} {B,C,D,E,F,G,H} {A,B,C,D,E,F,G,H}

10 מבוא מורחב 10 Representation (Cont.) (define (make-leaf symbol weight) (list 'leaf symbol weight)) (define (leaf? object) (eq? (car object) 'leaf)) (define (symbol-leaf x) (cadr x)) (define (weight-leaf x) (caddr x))

11 מבוא מורחב 11 Representation (Cont.) (define (make-code-tree left right) (list left right (append (symbols left) (symbols right)) (+ (weight left) (weight right)))) (define (left-branch tree) (car tree)) (define (right-branch tree) (cadr tree))

12 מבוא מורחב 12 Representation (Cont.) (define (symbols tree) (if (leaf? tree) (list (symbol-leaf tree)) (caddr tree))) (define (weight tree) (if (leaf? tree) (weight-leaf tree) (cadddr tree)))

13 13 Construction of Huffman tree A B CDE FG H 8 3 1 1 1 1 1 1 2 {G,H} 2 {E,F} 2 {C,D} 4 {E,F,G,H} 5 {B,C,D} 9 {B,C,D,E,F,G,H} 17 {A,B,C,D,E,F,G,H}

14 14 Construction of Huffman tree (generate-huffman-tree '((A 8) (B 3) (C 1) (D 1) (E 1) (F 1) (H 1) (G 1)) ((leaf a 8) ((((leaf g 1) (leaf h 1) (g h) 2) ((leaf f 1) (leaf e 1) (f e) 2) (g h f e) 4) (((leaf d 1) (leaf c 1) (d c) 2) (leaf b 3) (d c b) 5) (g h f e d c b) 9) (a g h f e d c b) 17)

15 15 Construction of Huffman tree (define (generate-huffman-tree pairs) (successive-merge (make-leaf-set pairs))) (define (make-leaf-set pairs) (if (null? pairs) '() (let ((pair (car pairs))) (adjoin-set (make-leaf (car pair) (cadr pair)) (make-leaf-set (cdr pairs))))))

16 16 Construction of Huffman tree (define (adjoin-set x set) (cond ((null? set) (list x)) ((< (weight x) (weight (car set))) (cons x set)) (else (cons (car set) (adjoin-set x (cdr set)))))) (define (successive-merge trees) (if (null? (cdr trees)) (car trees) (let ((smallest (car trees)) (2smallest (cadr trees)) (rest (cddr trees))) (successive-merge (adjoin-set (make-code-tree smallest 2smallest) rest)))))

17 מבוא מורחב 17 Huffman’s encoding trees (Cont.) (define (decode bits tree) (define (decode-1 bits current-branch) (if (null? bits) '() (let ((next-branch (choose-branch (car bits) current-branch))) (if (leaf? next-branch) (cons (symbol-leaf next-branch) (decode-1 (cdr bits) tree)) (decode-1 (cdr bits) next-branch))))) (decode-1 bits tree)) (define (choose-branch bit branch) (cond ((= bit 0) (left-branch branch)) ((= bit 1) (right-branch branch)) (else (error "bad bit -- CHOOSE-BRANCH" bit))))

18 18 Decoding example A B CDE FG H 8 3 1 1 1 1 1 1 2 {G,H} 2 {E,F} 2 {C,D} 4 {E,F,G,H} 5 {B,C,D} 9 {B,C,D,E,F,G,H} 17 {A,B,C,D,E,F,G,H} 10001010

19 מבוא מורחב 19 Midterm last spring (cont) השלם / י את מימוש השגרה (max-ind lst-ind) שמקבלת רשימה שהתקבלה מהפעלת add-ind על רשימת מספרים, ומחזירה את הזוג שבו האיבר השני הוא הגדול ביותר. לדוגמא : (max-ind (add-ind 0 (list 10 17 2 5))) ==> (1. 17) (define (max-ind lst-ind) (accumulate ________________________________ ____________ )) lst-ind (lambda (x y) (if (> (cdr x) (cdr y)) x y)) (car lst-ind)

20 מבוא מורחב 20 Midterm last spring (Cont) תהא lst רשימת מספרים שכל איבריה שונים. הגדר שגרה (break-list lst) שמחזירה רשימה באורך שלוש. האיבר הראשון ברשימה שמוחזרת היא רשימת איברי lst שמופיעים לפני האיבר המירבי של lst. האיבר השני ברשימה הוא האיבר המירבי ב -lst. האיבר השלישי הוא רשימת איברי lst שמופיעים לאחר האיבר המירבי. לדוגמא : (break-list ‘(1 8 3 6 4 9 2 5 7))  ( (1 8 3 6 4) 9 (2 5 7) ) (break-list ‘(5 4 3 2 1))  ( () 5 (4 3 2 1) ) (break-list ‘(7))  ( () 7 () )


Download ppt "מבוא מורחב 1 Representing lists as binary trees 7 3 1 5 9 12 3 1 5 7 9 1 9 7 3 5."

Similar presentations


Ads by Google