Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wrapper class instead of static variables

This is my first question ever at StackOverFlow: I am studying to interviews with the help of "Cracking the code interview" (5th Edition) book, and I was solving the next problem:

Implement a function to check if a binary tree is a binary search tree (Q 4.5 pg 86).

Before we move on, I would like just to remind you the difference between a Binary search tree to a simple Binary tree:

A Binary search tree imposes the condition that for all nodes, the left children are less than or equal to the current node, which is less than all the right nodes.

So one of the solution the book offers is to scan the tree with In-Order traversal and on the fly to check if every node we visit is greater then the last one, and it assumes the tree can't have a duplicate values:

public static int last_printed = Integer.MIN_VALUE;
public static boolean checkBST(TreeNode n) {
    if(n == null) return true;

        // Check / recurse left
        if (!checkBST(n.left)) return false;

        // Check current
        if (n.data <= last_printed) return false;
        last_printed = n.data;

        // Check / recurse right
        if (!checkBST(n.right)) return false;

        return true; // All good!
}

Now, up here everything is good, but then the book quotes :

If you don't like the use of static variables, then you can tweak this code to use a wrapper class for the integer, as shown below:

Class WrapInt {
    public int value;
}     

After reading on wrapper class all over here and in other websites I just couldn't come to the conclusion, why and how should I use the wrapper class here instead of the static variable?

like image 853
vigdora Avatar asked Apr 21 '15 11:04

vigdora


People also ask

What can I use instead of a static variable?

Instance Variables: Instance variables are non-static variables and are declared in a class outside any method, constructor or block.

What is the advantage of using wrapper classes?

The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which is only possible with the help of Wrapper classes. As the wrapper classes have objects we can store null as a value. We could not store null in variables of primitive datatype.

Why would we need to use the Integer wrapper class?

Why we need Wrapper Class. Wrapper Class will convert primitive data types into objects. The objects are necessary if we wish to modify the arguments passed into the method (because primitive types are passed by value).

What are wrapper classes and why are they useful for Arraylists?

The wrapper class objects support synchronization in the case of multithreading. It allows various features of ArrayList such as dynamic sizing and implementation of associated functions which are only defined on class objects. Without a wrapper class it will be very difficult to implement an ArrayList.


2 Answers

This is a mechanism whereby you can create an instance of WrapInt, and pass it around. You then expose the value only to code that should know about it, instead of a public static non-final variable that can be accessed and changed from anywhere.

The reason you have the wrapper class is because Java primitives are pass-by-value; passing around an int and then updating it wouldn't propagate the change through the rest of your system.

This would look like this:

public static boolean checkBST(TreeNode n) {
    WrapInt counter = new WrapInt();
    return checkBST(n, counter);
}

public static boolean checkBST(TreeNode n, WrapInt counter) {
    if(n == null) return true;

        // Check / recurse left
        if (!checkBST(n.left, counter)) return false;

        // Check current
        if (n.data <= counter.value) return false;
        counter.value = n.data;

        // Check / recurse right
        if (!checkBST(n.right, counter)) return false;

        return true; // All good!
}
like image 64
Steve Chaloner Avatar answered Sep 26 '22 15:09

Steve Chaloner


Here you go:

public static boolean checkBST(TreeNode n) {
     WrapInt i = new WrapInt();
     i.value = INTEGER.MIN_VALUE;
     doCheckBST(n, i);
}

private static boolean doCheckBST(TreeNode n, WrapInt last_printed) {
if(n == null) return true;

    // Check / recurse left
    if (!checkBST(n.left, last_printed)) return false;

    // Check current
    if (n.data <= last_printed.value) return false;
    last_printed.value = n.data;

    // Check / recurse right
    if (!checkBST(n.right, last_printed)) return false;

    return true; // All good!
}
like image 32
Sergey Alaev Avatar answered Sep 25 '22 15:09

Sergey Alaev