2015-11-07 77 views
-4

目標是創建一個huffman樹(谷歌,如果你不知道它是什麼),其輸出不包含值的權重,只是位於由佔位符「內部「與。我創建了一個看起來正確的工作函數,除了每個「內部」外,還有一些額外的空列表不應存在。如果有人可以看看代碼,看看我的錯誤或優化它的方式,我會很感激。R5RS Scheme,霍夫曼樹函數

(define (build-huffman lst) 
    (let ((x (insert-list-of-pairs lst '()))) 
    (define (huffman-help heap) 
     (if (null? (remove-min heap)) 
      heap 
      (let ((rx (remove-min heap))) 
      (if (list? (h-min heap)) 
       (huffman-help (insert (cons (make-internal-node (value (h-min heap)) 
                   (value (h-min rx))) 
              (+ (weight (h-min rx)) 
               (weight (h-min heap)))) 
             (remove-min rx))) 
       (huffman-help (insert (cons (make-internal-node (create-heap (value (h-min heap)) '() '()) 
                   (create-heap (value (h-min rx)) '() '())) 
              (+ (weight (h-min rx)) 
              (weight (h-min heap)))) 
             (remove-min rx))))))) 
    (car (car (huffman-help x))))) 

一些功能是自我解釋,我知道這個問題是這樣的代碼沒有任何其它函數中。

(插入列表對) - 支持一個列表對,例如。 「((no。7)(yes。4))」,並將其插入堆中。

(插入) - 將一對插入堆中。

(remove-min) - 刪除堆的最小值。

(使內部節點0樹1樹) - >(名單「內部0樹1樹)

+0

你注意到,像你這樣做並且寫下「谷歌如果你不知道問題是什麼」是相當誘人的? ;-) – Marged

+0

我只問谷歌什麼是哈夫曼樹,因爲維基百科或任何其他來源將能夠比我更好地解釋它。谷歌搜索這個問題沒有帶來任何結果。 – MitchG

回答

0

你有尋找一種模式?在我看來,在沒有更多相同節點添加到樹中之後,您的代碼會添加空列表。它看起來像作業,所以我不能給你一個答案,但看看你的代碼,看看你是否可以告訴它在添加最後一個節點的權重後停止。