rsr5方案表达的思想
我最初的想法涉及一棵无限树,它的节点是函数,它接受一个值和一棵树,并将该值插入到该树中找到函数的同一位置。然后,您可以在列表上执行类似 fold2 的递归,并对函数树进行级别顺序遍历,将连续函数应用于连续列表元素以累积二叉树。(虽然仅适用于惰性评估)
虽然看起来有点笨拙,所以我将想法修改为可以提供给 ('l 'r 'l) 之类的函数的数据树,或者可以告诉在哪里插入和索引的东西。
(define (insert-new ls-and-rs tree)
(cond ((or (empty-tree?) (null? ls-and-rs))
(if (and (empty-tree?) (null? ls-and-rs))
(make-tree x emptyTree emptyTree)
(error "insert-new can only insert at emptyree")))
((sybol=? (car ls-and-rs) 'l)
(make-tree (node tree)
(insert-new (cdr ls-and-rs)
(left-tree tree))
(right-tree tree)))
((sybol=? (car ls-and-rs) 'r)
(make-tree (node tree)
(left-tree tree)
(insert-new (cdr ls-and-rs)
(right-tree tree))))
(else (error "insert-new expected 'l or 'r, recieved "
(car ls-and-rs))))))
然后我看到你可以从索引本身构建它。如果 1 是树的头部,则为索引。否则,如果它很奇怪,它是一个节点的右分支。如果它甚至是一个左分支。它的父母的索引总是孩子的地板除以二。根据这些知识,您可以仅使用索引构建插入器或访问器。
(define (branch-order i)
(let loop ((i i) (accum '()))
(cond ((i = 1) accum)
((odd? i) (loop (quotient i 2) (cons 'r accum)))
(else (loop (quotient i 2) (cons 'l accum))))))
从那里它是一个简单的递归
(define (list->tree list)
(let loop ((list list) (i 1) (tree emptyTree))
(cond ((null? list) tree)
(else (loop (cdr list)
(+ i 1)
(insert-new (branch-order i) tree))))))
当然,最简单的方法是您是否可以接受最小深度的二叉树。深度树除了节点和分支之外,还列出了它的最小深度。然后插入将插入到左子树中,除非右子树的最小深度小于左子树的最小深度。
(define (list->d-tree list)
(fold-left (flip balanced-insert) emptyTree list))
(define (balanced-insert x d-tree)
(cond ((= 0 (d-d-tree d-tree))
(mk-d-tree x emptyTree emptyTree)
((= 1 (- (d-d-tree d-tree) (d-d-tree (l-d-tree d-tree))))
(mk-d-tree (n-d-tree d-tree)
(balanced-insert x (l-d-tree d-tree))
(r-d-tree d-dtree)))
(else
(mk-d-tree (n-d-tree d-tree)
(l-d-tree d-tree)
(balanced-insert x (r-d-tree d-dtree))))))
(define (mk-d-tree n l r)
(let ((d-l (d-d-tree l))
(d-r (d-d-tree r)))
(list n l r (+ 1 (min d-l d-r)))))
(define (n-d-tree d-tree)
(car d-tree))
(define (l-d-tree d-tree)
(cadr d-tree))
(define (r-d-tree d-tree)
(caddr d-tree))
(define (d-d-tree d-tree)
(if emptyTree? d-tree)
0
(cadddr d-tree)))
(define (emptyTree? tree)
(null? tree))
(define emptyTree '())
(define (flip f)
(lambda (a b)
(f b a)))
TLdr; 使用最小深度树并插入到最左边的最小深度。