計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Huffman Encoding Trees - Sets of weighted elements - symbol-frequency pairs

Structure and Interpretation of Computer Programs: JavaScript Edition(Harold Abelson(著)、Gerald Jay Sussman(著)、Julie Sussman(著)、The MIT Press)のChapter 2(Building Abstractions with Data)、2.3(Symbolic Data)、2.3.4(Example: Huffman Encoding Trees)、Sets of weighted elements、Exercise 2.69の解答を求めてみる。

コード

function stringfy(x) {
    if (is_null(x)) {
        return "null";
    }
    if (is_pair(x)) {
        return "[" + stringfy(head(x)) + ", " + stringfy(tail(x)) + "]";
    }
    if (x === undefined) {
        return "undefined";
    }
    return x.toString();
}
function display(x) {
    return console.log(stringfy(x));
}
function pair(x, y) {
    return [x, y];
}
function head(z) {
    return z[0];
}
function tail(z) {
    return z[1];
}
function is_pair(x) {
    return Array.isArray(x);
}
function length(x) {
    function iter(x, n) {
        return is_null(x) ?
            n :
            iter(tail(x), n + 1);
    }
    return iter(x, 0);
}
function list(...args) {
    return args.length === 0 ?
        null :
        pair(args[0], list(...args.slice(1)));
}
function is_null(x) {
    return x === null;
}
function append(list1, list2) {
    return is_null(list1)
        ? list2
        : pair(head(list1), append(tail(list1), list2));
}
function make_leaf(symbol, weight) {
    return list("leaf", symbol, weight);
}
function is_leaf(object) {
    return head(object) === "leaf";
}
function symbol_leaf(x) {
    return head(tail(x));
}
function weight_leaf(x) {
    return head(tail(tail(x)));
}
function make_code_tree(left, right) {
    return list("code_tree", left, right,
        append(symbols(left), symbols(right)),
        weight(left) + weight(right));
}
function left_branch(tree) {
    return head(tail(tree));
}
function right_branch(tree) {
    return head(tail(tail(tree)));
}
function symbols(tree) {
    return is_leaf(tree)
        ? list(symbol_leaf(tree))
        : head(tail(tail(tail(tree))));
}
function weight(tree) {
    return is_leaf(tree)
        ? weight_leaf(tree)
        : head(tail(tail(tail(tail(tree)))));
}
function decode(bits, tree) {
    function decode_1(bits, current_branch) {
        if (is_null(bits)) {
            return null;
        }
        const next_branch = choose_branch(
            head(bits),
            current_branch
        );
        return is_leaf(next_branch)
            ? pair(symbol_leaf(next_branch), decode_1(tail(bits), tree))
            : decode_1(tail(bits), next_branch);
    }
    return decode_1(bits, tree);
}
function choose_branch(bit, branch) {
    return bit === 0
        ? left_branch(branch)
        : bit === 1
            ? right_branch(branch)
            : error(bit, "bad bit -- choose_branch");
}
function encode(message, tree) {
    return is_null(message)
        ? null
        : append(
            encode_symbol(head(message), tree),
            encode(tail(message), tree)
        );
}
function is_element(elt, elts) {
    return is_null(elts)
        ? false
        : head(elts) === elt
            ? true
            : is_element(elt, tail(elts));
}
function encode_symbol(symbol, tree) {
    function iter(tree) {
        if (is_leaf(tree)) {
            return null;
        }
        const left = left_branch(tree);
        if (is_element(symbol, left_branch(tree))) {
            return pair(0, iter(left));
        }
        const right = right_branch(tree);
        return pair(1, iter(right));
    }
    if (!is_element(symbol, symbols(tree))) {
        return error(symbol, "symobl is not found");
    }
    return iter(tree);
}
function generate_huffman_tree(pairs) {
    return successive_merge(make_leaf_set(pairs));
}
function adjoin_set(x, set) {
    return is_null(set)
        ? list(x)
        : weight(x) < weight(head(set))
            ? pair(x, set)
            : pair(head(set), adjoin_set(x, tail(set)));
}
function make_leaf_set(pairs) {
    if (is_null(pairs)) {
        return null;
    }
    const first_pair = head(pairs);
    return adjoin_set(
        make_leaf(
            head(first_pair),
            head(tail(first_pair))
        ),
        make_leaf_set(tail(pairs))
    );
}
function successive_merge(set) {
    if (is_null(tail(set))) {
        return head(set);
    }
    return successive_merge(
        adjoin_set(
            make_code_tree(head(set), head(tail(set))),
            tail(tail(set))
        )
    );
}
const tree = make_code_tree(
    make_leaf("A", 4),
    make_code_tree(
        make_leaf("B", 2),
        make_code_tree(
            make_leaf("D", 1),
            make_leaf("C", 1)
        )
    )
);
const sample_pairs = list(
    list("A", 4),
    list("B", 2),
    list("C", 1),
    list("D", 1),
);
const sample_tree = generate_huffman_tree(sample_pairs);
display(sample_pairs);
display(tree);
display(generate_huffman_tree(sample_pairs));
const sample_message = list(0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0);
const decoded = decode(sample_message, sample_tree);
const encoded = encode(decoded, sample_tree);

display(sample_message);
display(decoded);
display(encoded);

入出力結果(Terminal, Zsh)

% node answer2.69.js
[[A, [4, null]], [[B, [2, null]], [[C, [1, null]], [[D, [1, null]], null]]]]
[code_tree, [[leaf, [A, [4, null]]], [[code_tree, [[leaf, [B, [2, null]]], [[code_tree, [[leaf, [D, [1, null]]], [[leaf, [C, [1, null]]], [[D, [C, null]], [2, null]]]]], [[B, [D, [C, null]]], [4, null]]]]], [[A, [B, [D, [C, null]]]], [8, null]]]]]
[code_tree, [[leaf, [A, [4, null]]], [[code_tree, [[leaf, [B, [2, null]]], [[code_tree, [[leaf, [D, [1, null]]], [[leaf, [C, [1, null]]], [[D, [C, null]], [2, null]]]]], [[B, [D, [C, null]]], [4, null]]]]], [[A, [B, [D, [C, null]]]], [8, null]]]]]
[0, [1, [1, [0, [0, [1, [0, [1, [0, [1, [1, [1, [0, null]]]]]]]]]]]]]
[A, [D, [A, [B, [B, [C, [A, null]]]]]]]
[0, [1, [1, [0, [0, [1, [0, [1, [0, [1, [1, [1, [0, null]]]]]]]]]]]]]
%