1 / 20

Representing lists as binary trees

3. 1. 7. 3. 7. 1. 9. 3. 5. 9. 5. 12. 5. 1. 12. 7. 9. 12. Representing lists as binary trees. 7. 9. 3. 12. 5. 1. Representing lists as binary trees (Cont.). ( define (entry tree) (car tree)) (define (left-branch tree) (cadr tree))

tracen
Download Presentation

Representing lists as binary trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 7 9 3 12 5 1 Representing lists as binary trees (Cont.) (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. 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. unordered ordered Element-of-set Adjoin-set Intersection-set Union-set (n) (n) (n) (n) (n) (n2) (n) (n2) Complexity trees (log(n)) (log(n)) (nlog(n)) (nlog(n)) מבוא מורחב

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

  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 001000010000011000100000101000001001000000000110000111 This is a fixed length code. Can we make the sequence of 0’s and 1’s shorter ? מבוא מורחב

  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. A B C D E F G H 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.

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

  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. 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. 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. {A,B,C,D,E,F,G,H} 17 9 {B,C,D,E,F,G,H} 4 {E,F,G,H} 5 {B,C,D} 2 2 2 {C,D} {G,H} {E,F} C D E F G B A H 1 1 1 1 3 1 1 8 Construction of Huffman tree

  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. 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. 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. 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. {A,B,C,D,E,F,G,H} 17 9 {B,C,D,E,F,G,H} 4 {E,F,G,H} 5 {B,C,D} 2 2 2 {C,D} {G,H} {E,F} C D E F G B A H 1 1 1 1 3 1 1 8 Decoding example 10001010

  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 ________________________________ ____________ ____________ )) (lambda (x y) (if (> (cdr x) (cdr y)) x y)) (car lst-ind) lst-ind מבוא מורחב

  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 () ) מבוא מורחב

More Related