(defun bin-tree-member-p (B E) "Test if E is an element in binary tree B." (if (bin-tree-leaf-p B) (equal E (bin-tree-leaf-element B)) (or (equal E (bin-tree-node-element B)) (bin-tree-member-p (bin-tree-node-left B) E) (bin-tree-member-p (bin-tree-node-right B) E))))
跟踪执行如下:
CL-USER> (trace bin-tree-member-p) (BIN-TREE-MEMBER-P) USER(15): (bin-tree-member-p '(+ (* (2) (3)) (- (7) (8))) 7) 0: (BIN-TREE-MEMBER-P (+ (* (2) (3)) (- (7) (8))) 7) 1: (BIN-TREE-MEMBER-P (* (2) (3)) 7) 2: (BIN-TREE-MEMBER-P (2) 7) 2: returned NIL 2: (BIN-TREE-MEMBER-P (3) 7) 2: returned NIL 1: returned NIL 1: (BIN-TREE-MEMBER-P (- (7) (8)) 7) 2: (BIN-TREE-MEMBER-P (7) 7) 2: returned T 1: returned T 0: returned T T
再来联系一个例子,计算二叉树成员的个数:
(defun bin-tree-size (B) "Return the number of members in binary tree B." (if (bin-tree-leaf-p B) 1 (+ 1 (bin-tree-size (bin-tree-node-left B)) (bin-tree-size (bin-tree-node-right B)))))
(defun fast-bin-tree-preorder (B) "A tail-recursive version of bin-tree-preorder." (preorder-aux B nil))
(defun preorder-aux (B A) "Append A to the end of the list containing elements of B in preorder." (if (bin-tree-leaf-p B) (cons (bin-tree-leaf-element B) A) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (cons elmt (preorder-aux left (preorder-aux right A))))))
(defun bin-tree-postorder (B) "Create a list containing elements of B in postorder." (if (bin-tree-leaf-p B) (list (bin-tree-leaf-element B)) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (append (bin-tree-postorder left) (append (bin-tree-postorder right) (cons elmt nil))))))
(defun fast-bin-tree-postorder (B) "A tail-recursive version of bin-tree-postorder." (postorder-aux B nil))
(defun postorder-aux (B A) "Append A to the end of the list containing elements of B in postorder." (if (bin-tree-leaf-p B) (cons (bin-tree-leaf-element B) A) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (postorder-aux left (postorder-aux right (cons elmt A))))))
(defun bin-tree-inorder (B) "Create a list containing elements of B in inorder." (if (bin-tree-leaf-p B) (list (bin-tree-leaf-element B)) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (append (bin-tree-inorder left) (cons elmt (bin-tree-inorder right))))))
(defun fast-bin-tree-inorder (B) "A tail-recursive version of bin-tree-inorder." (inorder-aux B nil))
(defun inorder-aux (B A) "Append A to the end of the list containing elements of B in inorder." (if (bin-tree-leaf-p B) (cons (bin-tree-leaf-element B) A) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (inorder-aux left (cons elmt (inorder-aux right A))))))
(defun make-empty-BST () "Create an empty BST." nil)
(defun BST-empty-p (B) "Check if BST B is empty." (null B))
考虑到额外的计算约束,相关测试实现如下:
(defun BST-member-p (B E) "Check if E is a member of BST B." (if (BST-empty-p B) nil (BST-nonempty-member-p B E)))
(defun BST-nonempty-member-p (B E) "Check if E is a member of nonempty BST B." (if (bin-tree-leaf-p B) (= E (bin-tree-leaf-element B)) (if (<= E (bin-tree-node-element B)) (BST-nonempty-member-p (bin-tree-node-left B) E) (BST-nonempty-member-p (bin-tree-node-right B) E))))
USER(16): (trace BST-member-p BST-nonempty-member-p) (BST-NONEMPTY-MEMBER-P BST-MEMBER-P) USER(17): (BST-member-p '(2 (1 (1) (2)) (3 (3) (4))) 3) 0: (BST-MEMBER-P (2 (1 (1) (2)) (3 (3) (4))) 3) 1: (BST-NONEMPTY-MEMBER-P (2 (1 (1) (2)) (3 (3) (4))) 3) 2: (BST-NONEMPTY-MEMBER-P (3 (3) (4)) 3) 3: (BST-NONEMPTY-MEMBER-P (3) 3) 3: returned T 2: returned T 1: returned T 0: returned T T
以下函数实现插入功能:
(defun BST-insert (B E) "Insert E into BST B." (if (BST-empty-p B) (make-bin-tree-leaf E) (BST-nonempty-insert B E)))
(defun BST-nonempty-insert (B E) "Insert E into nonempty BST B." (if (bin-tree-leaf-p B) (BST-leaf-insert B E) (let ((elmt (bin-tree-node-element B)) (left (bin-tree-node-left B)) (right (bin-tree-node-right B))) (if (<= E (bin-tree-node-element B)) (make-bin-tree-node elmt (BST-nonempty-insert (bin-tree-node-left B) E) right) (make-bin-tree-node elmt left (BST-nonempty-insert (bin-tree-node-right B) E))))))
(defun BST-leaf-insert (L E) "Insert element E to a BST with only one leaf." (let ((elmt (bin-tree-leaf-element L))) (if (= E elmt) L (if (< E elmt) (make-bin-tree-node E (make-bin-tree-leaf E) (make-bin-tree-leaf elmt)) (make-bin-tree-node elmt (make-bin-tree-leaf elmt) (make-bin-tree-leaf E))))))
(defun sorted-list-empty-p (L) "Test if a sorted list L is empty." (null L))
(defun sorted-list-member-p (L E) "Test if element E is a member of a sorted list L." (if (null L) nil (if (> E (first L)) (sorted-list-member-p (rest L) E) (= E (first L)))))
(defun sorted-list-insert (L E) "Insert element E into a sorted list L to produce a new sorted list." (if (null L) (list E) (if (> E (first L)) (cons (first L) (sorted-list-insert (rest L) E)) (if (= E (first L)) L (cons E L)))))
(defun sorted-list-remove (L E) "Remove element E from sorted list L to produce a new sorted list." (if (null L) nil (if (> E (first L)) (cons (first L) (sorted-list-remove (rest L) E)) (if (= E (first L)) (rest L) L))))