我在树形图中插入一个键key
和一个值,返回一个新树,其中一个节点包含适当位置的键和值。val
t
我有这个定义:
(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))
我正在尝试这种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)))))))
我用这个来定义
(define right (insert (insert empty 3 10) 5 20))
我在 DrRacket 中遇到了这个错误
application: not a procedure;
expected a procedure that can be applied to arguments
given: (list 0 1 2)
arguments...: [none]