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 5AW

Explanation of Solution

Algorithm for a method “contains” to search a value of “x”:

Step 1: Define the method name “contains ()” which contains the node and search element as the parameters.

Step 2: Check if the value of node is equal to null. If it is equal, then return “false”.

Step 3: Check if the value of node is equal to the search element. If this condition is true, then return “true”.

Step 4: Again check if the search element is present in the left side of the tree by calling the function “contains ()” recursively and return “true” if the search element is present.

Step 5: Again check if the search element is present in the right side of the tree by calling the function “contains ()” recursively and return “true” if the search element is present.

Step 6: Finally, if the search element is not present, then return “false”.

A method “contains” to search a value of “x”:

//Function definition for "contains"

boolean contains(Node binarytree, int x)

{

    //Check if the value of node is equal to null

    if (binarytree == null)

        //Return false

        return false;

//Check if the value of node is equal to the search element

    if (binarytree.value == x)

        //Return true

        return true;

//Check if the search element is present in the left sub-tree

    if (contains(binarytree...

Blurred answer
Students have asked these similar questions
Explian this C program  #include <stdio.h> unsigned int rotateRight(unsigned int num, unsigned int bits) { unsignedint bit_count =sizeof(unsignedint) *8; bits = bits % bit_count; // Handle cases where bits >= bit_count return (num >> bits) | (num << (bit_count - bits)); } int main() { unsignedint num, bits; printf("Enter a number: "); scanf("%u", &num); printf("Enter the number of bits to shift: "); scanf("%u", &bits); printf("After rotation: %u\n", rotateRight(num, bits)); return0; }
Explian thiS C program #include<stdio.h> int countSetBits(int n) {    int count = 0;    while (n) {        count += n & 1;        n >>= 1;    }    return count;} int main() {    int num;    printf("Enter a number: ");    scanf("%d", &num);    printf("Output: %d units\n", countSetBits(num));    return 0;}
Please provide the Mathematica code
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
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
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr