Scheme A binary search tree is a binary tree for which the value in each node is
ID: 3869984 • Letter: S
Question
Scheme
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
struct node* makeNode(int value){
struct node* n = (struct node*) malloc(sizeof(struct node));
n->value = value;
n->left = n->right = NULL;
return n;
}
struct node* insert-bst(struct node* root, int value){
if(root == NULL) return makeNode(value);
if(value < root->value) root->left = insert-bst(root->left, value);
else if(value > root->value) root->right = insert-bst(root->right, value);
return root;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.