Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***IN DR. RACKET PROGRAMMING LANGUAGE ONLY, NO OTHER PROGRAMMING LANGUAGES ARE ACCEPTABLE *** ***PLEASE DO NOT ANSWER IF IT IS NOT IN DR. RACKET, IT

***IN DR. RACKET PROGRAMMING LANGUAGE ONLY, NO OTHER PROGRAMMING LANGUAGES ARE ACCEPTABLE ***

***PLEASE DO NOT ANSWER IF IT IS NOT IN DR. RACKET, IT PREVENTS OTHERS FROM ANSWERING IN THE CORRECT LANGUAGE AND WILL BE DOWNVOTED***

Write a program in three parts:

Part one: A recursive function tree-insert that takes a tree and a number and returns the tree with the number inserted.

(tree-insert 8 '()) should return (8) (tree-insert 5 (8)) should return (8 (5)) (tree-insert 3 '(6 () (7))) should return '(6 (3) (7)) (tree-insert 4 '(6 (3) (7))) should return '(6 (3 () (4)) (7))

Part two: Write another recursive function list-to-tree that takes a list of numbers and a sorted tree and inserts all of the numbers from the list into the tree.

This function can be given an empty list as input for the sorted tree.

List-to-tree will repeatedly call tree-insert.

Test your function with: (list-to-tree '(22 25 7 16 8 34 67 7 32 17 8 4 5 3) ())

Returns: '(22 (7 (7 (4 (3) (5))) (16 (8 (8)) (17))) (25 () (34 (32) (67)))) Your function must match this output.

Numbers in the first subtree are less than or equal to the root node

Numbers in the second subtree are greater than the root node

'(6 (3 (2) (5)) (7 () (9))) is a sorted binary tree

Part three: Write a recursive function tree-to-list that given a sorted tree, constructs the list of numbers in order.

Test your function with: tree-to-list (list-to-tree '(22 25 7 16 8 34 67 7 32 17 8 4 5 3) ()) ) list-to-tree should work as defined in the previous 2 parts

This should return (3 4 5 7 7 8 8 16 17 22 25 32 34 67)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Accounting questions

Question

Identify conflict triggers in yourself and others

Answered: 1 week ago