計算機科学のブログ

Building Abstractions with Data - Symbolic Data - Example: Representing Sets - Sets as binary trees - tree to list

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

コード

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 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 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_1(tree) {
    return is_null(tree)
        ? null
        : append(
            tree_to_list_1(left_branch(tree)),
            pair(entry(tree),
                tree_to_list_1(right_branch(tree)))
        );
}
function tree_to_list_2(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);
}

// 同じとは限らない。
const tree = make_tree(
    1,
    null,
    make_tree(
        3,
        make_tree(2, null, null),
        make_tree(4, null, null)
    )
);
const tree1 = make_tree(
    7,
    make_tree(
        3,
        make_tree(1, null, null),
        make_tree(5, null, null)
    ),
    make_tree(
        9,
        null,
        make_tree(11, null, null)
    )
);
const tree2 = make_tree(
    3,
    make_tree(1, null, null),
    make_tree(
        7,
        make_tree(5, null, null),
        make_tree(
            9,
            null,
            make_tree(11, null, null)
        )
    )
);
const tree3 = make_tree(
    5,
    make_tree(
        3,
        make_tree(1, null, null),
        null
    ),
    make_tree(
        9,
        make_tree(7, null, null),
        make_tree(11, null, null)
    )
);
function p(tree) {
    display("*****");
    display(tree);
    display(tree_to_list_1(tree));
    display(tree_to_list_2(tree));
}
// a. 同じ結果になる
p(tree1);
p(tree2);
p(tree3);

// b. tree_to_list1の方が遅い

入出力結果(Terminal, Zsh)

% node answer2.63.js
*****
[7, [[3, [[1, [null, [null, null]]], [[5, [null, [null, null]]], null]]], [[9, [null, [[11, [null, [null, null]]], null]]], null]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
*****
[3, [[1, [null, [null, null]]], [[7, [[5, [null, [null, null]]], [[9, [null, [[11, [null, [null, null]]], null]]], null]]], null]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
*****
[5, [[3, [[1, [null, [null, null]]], [null, null]]], [[9, [[7, [null, [null, null]]], [[11, [null, [null, null]]], null]]], null]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
[1, [3, [5, [7, [9, [11, null]]]]]]
%