計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Huffman Encoding Trees - Sets of weighted elements - bits

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.71の解答を求めてみる。

コード

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, symbols(left))) {
            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))
        )
    );
}
function make_pairs(n) {
    return n === 0
        ? null
        : pair(
            list(
                String.fromCharCode("a".charCodeAt(0) + n - 1),
                2 ** (n - 1)
            ),
            make_pairs(n - 1)
        );
}
const sample_pairs5 = make_pairs(5);
const sample_tree5 = generate_huffman_tree(sample_pairs5);
const sample_message5 = list("a", "e");
const encoded5 = encode(sample_message5, sample_tree5);
const decoded5 = decode(encoded5, sample_tree5);

display(sample_pairs5);
display(sample_tree5);
display(sample_message5);
display(encoded5);
display(decoded5);

const sample_pairs10 = make_pairs(10);
const sample_tree10 = generate_huffman_tree(sample_pairs10);
const sample_message10 = list("a", "j");
const encoded10 = encode(sample_message10, sample_tree10);
const decoded10 = decode(encoded10, sample_tree10);

display(sample_pairs10);
display(sample_tree10);
display(sample_message10);
display(encoded10);
display(decoded10);

入出力結果(Terminal, Zsh)

% node answer2.71.js
[[e, [16, null]], [[d, [8, null]], [[c, [4, null]], [[b, [2, null]], [[a, [1, null]], null]]]]]
[code_tree, [[code_tree, [[code_tree, [[code_tree, [[leaf, [a, [1, null]]], [[leaf, [b, [2, null]]], [[a, [b, null]], [3, null]]]]], [[leaf, [c, [4, null]]], [[a, [b, [c, null]]], [7, null]]]]], [[leaf, [d, [8, null]]], [[a, [b, [c, [d, null]]]], [15, null]]]]], [[leaf, [e, [16, null]]], [[a, [b, [c, [d, [e, null]]]]], [31, null]]]]]
[a, [e, null]]
[0, [0, [0, [0, [1, null]]]]]
[a, [e, null]]
[[j, [512, null]], [[i, [256, null]], [[h, [128, null]], [[g, [64, null]], [[f, [32, null]], [[e, [16, null]], [[d, [8, null]], [[c, [4, null]], [[b, [2, null]], [[a, [1, null]], null]]]]]]]]]]
[code_tree, [[code_tree, [[code_tree, [[code_tree, [[code_tree, [[code_tree, [[code_tree, [[code_tree, [[code_tree, [[leaf, [a, [1, null]]], [[leaf, [b, [2, null]]], [[a, [b, null]], [3, null]]]]], [[leaf, [c, [4, null]]], [[a, [b, [c, null]]], [7, null]]]]], [[leaf, [d, [8, null]]], [[a, [b, [c, [d, null]]]], [15, null]]]]], [[leaf, [e, [16, null]]], [[a, [b, [c, [d, [e, null]]]]], [31, null]]]]], [[leaf, [f, [32, null]]], [[a, [b, [c, [d, [e, [f, null]]]]]], [63, null]]]]], [[leaf, [g, [64, null]]], [[a, [b, [c, [d, [e, [f, [g, null]]]]]]], [127, null]]]]], [[leaf, [h, [128, null]]], [[a, [b, [c, [d, [e, [f, [g, [h, null]]]]]]]], [255, null]]]]], [[leaf, [i, [256, null]]], [[a, [b, [c, [d, [e, [f, [g, [h, [i, null]]]]]]]]], [511, null]]]]], [[leaf, [j, [512, null]]], [[a, [b, [c, [d, [e, [f, [g, [h, [i, [j, null]]]]]]]]]], [1023, null]]]]]
[a, [j, null]]
[0, [0, [0, [0, [0, [0, [0, [0, [0, [1, null]]]]]]]]]]
[a, [j, null]]
%