how to find predecessor in the tree

Discussion of Scheme and Racket
Post Reply
Lulu
Posts: 1
Joined: Tue Nov 18, 2014 8:10 am

how to find predecessor in the tree

Post by Lulu » Sat Nov 22, 2014 1:43 pm

Hello, Could you help me with a function, I need to find the predecessor in the tree, but my function always returns an empty list,

Code: Select all

(define (predecessor element tree)
   (cond
        ((null? tree) (node tree) ) ; If the tree is null result is  the needed element
        ((> element (node tree)) (mem element (right-branch tree))); if the element is greater than node element, we search it in the right-branch tree
        ((< element (node tr)) (mem element (left-branch tree))) ; if element is lesser than node element, we search it in the left-branch tree
    )
 )

Post Reply