简体   繁体   English

使用方案在二进制搜索树中插入节点密钥和值

[英]Insert node key and value in binary search tree using scheme

I am inserting a key key and a value val into the tree-map t returning a new tree with a node containing the key and the value in the appropriate location. 我插入钥匙key和值val到树映射t返回一个新的树有包含密钥并在适当的位置的值的节点。

I have this defined: 我有这个定义:

(define (tree-node key value left right)(list key value left right))

(define (get-key tn) (node_key tn))
(define (get-val tn) (node_val tn))
(define (get-left tn) (node_left tn))
(define (get-right tn) (node_right tn))

(define (node_key tn) (list-ref tn 0))
(define (node_val tn) (list-ref tn 1))
(define (node_left tn) (list-ref tn 2))
(define (node_right tn) (list-ref tn 3))

Im trying this insert method 我正在尝试这种insert方法

(define (insert t key val)

  (cond (empty? t))
  (tree-node (key val '() '()))

  ((equal key (get-key t))
   (tree-node (key val (get-left t) (get-right t)))

   ((<key (get-key t))
    (tree-node ((get-key t) (get-val t) (insert (get-left t key val)) (get-right t)))
    (tree-node ((get-key t) (get-val t) (get-left t) (insert (get-right t key val)))))))

I'm using this to define 我正在用它来定义

(define right (insert (insert empty 3 10) 5 20))

and I'm getting this error in DrRacket 我在DrRacket中遇到这个错误

application: not a procedure;
expected a procedure that can be applied to arguments
given: (list 0 1 2)
arguments...: [none]

What is happening is that a tree-node (which is just a list) is being returned in a position where a function is expected. 发生的事情是在期望功能的位置返回了树节点(仅是一个列表)。 Scheme cannot execute the list - it's not a function, so it's complaining. Scheme无法执行该列表-它不是一个函数,因此很抱怨。

I suspect this is because you have messed up your parentheses. 我怀疑这是因为您弄乱了括号。

(cond (empty? t))

Is a self-contained compound expression. 是一个自包含的复合表达式。 It is evaluated, returning the value of t. 求值,返回t的值。 The expressions after that are not evaluated as part of the cond. 之后的表达式不作为cond的一部分求值。

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

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