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 8674307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:40:41+00:00 2026-06-12T19:40:41+00:00

Imagine an arithmetic expression such as (+ 1 (* 2 (- 3 5))) being

  • 0

Imagine an arithmetic expression such as (+ 1 (* 2 (- 3 5))) being thought of as a tree-like structure with numbers at the leaves and operator symbols at the interior nodes like below:

     +
   /   \
  1     *
       /  \
      2    -
          /  \
         3    5

I have these functions already defined to access certain parts of the tree:

;; returns tree node
(define (operator lst)
  (cadr lst))

;; returns left tree
(define (left-op lst)
  (car lst))

;; returns right tree
(define (right-op lst)
  (cddr lst))

I am trying to write 3 functions preorder, inorder, and postorder that return a list of the tree traversed in the order they were encountered

I know how the tree traversal works from previous java programming but am having trouble coding this

ex for above:
(preorder '(+ 1 (* 2 (- 3 5)))) => (+ 1 * 2 - 3 5)

  • 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-12T19:40:43+00:00Added an answer on June 12, 2026 at 7:40 pm

    Your implementation of trees is not quite right, you need to represent a leaf (a number in the examples) as another tree with null left and right subtrees. Also, it’s useful having a make-tree “constructor”. Let’s go step-by-step – first, a correct abstraction for representing trees:

    (define (make-tree value left right)
      (list left value right))
    
    (define (operator tree)
      (cadr tree))
    
    (define (left-op tree)
      (car tree))
    
    (define (right-op tree)
      (caddr tree))
    

    Now for the traversals. I’ll help you with the first one, preorder:

    (define (preorder tree)
      (if (null? tree)
          '()
          (append (list (operator tree))
                  (preorder (left-op tree))
                  (preorder (right-op tree)))))
    

    The tree in the question would look like this:

    (define tree
      (make-tree '+
                 (make-tree 1 '() '())
                 (make-tree '*
                            (make-tree 2 '() '())
                            (make-tree '-
                                       (make-tree 3 '() '())
                                       (make-tree 5 '() '())))))
    

    Use it like this:

    (preorder tree)
    > '(+ 1 * 2 - 3 5)
    

    The other two traversals are very similar, just rearrange the three arguments for append in the correct order for each case – I’ll let that as an exercise for the reader.

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

Sidebar

Related Questions

Imagine an arithmetic expression such as (+ 1 (* 2 (- 3 5))) being
Imagine you have a function tree which has a number of nodes which can
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine a Model/Collection like: var AModel = Backbone.Model.extend({ defaults: { a: 'a string', b:
Imagine a segmented creature such as a centipede. With control of the head segment,
Is there such a thing that finds numbers using regex and can perform simple
Imagine this directory structure: app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py I'm coding
Imagine this db structure: `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `correct` tinyint(1) NOT NULL
Imagine a cube-shaped piece of Swiss cheese. We model the cheese through a 20x20x20
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and

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.