Question

9-Define a scheme procedure that returns the total number of leaves of a tree For example,...

9-Define a scheme procedure that returns the total number of leaves of a tree
For example, (count-leaves '((4 5 6) (1 2) () (3) )) returns 6


Then, trace the procedure with the given example.

Homework Answers

Answer #1

Here is the solution. Please do upvote thank you.

(define (count-leaves t)
(cond ((null? t) 0)
((not (pair? t)) 1)
(else (+ (count-leaves (car t))
(count-leaves (cdr t))))))

; The general logic of this function is:
; If the tree is null, return 0
; elif: return 1 if at a leaf. We know
; That we're at a leaf if the position is not null and not a pair
; else: we are at a pair and must branch our function

; Testing it below:

1 ]=> (load "hierarchical_structures_1.scm")

;Loading "hierarchical_structures_1.scm"... done
;Value: count-leaves

1 ]=> (define tree-1 (cons (list 1 2) (list 3 4)))

;Value: tree-1

1 ]=> tree-1

;Value 11: ((1 2) 3 4)

1 ]=> (count-leaves tree-1)

;Value: 4

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
11. Define a scheme procedure that returns the total number of leaves of a tree For...
11. Define a scheme procedure that returns the total number of leaves of a tree For example, (count-leaves '((4 5 6) (1 2) () (3) )) returns 6 Then, trace the procedure with the given example. Scheme language
Define a function in Scheme which takes a list of numbers and a scalar and returns...
Define a function in Scheme which takes a list of numbers and a scalar and returns the rest of the list after the first occurrence of the scalar that was passed. An empty list should be returned if the value doesn't exist. For example, (list-past-scalar '(5 2 8 3 1 9 2 3) 3) #returns (1 9 2 3) (list-past-scalar '(37 18 38 65 90) 100) #returns ()
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as...
LANGUAGE: SCHEME R5RS Create a procedure called preceeding that will take a list of numbers as argument and determine the elements and indices of those elements that preceed a negative number in the given list. The returned information should be in the form of a pair of lists: ((values) . (indices)). E.g. (preceeding '(1 -2 3 4 -5 6 -7 -8)) → ((1 4 6 -7).(0 3 5 6)) ;note: drracket will display this as ((1 4 6 -7) 0...
python If a number num1 divides another number num2 evenly then num1 is a divisor of...
python If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13. Write a function named count_divisors(m,n) that works as follows. Input: the function takes two integer arguments m and n Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs...
There will be a total of 16 minerals (7 major & 9 trace) Minerals Name –...
There will be a total of 16 minerals (7 major & 9 trace) Minerals Name – both name and number if given Function Major or trace if minerals 4 food sources Name of deficiency or symptoms of deficiency Toxicity Yes or no 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
The following table shows the total number of imperfections (nonconformities) in the given number of finished...
The following table shows the total number of imperfections (nonconformities) in the given number of finished rolls of paper for 5 hours. Set up and draw a control chart for imperfections per roll of paper. Does the process seem to be in statistical control? Number of rolls Produced Hour total number of Imperfections Number of rolls Produced 1 12 4 2 8 4 3 9 3 4 10 5 5 6 4
Instructions: Complete the two exercises shown below. 1) Below are 12 audit procedures. Classify each procedure...
Instructions: Complete the two exercises shown below. 1) Below are 12 audit procedures. Classify each procedure according to the following types of audit evidence: (1) physical examination, (2) confirmation, (3) documentation, (4) observation, (5) inquiry of the client, (6) reperformance, and (7) analytical procedure. Type of Evidence Audit Procedures 1. Watch client employees count inventory to determine whether company procedures are being followed. 2. Count inventory items and record the amount in the audit files. 3. Trace postings from the...
Problem 3 Given a BST with N nodes, how many tree shapes are there with height...
Problem 3 Given a BST with N nodes, how many tree shapes are there with height N-1? Explain your reasoning. Problem 4 Given a BST with N nodes, how many tree shapes are there with height N-2? Explain your reasoning . Problem 5 Consider an empty 2-3 tree. Draw the tree after each of the following operations is executed: insert 0, insert 9, insert 2, insert 6, insert 7, insert 3, insert 8, delete 2, delete 6
Solution Procedure: In solving these problems and subsequent ones, use the following procedure: (1) Define the...
Solution Procedure: In solving these problems and subsequent ones, use the following procedure: (1) Define the events of interest. (2) Write down the information given. (3) Write down the question. (4) Use the appropriate rule to answer the question. An office worker receives an average of 22.5 email messages per day. If his working day lasts seven and a half hours, what is the probability that: (a) He receives no emails in an hour? (b) He receives one email in...
Bag Blue Orange Green Yellow Red Brown Total Number of Candies 1 7 23 9 11...
Bag Blue Orange Green Yellow Red Brown Total Number of Candies 1 7 23 9 11 4 6 60 2 16 16 4 7 9 4 56 3 14 14 3 10 5 11 57 4 13 14 7 8 8 6 56 5 18 12 7 10 5 7 59 6 12 9 11 9 8 8 57 7 15 16 6 6 6 8 57 8 15 17 8 4 6 7 57 9 12 14 10 5...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT