btree.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. ;;; btree: an example recursive Pre-Scheme record type
  2. ;; NOTE: Records are really pointers to a struct, so the recursive use
  3. ;; of btree-node below means the struct contains two pointer fields and
  4. ;; an integer. The constructor allocates and returns a pointer.
  5. (define-record-type btree-node :btree-node
  6. (make-btree left right value)
  7. (left btree-node btree-left)
  8. (right btree-node btree-right)
  9. (value integer btree-value))
  10. ;; XXX: Records can't be created at top-level; "no evaluator for MAKE-RECORD"
  11. ;; (define bnull (make-btree null-pointer null-pointer 0))
  12. (define (btree-equal? a b)
  13. (or (eq? a b)
  14. (and (= (btree-value a) (btree-value b))
  15. (btree-equal? (btree-left a) (btree-left b))
  16. (btree-equal? (btree-right a) (btree-right b)))))
  17. (define (deallocate-btree t)
  18. (if (not (null-pointer? (btree-left t)))
  19. (deallocate-btree (btree-left t)))
  20. (if (not (null-pointer? (btree-right t)))
  21. (deallocate-btree (btree-right t)))
  22. (deallocate t))
  23. (define (main)
  24. (define out (current-output-port))
  25. (define null (null-pointer))
  26. (let* ((a1 (make-btree null null 6))
  27. (b1 (make-btree null null 5))
  28. (c1 (make-btree a1 b1 4))
  29. (a2 (make-btree null null 6))
  30. (b2 (make-btree null null 5))
  31. (c2 (make-btree a2 b2 4)))
  32. (cond ((btree-equal? c1 c2)
  33. (write-string "trees are equal\n" out))
  34. (else
  35. (write-string "trees are not equal\n" out)))
  36. (deallocate-btree c1)
  37. (deallocate-btree c2))
  38. 0)