Skip to content

Instantly share code, notes, and snippets.

@MShrimp4
Last active September 28, 2019 10:14
Show Gist options
  • Save MShrimp4/206baa8be6016174307d5fcda3544d07 to your computer and use it in GitHub Desktop.
Save MShrimp4/206baa8be6016174307d5fcda3544d07 to your computer and use it in GitHub Desktop.
(define (make-itree)
(list (list) (list)))
(define (add-index! itree root-dest)
(define (add-index-rest! index)
(cond
((null? (cdr index)) (set-cdr! index (cdr root-dest)))
((< (caadr root-dest) (caadr index)) (set-cdr! index (cons (cadr root-dest) (cdr index))))
(else (add-index-rest! (cdr index)))))
(if (< (caadr root-dest) (caaar itree))
(set-car! itree (cons (cadr root-dest) (car itree)))
(add-index-rest! (car itree))))
(define (add-node! itree root branch)
(let ((dest (find-elm itree root)))
(if (null? dest)
#f
(begin
(set-cdr! dest (cons (list branch) (cdr dest)))
(add-index! itree dest)
#t))))
(define (find-elm itree root)
(let ((index (car itree))
(dest #f))
(for-each (lambda (elm) (if (= (car elm) root) (set! dest elm) #f))
index)
dest))
(define a '(() ((0))) )
(set-car! a (list (caadr a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment