Write in the Scheme programming language A binary search tree is a binary tree f
ID: 3871451 • Letter: W
Question
Write in the Scheme programming language
A binary search tree is a binary tree for which the value in each node is greater than or equal to all values in its left subtree, and less than all values in its right subtree. In Scheme, write a recursive function (insert-bst V T), which returns the binary search tree that results by inserting value V into binary search tree T. For example, by inserting the value 20 into the binary search tree T shown above, the result should be:
A binary search tree is a binary tree for which the value in each node is greater than or equal to all values in its left subtree, and less than all values in its right subtree. The binary tree given as example in problem 2 also qualifies as a binary search tree. Using the same list representation, write a recursive function (insert-bst V T), which returns the binary search tree that results by inserting value V into binary search tree T. For example, by inserting the value 20 into the binary search tree T shown above, the result should be: 13 25 20Explanation / Answer
Here is the function to insert a value V in Binary Tree T.
In this Function, I am using a constructor ( make-node ) which is in my structure.
Here is my Structure : -
(define-structure
(node
(constructor make-node (elt #!optional left right)))
elt (left '()) (right '()))
But you Can change it according to ur Structure
(define (insert-bst V T)
(if (null? T)
(make-node V)
(let ((elt (node-elt T)))
(cond ((< x elt)
(make-node elt
(insert-bst V (node-left T))
(node-right T)))
((> V elt)
(make-node elt
(node-left T)
(insert-bst V (node-right T))))
(else T)))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.