任何人都可以帮我解决左右旋转二叉搜索树的基本情况吗?我尝试将正确的旋转功能写为:
(define (right-rotate T)
(make-tree (car (left T))
(left (left T))
(make-tree(value T) (right (left T)) (right T))))
但这让我在某个地方调用了一个空列表。这个代码对于正确的旋转看起来是否正确?另外,我的基本情况可以在这里吗?
答案 0 :(得分:1)
您确实需要提供更多信息,例如您对“树”的表示方式以及树如何缺少其定义和处理的“左”或“右”子项。
(define (make-tree value left right)
`(TREE ,value ,left ,right))
(define value cadr)
(define right caddr)
(define left cadddr)
;; How is an 'empty' tree denoted?
(define empty 'EMPTY)
(define (is-empty? tree)
(eq? tree empty))
(define (make-leaf value)
(make-tree value empty empty))
;; Now you have the basis for a solution; here is a start.
(define (right-rotate tree)
(if (is-empty? tree)
empty
(let ((l (left tree)))
(if (is-empty? l)
<something>
(make-tree (value l)
(left l)
(make-tree (value tree) (right l) (right tree)))))))