Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:13:26+00:00 2026-06-12T12:13:26+00:00

I have already defined helper functions to be: ;; returns value of node (define

  • 0

I have already defined helper functions to be:

;; returns value of node
(define (value node)
  (if (null? node) '()
      (car node)))

;; returns left subtree of node
(define (left node)
  (if (null? node) '()
      (cadr node)))

;; returns right subtree of node
(define (right node)
  (if (null? node) '()
      (caddr node)))

and I am trying to write a function leaves that returns a list with the leaves of the tree in order of left to right.

(define (leaves tree)
    (if (and (?null (left tree)) (?null (right tree)))
        ???
        (leaves (left tree)) (leaves (right tree))))

but that is as far as I can get

ex: (leaves ‘(1 (2 () ()) (3 () ()))) should evaluate to ‘(2 3)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:13:27+00:00Added an answer on June 12, 2026 at 12:13 pm

    In what you have so far, the ??? is going to need to evaluate to the value of the leaf, ie. (value node) because it is the base case of your iteration. Also, you’re going to need to combine the values you get back from the base case in your iteration case. list is usually a good first candidate to try when you need to combine multiple results cons is usually my second try. Taking those suggestions, your leaves function looks like this:

    (define (leaves tree)
        (if (and (null? (left tree)) (null? (right tree)))
            (value tree)
            (list (leaves (left tree)) (leaves (right tree)))))
    

    which, when run on your sample of (leaves '(1 (2 () ()) (3 () ()))) does indeed evaluate to (2 3).

    HOWEVER; YOU’RE NOT DONE! We’re only testing with 1 level of recursion. What if we make a bigger tree? Something like: (leaves '(1 (2 (4 () ()) (5 () ())) (3 (6 () ()) (7 () ())))) Running this gives ((4 5) (6 7)). Those are the right values in the right order, but we have too much structure in there, too many parenthesis. This is a typical problem you will encounter throughout your scheme career, so let me explain why it happens, and how you can go about attacking the problem.

    If look at the two branches of our if form, you’ll notice that (value tree) returns an atom, or a number in this case. The else branch takes two of ??? and turns it into a list of ???. We’re going to be executing the else branch multiple times – any time we’re not in the base case. This means we’re going to continue to wrap, and wrap, and wrap into a deeper and deeper list structure. So here’s what we do about it.

    Lets return a list in our base case, and keep our list flat in the recursion case. To return a list in our base case it is as simple as returning (list (value tree)) instead of just (value tree). In the recursion case, we need a function that takes 2 lists and combines them without making a deeper list. Such a function does exist – append. So let’s look at what our leaves function looks like now:

    (define (leaves tree)
            (if (and (null? (left tree)) (null? (right tree)))
                (list (value tree))
                (append (leaves (left tree)) (leaves (right tree)))))
    

    Intermezzo – Test cases

    Racket has test suite library that has a very low barrier to entry called rackunit. Let’s throw together a few quick test cases at the bottom of the file.

    (require rackunit)
    ;;empty tree
    (check-equal? (leaves '()) '())
    
    ;;simple balanced tree
    (check-equal?
     (leaves '(1 (2 () ()) (3 () ())))
     '(2 3))
    
    ;;larger balanced tree
    (check-equal?
     (leaves '(1 (2 (4 () ()) (5 () ())) (3 (6 () ()) (7 () ()))))
     '(4 5 6 7))
    
    ;;unbalanced tree
    (check-equal?
     (leaves '(1 (2 (4 () ()) ()) (3 () ())))
     '(4 3))
    

    Recently, racket has added support for submodules and specific support for test submodules if you are curious and want to look into them.


    Back to our leaves problem. Running our tests, we notice our function doesn’t behave well on unbalanced trees. We get extra ()s when we have a node that only has 1 leaf. That is because we are traversing both the left and the right subtrees whenever we’re at a node that isn’t a leaf. What we really need are two more cases in our if. We could nest the ifs, but scheme’s cond form makes better sense.

    Now, the template we’re aiming to fill out is:

    (define (leaves tree)
      (cond [(leaf? tree) (...)]
        [(and (has-left? tree) (has-right? tree)) 
         (...)]
        [(has-left? tree) (...)]
        [(has-right? tree) (...)]
        [else (error "should never get here")]))
    

    I’ll stop there in-case this is homework, and to give you the satisfaction of understanding and solving this the rest of the way. I hope my explanations have given you more direction that just “here’s the code” answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a object in javascript and some already defined functions. but how can
I have a Java array defined already e.g. float[] values = new float[3]; I
I'm taking some information from some variables I have already defined outside this function
When I compile my CUDA code with NVCC and I have already defined a
I have defined following templates, used for combining already defined predicates: namespace SomeNamespace {
I have already pushed all the changes to the server. Here is the order
I have already download and install this package to support HTML5 input type http://support.microsoft.com/kb/2468871
I have already googled and searched stackoverflow but I can find nothing related to
I have already tried multiple ways. defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ORGANIZATIONNAME=YourNameHere;}' Also i've tried
I have already created my application on web socket with ASP.net 4.0. I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.