Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 21, Problem 2FTE

Explanation of Solution

Given program code:

//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...

Blurred answer
Students have asked these similar questions
After our initial deployment for our ML home based security system, the first steps we took to contribute further to the project, we conducted load testing, tested and optimize for low latency, and automated user onboarding. What should be next?
Why investing in skills and technology is a critical factor in the financial management aspect of system projects.
why investing in skills and technology is a critical factor in the financial management aspect of systems projects.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Oracle 12c: SQL
Computer Science
ISBN:9781305251038
Author:Joan Casteel
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT