使用Scheme在B+树中查找元素

问题描述 投票:0回答:1

我需要遍历B+树。如果我点击一个列表,我需要处理它并递归地继续直到我得到一个元素。然后我将其与给定的数字进行比较。如果该数字大于该列表中的最大数字,我可以忽略该分支(列表)。较少时也一样。我只需要遍历树并找到元素。无论给定什么树,它都应该有效

这就是我所做的

(define ones (list '() 2 '() 4 '() 6 '() 7 '() 8 '()))
(define sixties (list '() 52 '() 54 '() 66 '() 67 '() 68 '()))
(define tens (list ones 10 '() 30 '() 50 sixties 70 '() 90 '()))
(define ninehundreds (list '() 930 '() 940 '() 960 '() 970 '() 988 '()))
(define tree (list tens 100 '() 300 '() 500 '() 700 '() 900 ninehundreds ))

(define (walk tree num)
   (if list? (car tree)
       (walk-list (car tree)))

  (if (not list? (car tree)))
       walk-list( cdr tree)
)

(define (walk-list lst)
  (if (list? (car lst)) (walk-list (car lst)))
  (if (not (list? (car lst))) (car lst))
)
(walk tree 0)

我对两个定义函数中的两个

if
有疑问。我也曾一度不断回来
#t

编辑:我相信我修正了我对语法错误的理解。但现在我在

if
 中的 
walk-list

声明中收到此消息
mcar: contract violation
  expected: mpair?
  given: ()
(define (walk tree num)
   (if (list? (car tree))
       (walk-list (car tree))
       (car tree))
)

(define (walk-list lst)
  (if  (list? (car lst))
    (walk-list (car lst))
    (car lst))
)
list scheme racket b-tree b-plus-tree
1个回答
0
投票

在 REPL 上尝试一下:

> (list? '())
#t

> (pair? '())
#f

您无法通过

car
拨打
'()
。这将导致您显示的错误。

© www.soinside.com 2019 - 2024. All rights reserved.