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 tree-insert 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 sub-trees 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 (tree-insert-custom value tree show-left? show-right?)
(cond
;; If the tree is empty, return a new list with the value
[(empty? tree)(list value)]
;; If the value is less than the current node, insert into the left subtree
[(< value (first tree))
(let*((new-left (tree-insert-custom value (if (>=(length tree)2)(second tree)'()) #t show-right?))
(right-subtree (if (and show-right? (>=(length tree)3))(third tree)'())))
(list (first tree)
new-left
right-subtree))] ;; Include left and right subtrees directly
;; If the value is greater than the current node, insert into the right subtree
[(> value (first tree))
(let*((left-subtree (if (and show-left? (>=(length tree)2))(second tree)'()))
(new-right (tree-insert-custom value (if (>=(length tree)3)(third tree)'()) show-left? #t)))
(list (first tree)
left-subtree
new-right))] ;; 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 (build-tree-with-quote elements)
(define (helper lst tree)
(if (empty? lst)
tree
(helper (rest lst)(tree-insert-custom (first lst) tree #t #t))))
`(quote ,(helper elements '()))) ;; Start with an empty tree and add each element, using quasiquote to include quote
(define A (build-tree-with-quote '(8))) ;; Insert 8->'(8)
(define B (build-tree-with-quote '(812))) ;; Insert 12->'(8((12)))
(define C (build-tree-with-quote '(83))) ;; Insert 3->'(8((3))())
(define D (build-tree-with-quote '(8312))) ;; Insert 12->'(8((3)(12)))
(define E (build-tree-with-quote '(83124))) ;; Insert 4->'(8((3((4)))(12)))
;; Display results with the quote included automatically
(display A) ;; Expected output: '(8)
(newline)
(display B) ;; Expected output: '(8((12)))
(newline)
(display C) ;; Expected output: '(8((3))())
(newline)
(display D) ;; Expected output: '(8((3)(12)))
(newline)
(display E) ;; Expected output: '(8((3((4)))(12)))
(newline)
=====================================
Current output:
'(8)
'(8()(12))
'(8(3)())
'(8(3)(12))
'(8(3()(4))(12))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!