简体   繁体   English

Scheme中的二进制搜索树,如果BST中存在值,则尝试使用Dr. Racket简单地返回true或false。 错误

[英]Binary Search Tree in Scheme, trying to use Dr. Racket to simply return true or false if value is present in BST. Error

I'm using Dr. Racket, language Pretty Big, and I'm trying to make a simple binary search tree "in?" 我正在使用Dr。Racket,语言Pretty Big,我正在尝试制作一个简单的二叉搜索树“in?” method, that will return if a value is in the binary search tree or not. 方法,如果值在二进制搜索树中,则返回。 It needs to be general, accepting any kind of search tree (whether it contain strings, ints, etc.), but I'm running into this error message that is driving me nuts. 它需要是通用的,接受任何类型的搜索树(无论它是否包含字符串,整数等),但我遇到了这个错误消息,这让我疯了。 Any help is appreciated, here is the code: 任何帮助表示赞赏,这里是代码:

EDITED:: It works now, but not with anything but numbers (or at least doesn't work with strings).. New issue: EDITED ::它现在有效,但除了数字之外没有任何东西(或者至少不能用于字符串)。新问题:

(define (bstsearch tree value)
  (cond 
  ((null? tree) #f)
  ((< value (car tree))
      (bstsearch  (cadr tree) value))
  ((> value (car tree))
      (bstsearch (caddr tree) value))
  ((= value (car tree))
      #t)
  ))

The error I'm receiving says: 我收到的错误说:

<: expects type <real number> as 1st argument, given: "horse"; other arguments were: "horse"

When using: 使用时:

 (bstsearch '("horse" ("cow" () ("dog" () ())) ("zebra" ("yak" ()()) ())) "horse")

as input. 作为输入。

Regarding your new issue, < and > only work for numbers. 关于您的新问题,<和>仅适用于数字。 An easy solution would be to pass the compare functions as arguments to your bstsearch procedure. 一个简单的解决方案是将比较函数作为参数传递给bstsearch过程。

Also, as mentioned before, please indent the code correctly. 另外,如前所述,请正确缩进代码。

You shouldn't wrap the arguments in another set of parens, so use 你不应该将参数包装在另一组parens中,所以请使用

(bstsearch  (cadr tree) value)

instead of 代替

(bstsearch  ((cadr tree) value))

One problem is you have your < and > reversed. 一个问题是你的<和>逆转了。 Assuming you want your left sub tree to be the smaller, then (< value (car tree)) should call again with the (cadr tree). 假设您希望左子树更小,那么(<值(车辆树))应该再次使用(cadr树)调用。

Also you should use #t instead of (#t). 你也应该用#t代替(#t)。

Your newly faced problem is because of your comparer function "=". 您遇到的新问题是因为您的比较器功能“=”。 If you change that with "equal?" 如果你用“相等”改变它? function it should be generic and work in any kind of data. 功能它应该是通用的,适用于任何类型的数据。 Comparers also should change if you want to make it generic. 如果你想让它变得通用,比较者也应该改变。 You must take it from user as input so generic version of it should be: 您必须从用户那里获取它作为输入,因此它的通用版本应该是:

(define (bstsearch tree value comparer)

(cond 

((null? tree) #f)

  ((equal? value (car tree)) #t)

  ((comparer value (car tree))
      (bstsearch  (cadr tree) value))

  ((not (comparer value (car tree)))
      (bstsearch (caddr tree) value))

  ))
  • Comparer function should be in format of (XX -> boolean), "<", ">", "string<?" Comparer函数的格式应为(XX - > boolean),“<”,“>”,“string <?” are built in examples but you can write your own comparer for your own data structure too 是在示例中构建的,但您也可以为自己的数据结构编写自己的比较器

  • Note that the equal condition is on the 2. line. 请注意,相等条件位于2.行。 I hope this helps :) 我希望这有帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM