計算機プログラムの構造と解釈(SICP)学習メモ
蒋 いつ峰 2008/09/07   |  プログラミング基礎
第二章 練習2.24~2.32 2008/09/07  |  PK:SICP 練習 2.24~2.32

SICP練習2.24~2.32です。木に関する練習です。


問題

練習2.24~2.28、練習2.30~2.31: 略
練習2.29:二分木
練習2.32:集合のサブセット

解答

練習2.29の解答(b、cだけ)

 
  1. import tango.io.Stdout;  
  2.   
  3. /** 
  4.  * SICP練習2.29。 
  5.  */  
  6. class Mobile {  
  7.     Branch left;  
  8.     Branch right;  
  9.   
  10.     this(Branch l, Branch r) {  
  11.         left = l;  
  12.         right = r;  
  13.     }  
  14. }  
  15.   
  16. class Structure {  
  17.     int weight;  
  18.     Mobile child;  
  19.   
  20.     this(int w, Mobile c) {  
  21.         weight = w;  
  22.         child = c;  
  23.     }  
  24. }  
  25.   
  26. class Branch {  
  27.     int length;  
  28.     Structure str;  
  29.   
  30.     this(int len, Structure s) {  
  31.         length = len;  
  32.         str = s;  
  33.     }  
  34. }  
  35.   
  36. // b、総重量を求める  
  37. int strWeight(Structure s) {  
  38.     if (s.child !is null) {  
  39.         return strWeight(s.child.left.str) + strWeight(s.child.right.str);  
  40.     }  
  41.     return s.weight;  
  42. }  
  43.   
  44. int totalWeight(Mobile m) {  
  45.     return strWeight(m.left.str) + strWeight(m.right.str);  
  46. }  
  47.   
  48. // c、バランスかどうか  
  49. bool isBalance(Mobile m) {  
  50.     bool balance;  
  51.     Branch left = m.left;  
  52.     Branch right = m.right;  
  53.     Structure ls = left.str;  
  54.     Structure rs = right.str;  
  55.   
  56.     // child is balance?  
  57.     Mobile lc = ls.child;  
  58.     if (lc !is null) {  
  59.         balance = isBalance(lc);  
  60.         if (!balance) {  
  61.             return false;  
  62.         }  
  63.     }  
  64.   
  65.     Mobile rc = rs.child;  
  66.     if (rc !is null) {  
  67.         balance = isBalance(rc);  
  68.         if (!balance) {  
  69.             return false;  
  70.         }  
  71.     }  
  72.   
  73.     // self is balance?  
  74.     int lt = left.length * strWeight(ls);  
  75.     int rt = right.length * strWeight(rs);  
  76.     return lt == rt;  
  77. }  
  78.   
  79. void main() {  
  80.     // build a balance mobile  
  81.     Structure s1 = new Structure(1, null);  
  82.     Structure s2 = new Structure(2, null);  
  83.     Structure s3 = new Structure(4, null);  
  84.     Structure s4 = new Structure(8, null);  
  85.   
  86.     Branch b1 = new Branch(8, s1);  
  87.     Branch b2 = new Branch(4, s2);  
  88.     Branch b3 = new Branch(2, s3);  
  89.     Branch b4 = new Branch(1, s4);  
  90.   
  91.     Mobile m1 = new Mobile(b1, b2);  
  92.     Mobile m2 = new Mobile(b3, b4);  
  93.   
  94.     Structure sa = new Structure(-1, m1);  
  95.     Structure sb = new Structure(-1, m2);  
  96.   
  97.     Branch ba = new Branch(4, sa);  
  98.     Branch bb = new Branch(1, sb);  
  99.   
  100.     Mobile root = new Mobile(ba, bb);  
  101.   
  102.     // b  
  103.     int tw = totalWeight(root);  
  104.     Stdout.formatln("total weight is: {}", tw);  
  105.   
  106.     // c  
  107.     bool balance = isBalance(root);  
  108.     Stdout.formatln("balance = {}", balance);  
  109. }  

実行結果

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)))))
 


閲覧  |  コメント  |  目次

 
ヘルプ  |  ご利用規約  |  相互リンク  |  問合せ
リンクはご自由に、問合せはお気軽に
©2007 Uprush