Explanation of Solution
Given
//Add a value to a binary search tree
//Return root of resulting search tree
Node add(Node tree, int value) //Line 1
{ //Line 2
//Check if tree is equal to null
if (tree == null) //Line 3
{ //Line 4
//Return new node
return new Node(x); //Line 5
//Check if the value is less than the value of the current node
if (value < tree.value) //Line 6
//Return and add the value to the left side
return add (tree.left, value); //Line 7
//Else
else //Line 8
//Return and add the value to the right side
return add (tree.right, value); Line 8
}//Line 9
The above program code snippet is used to add a value to the binary search tree and the program returns the modified root value.
Error in the program code:
Error 1:
In “Line 7”, the result of the recursive call needs to be grafted back into the tree. So this line should be modified as shown below.
//Call the function "add ()" and store the resultant into the node "tree.left"
tree.left = add(tree.left, value);
//Return the node.
return tree;
Error 2:
In “Line 8”, the result of the recursive call needs to be grafted back into the tree. So this line should be modified as shown below.
//Call the function "add ()" and store the resultant into the node "tree.right"
tree.right = add(tree.right, value);
//Return the node.
return tree;
Corrected code:
The modified code is highlighted below...
Want to see the full answer?
Check out a sample textbook solutionChapter 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Breadth-first search must be done without recursion. (with iterative)arrow_forwardIn C++arrow_forwardCreate a function that takes an array that represent a Binary Tree and a value and return true if the value is in the tree and, false otherwise. Examples valueInTree (arr1, 5) valueInTree (arr1, 9) valueInTree (arr2, 51) - true 1 false falsearrow_forward
- data structure-JAVAarrow_forwardPreorder: A B F G E H C D I J Inorder: F G B E H A D I C J After drawing the tree, how many is/are the terminal node(s)? (Numeric Answer ONLY)arrow_forwardpython: In a binary search tree, write another way of function that takes in a root, p, and checks whether the tree rooted in p is a binary search tree or not. What is the time complexity of your function? def is_bst(self, p): root=p def helper(root, l, r): if not root: return True if not (left<root.val and root.val<right): return False return helper(root.l, l, root.val) and helper(root.r, root.val, r) return helper(root, float('-inf'), float('inf'))arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education