本题考察理解递归代码的方法和表达能力。
a. 对有序列表作递归调用,划分为左右两部份,并以左右两部份分别生成左子树和右子树,最终生成平衡二叉树。对(1 3 5 7 9 11)的执行过程如下:
partial-tree (1 .. 11) 6
left-size 2
left-result partial-tree (1 .. 11) 2
left-size 0
left-result partial-tree (1 .. 11) 0 => (() 1 .. 11)
left-tree ()
non-left-elts (1 .. 11)
right-size 1
this-entry 1
right-result partial-tree (3 .. 11) 1
left-size 0
left-result partial-tree (3 .. 11) 0 => (() 3 .. 11)
left-tree ()
non-left-elts (3 .. 11)
right-size 0
this-entry 3
right-result partial-tree (5 .. 11) 0 => (() 5 .. 11)
right-tree ()
remaining-elts (5 .. 11)
=> ((3 () ()) 5 .. 11)
right-tree (3 () ())
remaining-elts (5 .. 11)
=> ((1 () (3 () ())) 5 .. 11)
left-tree (1 () (3 () ()))
non-left-elts (5 .. 11)
right-size 3
this-entry 5
right-result partial-tree (7 .. 11) 3
left-size 1
left-result partial-tree (7 .. 11) 1
left-size 0
left-result partial-tree (7 .. 11) 0 => (() 7 .. 11)
left-tree ()
non-left-elts (7 .. 11)
right-size 0
this-entry 7
right-result partial (9 11) 0 => (() 9 11)
right-tree ()
remaining-elts (9 11)
=> ((7 () ()) 9 11)
left-tree (7 () ())
non-left-elts (9 11)
right-size 1
this-entry 9
right-result partial-tree (11) 1
left-size 0
left-result partial-tree (11) 0 => (() 11)
left-tree ()
non-left-elts (11)
right-size 0
this-entry 11
right-result partial-tree () 0 => (())
right-tree ()
remaining-elts ()
=> ((11 () ()))
right-tree (11 () ())
remaining-elts ()
=> ((9 (7 () ()) (11 () ())))
right-tree (9 (7 () ()) (11 () ()))
remaining-elts ()
=> (5 (1 () (3 () ())) (9 (7 () ()) (11 () ())))
b. order of growth为O(n)