-
Notifications
You must be signed in to change notification settings - Fork 1
/
binarytree.scm
37 lines (23 loc) · 1.33 KB
/
binarytree.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;Copyright (C) 2010 Massachusetts Institute of Technology
;This is free software; see the source for copying conditions. There is NO
;warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;Image saved on Tuesday March 9, 2010 at 6:41:34 PM
; Release 9.0.1 || Microcode 15.1 || Runtime 15.7 || SF 4.41 || LIAR/i386 4.118
;Edwin 3.116
;You are in an interaction window of the Edwin editor.
;Type `C-h' for help, or `C-h t' for a tutorial.
;`C-h m' will describe some commands.
;`C-h' means: hold down the Ctrl key and type `h'.
;Package: (user)
(define (bstc l) (if (null? l) '() (list (car l) (bstc (lesser (cdr l) (car l))) (bstc (greater (cdr l) (car l)))))) ; FUNCTION FOR CREATING BINARY TREE
;Value: bstc
(define (greater l a) (if (null? l) '() (if (>= (car l) a) (cons (car l) (greater (cdr l) a)) (greater (cdr l) a)))) ; RETURNS ALL THE GREATER ELEMENTS IN LIST
;Value: greater
(define (lesser l a) (if (null? l) '() (if (< (car l) a) (cons (car l) (lesser (cdr l) a)) (lesser (cdr l) a)))) ; RETURNS ALL SMALLER ELEMENTS IN THE LIST
;Value: lesser
(bstc '(5 2 6))
;Value 14: (5 (2) (6))
(define (bstsearch l a) (if (null? l) #f (if (= (car l) a) #t (if (< a (car l)) (bstsearch (cadr l) a) (bstsearch (cddr l) a))))) ; FUNCTION FOR SEARCHING..
;Value: bstsearch
(bstsearch (bstc '(5 23 34)) 3)
;Value: #f