SICP練習2.24~2.32です。木に関する練習です。
問題
練習2.24~2.28、練習2.30~2.31: 略
練習2.29:二分木
練習2.32:集合のサブセット
解答
練習2.29の解答(b、cだけ)
- import tango.io.Stdout;
-
-
-
-
- class Mobile {
- Branch left;
- Branch right;
-
- this(Branch l, Branch r) {
- left = l;
- right = r;
- }
- }
-
- class Structure {
- int weight;
- Mobile child;
-
- this(int w, Mobile c) {
- weight = w;
- child = c;
- }
- }
-
- class Branch {
- int length;
- Structure str;
-
- this(int len, Structure s) {
- length = len;
- str = s;
- }
- }
-
-
- int strWeight(Structure s) {
- if (s.child !is null) {
- return strWeight(s.child.left.str) + strWeight(s.child.right.str);
- }
- return s.weight;
- }
-
- int totalWeight(Mobile m) {
- return strWeight(m.left.str) + strWeight(m.right.str);
- }
-
-
- bool isBalance(Mobile m) {
- bool balance;
- Branch left = m.left;
- Branch right = m.right;
- Structure ls = left.str;
- Structure rs = right.str;
-
-
- Mobile lc = ls.child;
- if (lc !is null) {
- balance = isBalance(lc);
- if (!balance) {
- return false;
- }
- }
-
- Mobile rc = rs.child;
- if (rc !is null) {
- balance = isBalance(rc);
- if (!balance) {
- return false;
- }
- }
-
-
- int lt = left.length * strWeight(ls);
- int rt = right.length * strWeight(rs);
- return lt == rt;
- }
-
- void main() {
-
- Structure s1 = new Structure(1, null);
- Structure s2 = new Structure(2, null);
- Structure s3 = new Structure(4, null);
- Structure s4 = new Structure(8, null);
-
- Branch b1 = new Branch(8, s1);
- Branch b2 = new Branch(4, s2);
- Branch b3 = new Branch(2, s3);
- Branch b4 = new Branch(1, s4);
-
- Mobile m1 = new Mobile(b1, b2);
- Mobile m2 = new Mobile(b3, b4);
-
- Structure sa = new Structure(-1, m1);
- Structure sb = new Structure(-1, m2);
-
- Branch ba = new Branch(4, sa);
- Branch bb = new Branch(1, sb);
-
- Mobile root = new Mobile(ba, bb);
-
-
- int tw = totalWeight(root);
- Stdout.formatln("total weight is: {}", tw);
-
-
- bool balance = isBalance(root);
- Stdout.formatln("balance = {}", balance);
- }
import tango.io.Stdout; /** * SICP練習2.29。 */ class Mobile { Branch left; Branch right; this(Branch l, Branch r) { left = l; right = r; } } class Structure { int weight = -1; Mobile child; this(int w, Mobile c) { weight = w; child = c; } } class Branch { int length; Structure str; this(int len, Structure s) { length = len; str = s; } } // b、総重量を求める int strWeight(Structure s) { if (s.child !is null) { return strWeight(s.child.left.str) + strWeight(s.child.right.str); } return s.weight; } int totalWeight(Mobile m) { return strWeight(m.left.str) + strWeight(m.right.str); } // c、バランスかどうか bool isBalance(Mobile m) { bool balance; Branch left = m.left; Branch right = m.right; Structure ls = left.str; Structure rs = right.str; // child is balance? Mobile lc = ls.child; if (lc !is null) { balance = isBalance(lc); if (!balance) { return false; } } Mobile rc = rs.child; if (rc !is null) { balance = isBalance(rc); if (!balance) { return false; } } // self is balance? int lt = left.length * strWeight(ls); int rt = right.length * strWeight(rs); return lt == rt; } void main() { // build a balance mobile Structure s1 = new Structure(1, null); Structure s2 = new Structure(2, null); Structure s3 = new Structure(4, null); Structure s4 = new Structure(8, null); Branch b1 = new Branch(8, s1); Branch b2 = new Branch(4, s2); Branch b3 = new Branch(2, s3); Branch b4 = new Branch(1, s4); Mobile m1 = new Mobile(b1, b2); Mobile m2 = new Mobile(b3, b4); Structure sa = new Structure(-1, m1); Structure sb = new Structure(-1, m2); Branch ba = new Branch(4, sa); Branch bb = new Branch(1, sb); Mobile root = new Mobile(ba, bb); // b int tw = totalWeight(root); Stdout.formatln("total weight is: {}", tw); // c bool balance = isBalance(root); Stdout.formatln("balance = {}", balance); }
実行結果
total weight is: 15
balance = true |
練習2.32の解答
(define (subsets s)
(if (null? s)
(list nil)
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (x) (cons (car s) x)) rest)))))
|