計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Representing Sets - Sets as binary trees - binary trees, union set, intersection set

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.3(Example: Representing Sets)、Sets as binary trees、Exercise 2.65の解答を求めてみる。

コード

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 make_tree(entry, left, right) {
    return list(entry, left, right);
}
function entry(tree) {
    return head(tree);
}
function left_branch(tree) {
    return head(tail(tree));
}
function right_branch(tree) {
    return head(tail(tail(tree)));
}
function make_tree(entry, left, right) {
    return list(entry, left, right);
}
function tree_to_list(tree) {
    function copy_to_list(tree, result_list) {
        return is_null(tree)
            ? result_list
            : copy_to_list(
                left_branch(tree),
                pair(
                    entry(tree),
                    copy_to_list(
                        right_branch(tree),
                        result_list
                    )
                ));
    }
    return copy_to_list(tree, null);
}
function list_to_tree(elements) {
    return head(partial_tree(elements, length(elements)));
}
function partial_tree(elts, n) {
    if (n === 0) {
        return pair(null, elts);
    }
    const left_size = Math.floor((n - 1) / 2);
    const left_result = partial_tree(elts, left_size);
    const left_tree = head(left_result);
    const non_left_elts = tail(left_result);
    const right_size = n - (left_size + 1);
    const this_entry = head(non_left_elts);
    const right_result = partial_tree(tail(non_left_elts), right_size);
    const right_tree = head(right_result);
    const remaining_elts = tail(right_result);
    return pair(
        make_tree(this_entry, left_tree, right_tree),
        remaining_elts
    );
}
function union_set(set1, set2) {
    function iter(elts1, elts2) {
        if (is_null(elts1)) {
            return elts2;
        }
        if (is_null(elts2)) {
            return elts1
        }
        const x = head(elts1);
        const y = head(elts2);
        return x < y
            ? pair(x, iter(tail(elts1), elts2))
            : x === y
                ? pair(x, iter(tail(elts1), tail(elts2)))
                : pair(y, iter(elts1, tail(elts2)));
    }
    return list_to_tree(
        iter(
            tree_to_list(set1),
            tree_to_list(set2)
        )
    );
}
function intersection_set(set1, set2) {
    function iter(elts1, elts2) {
        if (is_null(elts1) || is_null(elts2)) {
            return null;
        }
        const x = head(elts1);
        const y = head(elts2);
        return x < y
            ? iter(tail(elts1), elts2)
            : x === y
                ? pair(x, iter(tail(elts1), tail(elts2)))
                : iter(elts1, tail(elts2));
    }
    return list_to_tree(
        iter(
            tree_to_list(set1),
            tree_to_list(set2)
        )
    )
}

const elts1 = list(1, 3, 5, 6, 7, 9, 11);
const elts2 = list(2, 4, 6, 8, 9, 10);
const set1 = list_to_tree(elts1);
const set2 = list_to_tree(elts2);
const u = union_set(set1, set2);
const i = intersection_set(set1, set2);
display(set1);
display(elts1);
display(set2);
display(elts2);
display(u);
display(tree_to_list(u));
display(i);
display(tree_to_list(i));

入出力結果(Terminal, Zsh)

% node answer2.65.js
[6, [[3, [[1, [null, [null, null]]], [[5, [null, [null, null]]], null]]], [[9, [[7, [null, [null, null]]], [[11, [null, [null, null]]], null]]], null]]]
[1, [3, [5, [6, [7, [9, [11, null]]]]]]]
[6, [[2, [null, [[4, [null, [null, null]]], null]]], [[9, [[8, [null, [null, null]]], [[10, [null, [null, null]]], null]]], null]]]
[2, [4, [6, [8, [9, [10, null]]]]]]
[6, [[3, [[1, [null, [[2, [null, [null, null]]], null]]], [[4, [null, [[5, [null, [null, null]]], null]]], null]]], [[9, [[7, [null, [[8, [null, [null, null]]], null]]], [[10, [null, [[11, [null, [null, null]]], null]]], null]]], null]]]
[1, [2, [3, [4, [5, [6, [7, [8, [9, [10, [11, null]]]]]]]]]]]
[6, [null, [[9, [null, [null, null]]], null]]]
[6, [9, null]]
%