Question

In: Computer Science

Implement a function named printRange that, given the pointer to the root of a BST, a...

Implement a function named printRange that, given the pointer to the root of a BST, a low key value, and a high key value, prints in sorted order all records whose key values fall between the two given keys (inclusive). Function printRange should visit as few nodes in the BST as possible. Here is the start code (in Java 8) for this problem. Input Format Three lines. The first line includes the number of keys to be put in the BST, and the second line includes the actual numbers. The third line includes two numbers, for low key and high key, respectively. Constraints The keys are integers. Output Format The key values that fall between the two given low and high keys, in sorted order. Sample Input 0 10 39 78 10 99 79 48 93 58 73 83 5 98 Sample Output 0 10 39 48 58 73 78 79 83 93

start code

import java.util.Scanner;
class Node {
private Node left;
private Node right;
private int data;
Node(int data) {
this.data = data;
left = null;
right = null;
}
void setData(int d) {
data = d;
}
int getData() {
return data;
}
void setLeft(Node i) {
left = i;
}
void setRight(Node i) {
right = i;
}
Node getLeft() {
return left;
}
Node getRight() {
return right;
}
}
class PrintRange {
public static Node insert(Node root, int data) {
if (root == null) {
return new Node(data);
} else {
Node cur;
if (data < root.getData()) {
cur = insert(root.getLeft(), data);
root.setLeft(cur);
} else {
cur = insert(root.getRight(), data);
root.setRight(cur);
}
return root;
}
}
//implement printRange() function
public static void printRange(Node root, int low, int high) {
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
Node root = null;
while (t-- > 0) {
int data = scan.nextInt();
root = insert(root, data);
}
int low = scan.nextInt();
int high = scan.nextInt();
scan.close();
printRange(root, low, high);
}
}

Solutions

Expert Solution

THE CODE FOR THE ABOVE QUESTION IS :


import java.util.Scanner;

class Node {

    private Node left;
    private Node right;
    private int data;

    Node(int data) {
        this.data = data;
        left = null;
        right = null;
    }

    void setData(int d) {
        data = d;
    }

    int getData() {
        return data;
    }

    void setLeft(Node i) {
        left = i;
    }

    void setRight(Node i) {
        right = i;
    }

    Node getLeft() {
        return left;
    }

    Node getRight() {
        return right;
    }
}

class PrintRange {

    public static Node insert(Node root, int data) {
        if (root == null) {
            return new Node(data);
        } else {
            Node cur;
            if (data < root.getData()) {
                cur = insert(root.getLeft(), data);
                root.setLeft(cur);
            } else {
                cur = insert(root.getRight(), data);
                root.setRight(cur);
            }
            return root;
        }
    }

    //implement printRange() function
    public static void printRange(Node root, int low, int high) {

        // base case
        if (root == null) {
            return;
        }

        // Since the output should be in sorted order while visiting
        // as fewer nodes as possible to achieve the task.
        // We recursively first visit the left subtree and print all values
        // which are greater than low in the left subtree. Therefore if low
        // is greater than the value at the root node then there will be no values
        // in the left subtree greater than low.
        if (low < root.getData()) {
            printRange(root.getLeft(), low, high);
        }

        // Now if the value present at the current node lies in the range of
        // low and high (inclusive) we print it.
        // This statement is executed only when we have traversed upto the point
        // where the value at node is less than low for the left subtree &
        // similarly for the right subtree upto the node where the value is greater
        // than high.
        if (low <= root.getData() && high >= root.getData()) {
            System.out.print(root.getData() + " ");
        }

        // If the value present in the current node is smaller than high then
        // only we recurse the right subtree.
        if (high > root.getData()) {
            printRange(root.getRight(), low, high);
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int t = scan.nextInt();
        Node root = null;
        while (t-- > 0) {
            int data = scan.nextInt();
            root = insert(root, data);
        }
        int low = scan.nextInt();
        int high = scan.nextInt();
        scan.close();
        printRange(root, low, high);
    }
}

SAMPLE OUTPUT #1 :

SAMPLE OUTPUT #2 :


Related Solutions

write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of...
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be...
1. Delete function for a BST class given a value. You need to make sure that...
1. Delete function for a BST class given a value. You need to make sure that the tree remains a binary search tree after deletion. Your function should work for nodes that can handle all the following cases: a. Node to be deleted has no children b. Node to be deleted has only one child c. Node to be deleted has two children 2. A BST class function that returns the maximum value in a tree. 3. A BST class...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
In this problem, we will implement an nth root finder. Recall that the nth root of...
In this problem, we will implement an nth root finder. Recall that the nth root of x, written nāˆš x, is the number when raised to the power n gives x. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal...
In this assignment, we will implement a square root approximater and then an nth root approximater....
In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We learned in class that we can use the concepts of binary search to approx imate the square root of a number, and we can continue this logic to approximate the nth square root. Please look at Lecture Notes 03, section 4 for a little more...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
given and sketch in steps the root locuse of transfer function by matlab and hand calculation...
given and sketch in steps the root locuse of transfer function by matlab and hand calculation G(s)=(s+3)/s(s+2) given solution please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT