電腦程式的構造和解釋 練習題2 65

2021-10-05 20:25:52 字數 4767 閱讀 8608

可以用之前的有序表的過程union-set,intersection-set以及平衡樹的轉化過程tree->list、list->tree來寫。

這幾個過程的複雜度都是θ(n),最終過程裡面沒有迭代或者遞迴,所以複雜度還是θ(n)。

(define (union-tree set1 set2)

(list-

>tree (union-

set(tree-

>list set1)

(tree-

>list set2)))

)(define (intersection-tree set1 set2)

(list-

>tree (intersection-

set(tree-

>list set1)

(tree-

>list set2)))

)

附上完整的過程和執行結果

#lang racket

(define (element-of-

set? x set

)(cond (

(null

?set

) #f)((

= x (car set

)) #t)((

< x (car set

)) #f)

(else

(element-of-

set? x (cdr set))

)))(define (adjoin-

set x set

)(cond (

(null

?set

)(list x))(

(= x (car set))

set)((

< x (car set))

(cons x set))

(else

(cons (car set

)(adjoin-

set x (cdr set))

))))

(define (intersection-

set set1 set2)(if

(or (

null

? set1)

(null

? set2)

) '()(

let(

(x1 (car set1)

)(x2 (car set2)))

(cond (

(= x1 x2)

(cons x1 (intersection-

set(cdr set1)

(cdr set2)))

)((< x1 x2)

(intersection-

set(cdr set1) set2))(

(< x2 x1)

(intersection-

set set1 (cdr set2)))

))))

(define (union-

set set1 set2)

(cond (

(null

? set1) set2)((

null

? set2) set1)

(else

(let

((x1 (car set1)

)(x2 (car set2)))

(cond (

(= x1 x2)

(cons x1 (union-

set(cdr set1)

(cdr set2)))

)((< x1 x2)

(cons x1 (union-

set(cdr set1) set2)))

((< x2 x1)

(cons x2 (union-

set set1 (cdr set2)))

))))

))(define (entry tree)

(car tree)

)(define (left-branch tree)

(cadr tree)

)(define (right-branch tree)

(caddr tree)

)(define (make-tree entry left right)

(list entry left right)

)(define (element-of-tree? x set

)(cond (

(null

?set

)false)(

(= x (entry set))

true)(

(< x (entry set))

(element-of-tree? x (left-branch set))

)((> x (entry set))

(element-of-tree? x (right-branch set))

)))(define (adjoin-tree x set

)(cond (

(null

?set

)(make-tree x '() '()

))((

= x (entry set))

set)((

< x (entry set))

(make-tree (entry set

)(adjoin-tree x (left-branch set))

(right-branch set))

)((> x (entry set))

(make-tree (entry set

)(left-branch set

)(adjoin-tree x (right-branch set))

))))

(define (union-tree set1 set2)

(list-

>tree (union-

set(tree-

>list set1)

(tree-

>list set2)))

)(define (intersection-tree set1 set2)

(list-

>tree (intersection-

set(tree-

>list set1)

(tree-

>list set2)))

)(define (tree-

>list tree)

(define (copy-to-list tree result-list)(if

(null

? tree)

result-list

(copy-to-list (left-branch tree)

(cons (entry tree)

(copy-to-list (right-branch tree)

result-list)))

))(copy-to-list tree '())

)(define (list-

>tree elements)

(car (

partial

-tree elements (length elements)))

)(define (

partial

-tree elts n)(if

(= n 0

)(cons '(

) elts)

(let

((left-size (quotient (

- n 1)2

)))(

let(

(left-result (

partial

-tree elts left-size)))

(let

((left-tree (car left-result)

)(non-left-elts (cdr left-result)

)(right-size (

- n (

+ left-size 1))

))(let((

this

-entry (car non-left-elts)

)(right-result (

partial

-tree (cdr non-left-elts)

right-size)))

(let

((right-tree (car right-result)

)(remaining-elts (cdr right-result)))

(cons (make-tree this

-entry left-tree right-tree)

remaining-elts)))

))))

)(define a (list-

>tree '(13

57911

)))(define b (list-

>tree '(46

78910

)))(union-tree a b)

(tree-

>list (union-tree a b)

)

執行結果

'(6(

3(1(

)())

(4()

(5()

()))

)(9(

7()(

8()(

)))(

10()(

11()(

))))

)'(13

4567

891011

)

電腦程式的構造和解釋 練習題1 44

平滑函式的公式為 f s x f x dx f x f x dx 3f s x f x dx f x f x dx 3 fs x f x dx f x f x d x 3他的幾何意義應該是將每個點的導數,變化率變小。說簡單點就是函式那塊曲線彎度比較大,加上這個函式就會減小他的彎度,直到他無限接近於一...

電腦程式的構造和解釋 練習題2 19

lang racket define no more?coin values null coin values define except first denomination coin values cdr coin values define first denomination coin va...

電腦程式的構造和解釋 練習題2 61

跟element of set?一樣的道理,當元素大於當時的set表時,就說明元素根本不在這個表裡,所以將元素直接插入到當時表的前面就可以了。最壞的情況還是把錶迴圈了一遍,最好的情況是只迴圈了一次,這樣平均的步數就是n 2次。define adjoin set x set cond null set...