Question

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

Homework Answers

Answer #1

solution:

given data:

scheme procedure that returns the total number :

Scheme Code:

(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: ((4 5 6)(1 2)()(3)))

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

;Value: 6

please give me thumb up

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
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.
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...
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 ()
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.
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a list of numbers. For example: (longest ''(2 4 3 1 2 1 3 6 1 3 2 1 2 33 4 2 4 10 11 7)) --> (1 1 1 1 2 4 4 10 11). Do not use side-effecting functions (i.e., functions with exclamation point in their names such as set!), sequencing, iteration (e.g., do, for-each) or vectors. Do not use input/output mechanisms...
Exercise 3: Multi-way Trees A way to reduce the height of tree and ensure balance is...
Exercise 3: Multi-way Trees A way to reduce the height of tree and ensure balance is to allow multiple children of nodes. In your class you learned 2-3 trees which allows up to 2 keys in a node, and the number of children is equal to the number of keys + 1. B-trees extend this concept to any arbitrary number of keys (usually number of keys is even and number of children (equal to number of keys+1) is odd). Assume...
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...
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...
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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT