我有一個稱爲make-leaf-set的過程,該過程創建葉節點和另一個過程,該過程將最低的first-high排序。將虛線對合併到huffman樹中
(define (make-leaf-set pairs)
(if (null? pairs)
'()
(let ((pair (car pairs)))
(adjoin-set (make-leaf (car pair)
(cdr pair))
(make-leaf-set (cdr pairs))))))
(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))))))
"Predefined dotted paires"
(define pairs '((a . 2) (b . 5) (c . 1) (d . 3) (e . 1) (f . 3)))
=> ((leaf e 1) (leaf c 1) (leaf a 2) (leaf f 3) (leaf d 3) (leaf b 5))
對在使用時可以很好地工作(製作葉組對)。一切都是排序的。 我也用這就叫make碼樹
(define (make-code-tree left right)
(list left
right
(append (symbols left) (symbols right))
(+ (weight left) (weight right))))
(define (symbols tree)
(if (leaf? tree)
(list (symbol-leaf tree))
(caddr tree)))
(define (weight tree)
(if (leaf? tree)
(weight-leaf tree)
(cadddr tree)))
- 的目標是創造這需要對列表,然後創建一個哈夫曼樹的過程另一個程序。
作爲開始我COMED了這個
(define (grow-huffman-tree list)
(successive-merge (make-leaf-set list) '()))
(define (successive-merge par tree)
(if (null? par)
tree
(append (make-code-tree (car par) (cadr par)) tree)))
因爲它位於它合併兩個第一元件和給出((葉ë1)(葉的C 1)(EC)2) 。 但我希望它合併所有樹葉,使它像huffman-tree一樣構建,並且我無法設法將其他樹葉合併到此樹中。我試圖調用(連續合併(cdr par)樹)將導致元素d 3上的錯誤...
我不認爲你已經正確地吸收意見來自:http://stackoverflow.com/questions/15350954/get-pairs-out-of-a-huffman-tree/;你從'make-code-tree'返回的數據類的數據定義在哪裏? (就此而言,你的'leaf?'過程在哪裏定義? – pnkfelix 2013-03-13 02:30:55