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

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Applied Algorithmics - week7
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
Huffman Encoding Dr. Bernard Chen Ph.D. University of Central Arkansas.
Problem: Huffman Coding Def: binary character code = assignment of binary strings to characters e.g. ASCII code A = B = C =
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא.
Lecture 6: Huffman Code Thinh Nguyen Oregon State University.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Extended Introduction to CS Preparation for Final Exam.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
CSE 326 Huffman coding Richard Anderson. Coding theory Conversion, Encryption, Compression Binary coding Variable length coding A B C D E F.
Lossless Data Compression Using run-length and Huffman Compression pages
Huffman Codes Message consisting of five characters: a, b, c, d,e
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Huffman Coding Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Compression.  Compression ratio: how much is the size reduced?  Symmetric/asymmetric: time difference to compress, decompress?  Lossless; lossy: any.
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 8. Greedy Algorithms.
Huffman Code and Data Decomposition Pranav Shah CS157B.
1 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Huffman Encodings Section 9.4. Data Compression: Array Representation Σ denotes an alphabet used for all strings Each element in Σ is called a character.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Practice session 5 טיפוסים מורכבים Abstract Data Types הכנה לעבודה 3.
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
Bahareh Sarrafzadeh 6111 Fall 2009
Trees (Ch. 9.2) Longin Jan Latecki Temple University based on slides by Simon Langley and Shang-Hua Teng.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Lossless Decomposition and Huffman Codes Sophia Soohoo CS 157B.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1 Data Compression Hae-sun Jung CS146 Dr. Sin-Min Lee Spring 2004.
Huffman encoding.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Additional Scheme examples
Assignment 6: Huffman Code Generation
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Introduction to Scheme
ISNE101 – Introduction to Information Systems and Network Engineering
COP4020 Programming Languages
Chapter 9: Huffman Codes
Advanced Algorithms Analysis and Design
Lecture #8 מבוא מורחב.
The Metacircular Evaluator (Continued)
Trees Addenda.
Data Structure and Algorithms
topics mutable data structures
CSE 326 Huffman coding Richard Anderson.
6.001 SICP Data abstractions
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Huffman Coding Greedy Algorithm
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

מבוא מורחב 1 Representing lists as binary trees

מבוא מורחב 2 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 Complexity Element-of-set Adjoin-set Intersection-set Union-set  (n) (n2)(n2) (n2)(n2) unordered ordered  (n) trees  (log(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 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 This is a variable length code. How do we decode ? Use prefix codes: No codeword is a prefix of the other

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 Representation A B CDE FG H {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 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 Construction of Huffman tree A B CDE FG H {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 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 Decoding example A B CDE FG H {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}

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

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