Skip to content

Instantly share code, notes, and snippets.

@tushar-sharma
Created November 26, 2015 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tushar-sharma/c476003598c03373e52c to your computer and use it in GitHub Desktop.
Save tushar-sharma/c476003598c03373e52c to your computer and use it in GitHub Desktop.
Binary Search Tree in Scheme
;;Get the root element
(define (key node )(car node))
;;Get the Left sub tree
(define (left node) (cadr node))
;;Get the right sub tree
(define (right node) (caddr node))
;;insert element 'el' in the tree
(define (insert el tree)
(cond ((null? tree)
(list el '() '()))
((= el (key tree))
tree)
((< el (key tree))
(list (key tree)
(insert el (left tree))
(right tree)))
((> el (key tree))
(list (key tree)
(left tree)
(insert el (right tree))))))
;;loop through all elements & insert in tree
(define (list2tree ls)
(list2tree-help ls '()))
(define (list2tree-help ls tree)
(cond ((null? ls) tree)
(else (list2tree-help (cdr ls )
(insert (car ls) tree)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment