Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4624694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:09:55+00:00 2026-05-22T03:09:55+00:00

I’m suffering with this problem for a few days now. How can you build

  • 0

I’m suffering with this problem for a few days now. How can you build a tree with the data as specified on the following site:

http://www.impulseadventure.com/photo/jpeg-huffman-coding.html, under the topic:

The actual DHT in the JPEG file

I’ll reexplain it shortly here so,

You have :

  1. a table with lengths (bytesvector)
  2. a table with data (bytesvector as well)

Now I want to build a binary tree with these two arguments. Filled everytime from left to right with the data for the corresponding length. The deeper you go into the tree the longer your lengths. Lengths vary from 1 – 16. Take a look at the site and it should become clear.

Now I want to make such a tree in Scheme/Racket so I can walk to the tree and build a table for each encoded value.

The tree I have in my mind would look like:

'((x01 x02)((x03 (x11 x04))(((x00 ...)(...)))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:09:56+00:00Added an answer on May 22, 2026 at 3:09 am
    #lang r6rs
    
    (library
     (huffman-table)
     (export make-table find)
     (import (rnrs base (6))
             (rnrs io simple)
             (only (racket base) bytes bytes-length bytes-ref make-hash hash-set! hash-ref do)
             (rnrs mutable-pairs (6)))
    
     (define (make-node left right)
       (list left right))
     (define (left node)
       (car node))
     (define (right node)
       (cadr node))
     (define (left! node left)
       (set-car! node left)
       left)
     (define (right! node right)
       (set-car! (cdr node) right)
       right)
     (define (node? object)
       (eq? (car object) 'node))
    
     (define (make-leaf value)
       (list 'leaf value))
     (define (value leaf)
       (cadr leaf))
     (define (leaf? object)
       (eq? (car object) 'leaf))
    
     (define (generate-pairs lengths data)
       (define length (bytes-length lengths))
       (let out-loop ((l-idx 0)
                      (d-idx 0)
                      (res '()))
         (if (= l-idx length)
             (reverse res)
             (let in-loop 
               ((t 0)
                (amt (bytes-ref lengths l-idx))
                (temp-res '()))
               (if (= t amt)
                   (out-loop (+ l-idx 1)(+ d-idx (bytes-ref lengths l-idx))(cons temp-res res))
                   (in-loop (+ t 1) amt (cons (bytes-ref data (+ d-idx t)) temp-res)))))))
    
    
     (define (add-nodes node-lst)
       (let loop ((added-nodes '())
                  (node-lst node-lst))
         (cond ((null? node-lst) (reverse added-nodes))
               (else (let ((node (car node-lst))
                           (left-child (make-node '() '()))
                           (right-child (make-node '() '())))
                       (if (null? (left node))
                           (begin (left! node left-child)
                                  (right! node right-child)
                                  (loop (cons right-child (cons left-child added-nodes))
                                        (cdr node-lst)))
                           (begin (right! node right-child)
                                  (loop (cons right-child added-nodes)
                                        (cdr node-lst)))))))))
    
     (define (label-nodes! node-lst values)
       (let loop ((node-lst node-lst)
                  (values values))
         (cond ((null? values) node-lst)
               ((null? (cdr values))(if (null? (left (car node-lst)))
                                        (left! (car node-lst) (car values))
                                        (right! (car node-lst) (car values)))
                                    node-lst)
               (else (if (null? (left (car node-lst)))
                         (begin (left!  (car node-lst) (car  values))
                                (right! (car node-lst) (cadr values))
                                (loop (cdr node-lst)(cddr values)))
                         (begin (right! (car node-lst)(make-leaf (car values)))
                                (loop (cdr node-lst)(cdr values))))))))
    
     (define (make-tree pairs)
       (define root (make-node '() '()))
       ;(define curr-nodes (list root))
       (let loop ((curr-nodes (list root))
                  (pairs pairs))
         (cond 
           ((null? pairs) root)
           (else (loop (add-nodes (label-nodes! curr-nodes (car pairs)))
                       (cdr pairs))))))
    
     (define (atom? el)
       (not (pair? el)))
    
     (define (add bit bitstr) 
       (if bitstr 
           (string-append (number->string bit) bitstr) 
           #f))
    
     (define (code symbol tree) 
       (cond ((null? tree) #f)
             ((atom? tree) (if (= tree symbol)
                               ""
                               #f))
             (else (or (add 0 (code symbol (left tree))) 
                       (add 1 (code symbol (right tree)))))))
    
     (define (make-table lengths data)
       (define pairs (generate-pairs lengths data))
       (define tree (make-tree pairs))
       (define table (make-hash))
       (do ((i 0 (+ i 1)))
         ((= i (bytes-length data)) table)
         (let ((val (bytes-ref data i)))
           (hash-set! table (code val tree) val))))
    
     (define (find table bitstring)
       (hash-ref table bitstring #f))
    
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.