计算树方案中的重复值

时间:2015-03-23 17:49:07

标签: scheme

我试图在树中计算重复数。我附上一张图片以便更好地说明。我走错了路,我无处可去。 这就是我做的事情

(define (arbre-insere valeur arbre) 
  (cond ((null? arbre) (list (cons valeur 1) '() '())) 
            ((< valeur(car arbre)) 
                (list (cons  (car arbre) count) 
                      (arbre-insere valeur (cadr arbre)) 
                                       (caddr arbre))) 
            (> valeur(car arbre) (list cons ((car arbre) count) (cadr arbre) 
                  (arbre-insere valeur (caddr arbre) )))
            (else

             )
            ))][1]

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个草图,其中...<...>中的内容应由您填写。

(define leaf '())

; leaf? : tree -> boolean
;   return #t if the tree is a leaf,
;          #f otherwise
(define (leaf? tree)
  (null? leaf?))

; value : tree -> element
;   return the root element of the tree
(define (value tree) 
  ...)

; count : tree -> integer
;   return the count of the root element of tree
(define (count tree)
  ...)

; left : tree -> tree
;   return the left subtree of tree
(define (left tree)
  ...)

; right : tree -> tree 
;   return the right subtree of tree
(define (right tree)
  ...)

; make-node : value integer tree tree
;   construct tree from a value and count, 
;   left is a tree whose elements are smaller than value
;   right is a tree whose elements are greater than value
(define (make-node value count left right)
  (list left (cons value count) right))

; tree-insert : value tree -> tree
(define (tree-insert v t)
  (cond
    [(leaf? t)        (make-tree v 1 leaf leaf)]
    [(= v (value t))  (make-tree v <old-count+1> (left t) (right t))]
    [(< v (value t))  (make-tree v (make-node (value t) (count t) 
                                              (insert-tree v (left t)) r))]
    [(> v (value t))  <???>]
    [else (error 'tree-insert "an unexpected error occurred")]))