Skip to content

Commit 245bdc0

Browse files
authored
fixes #805 (#1012)
1 parent 9d4ccbd commit 245bdc0

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

xml/chapter3/section3/subsection3.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,102 @@ display(get(list("b")));
826826
(e.g., numerically or alphabetically). (Compare
827827
exercise<SPACE/><REF NAME="ex:set-lookup-binary-tree"/> of chapter<SPACE/><REF NAME="chap:data"></REF>.)
828828
<LABEL NAME="ex:3_26"/>
829+
<SOLUTION>
830+
<SNIPPET>
831+
<NAME>ex_3_26_solution</NAME>
832+
<EXAMPLE>ex_3_26_solution_example</EXAMPLE>
833+
<JAVASCRIPT>
834+
// provided by GitHub user devinryu
835+
836+
function entry(tree) { return head(tree); }
837+
function left_branch(tree) { return head(tail(tree)); }
838+
function right_branch(tree) { return head(tail(tail(tree))); }
839+
function make_tree(entry, left, right) {
840+
return list(entry, left, right);
841+
}
842+
843+
// kv is list(key, value)
844+
function adjoin_set(kv, set) {
845+
return is_null(set)
846+
? make_tree(kv, null, null)
847+
: head(kv) === head(entry(set))
848+
? set
849+
: head(kv) &lt; head(entry(set))
850+
? make_tree(entry(set),
851+
adjoin_set(kv, left_branch(set)),
852+
right_branch(set))
853+
: make_tree(entry(set),
854+
left_branch(set),
855+
adjoin_set(kv, right_branch(set)));
856+
}
857+
858+
function make_table() {
859+
let local_table = null;
860+
function lookup(given_key, tree_of_records) {
861+
if (is_null(tree_of_records)) {
862+
return null;
863+
} else {
864+
const this_entry = entry(tree_of_records);
865+
const this_key = head(this_entry);
866+
return given_key === this_key
867+
? this_entry
868+
: given_key &lt; this_key
869+
? lookup(given_key,
870+
left_branch(tree_of_records))
871+
: lookup(given_key,
872+
right_branch(tree_of_records));
873+
}
874+
}
875+
function insert(k, v) {
876+
let record = lookup(k, local_table);
877+
if(is_null(record)) {
878+
local_table = adjoin_set(list(k, v), local_table);
879+
} else {
880+
// do nothing
881+
}
882+
}
883+
function get(k) {
884+
return head(tail(lookup(k, local_table)));
885+
}
886+
function print() {
887+
return display(local_table);
888+
}
889+
function dispatch(m) {
890+
return m === "lookup"
891+
? get
892+
: m === "insert"
893+
? insert
894+
: m === "print"
895+
? print
896+
: error(m, "error");
897+
}
898+
return dispatch;
899+
}
900+
</JAVASCRIPT>
901+
</SNIPPET>
902+
<SNIPPET HIDE="yes">
903+
<NAME>ex_3_26_solution_example</NAME>
904+
<JAVASCRIPT>
905+
const t = make_table();
906+
const get = t("lookup");
907+
const put = t("insert");
908+
const print = t("print");
909+
910+
// The test results
911+
912+
put(3, "d");
913+
put(1, "a");
914+
put(2, "b");
915+
put(2, "c");
916+
put(4, "e");
917+
put(5, "f");
918+
919+
print();
920+
921+
display(get(2)); // displays: "b"
922+
</JAVASCRIPT>
923+
</SNIPPET>
924+
</SOLUTION>
829925
</EXERCISE>
830926

831927
<EXERCISE>

0 commit comments

Comments
 (0)