Question: In Racket, create a recursive tree - insert which takes a tree and a number and returns the tree with the number instead. Within the
In Racket, create a recursive treeinsert which takes a tree and a number and returns the tree with the number instead. Within the code, you will see the expected return but that is not what I am getting. What needs to be adjusted so I can get the subtrees the extra parenthesis needed?
How can I fix my code to get the return expected? Below is my code so far but I am unsure how to get the correct nesting.
Current code:
#lang racket
;; Define the function to incrementally insert and build the tree with quoted output
define treeinsertcustom value tree showleft? showright?
cond
;; If the tree is empty, return a new list with the value
empty treelist value
;; If the value is less than the current node, insert into the left subtree
value first tree
letnewleft treeinsertcustom value if length treesecond tree #t showright?
rightsubtree if and showright? length treethird tree
list first tree
newleft
rightsubtree ;; Include left and right subtrees directly
;; If the value is greater than the current node, insert into the right subtree
value first tree
letleftsubtree if and showleft? length treesecond tree
newright treeinsertcustom value if length treethird tree showleft? #t
list first tree
leftsubtree
newright ;; Include left and right subtrees directly
;; If the value is equal to the current node's value, return the tree unchanged
else tree
;; Function to build a single cumulative tree from a list of elements
define buildtreewithquote elements
define helper lst tree
if empty lst
tree
helper rest lsttreeinsertcustom first lst tree #t #t
quote helper elements ;; Start with an empty tree and add each element, using quasiquote to include quote
define A buildtreewithquote ;; Insert
define B buildtreewithquote ;; Insert
define C buildtreewithquote ;; Insert
define D buildtreewithquote ;; Insert
define E buildtreewithquote ;; Insert
;; Display results with the quote included automatically
display A ;; Expected output:
newline
display B ;; Expected output:
newline
display C ;; Expected output:
newline
display D ;; Expected output:
newline
display E ;; Expected output:
newline
Current output:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
