Friday 25 August 2023

 BinaryTree Data Structure implementation in JAVA: (Hard Coding Method)


public class test {

    static class Node {   

        int data;

        Node left;

        Node right;


        public Node(int data) {

            this.data = data;

            this.left = null;  // even if we do not explicitly specify them as null  then also it is by default                                            //set up as null in JAVA but it is always a good practise to assign them as          this.right = null;                                    //null

         

        }

    }

 

    static class BinaryTree {

        static Node buildTree(int rootValue) {

            if (rootValue == -1) return null;

            Node newNode = new Node(rootValue); // Now you can instantiate Node directly

            //newNode.left = null;

            //newNode.right = null;

            return newNode;

        }

    }


    public static void main(String[] args) {

        // You can create instances of Node and BinaryTree here as needed

        BinaryTree bt = new BinaryTree();

//For the sake of understanding we are doing hardcoding now. We'll see user input coding also in next article

        Node root = BinaryTree.buildTree(10); //Node root = bt.buildTree(10);

        root.left = new Node(6); 

        root.right= new Node(12);

        root.left.left = new Node(5);

        root.right.right = new Node(7);

        System.out.println(root.data);

        System.out.println(root.left.data+" "+root.right.data);

        System.out.println(root.left.left.data+" "+root.right.right.data);

         /* Our Tree is something like below:

                10
                /\

            6       12

            /            \

        5                    7

*/

    }

}

/*

Question: when we are removing both newnode.right and newnode.left = null and also this,left=null and this.right=null then also we are getting root.left value as null and no error is coming why?

The reason you're not seeing any errors or changes in the behavior of your program when you remove both newnode.left = null; and newnode.right = null; from the buildTree method and also remove this.left = null; and this.right = null; from the Node constructor is because Java automatically initializes object fields to default values when an object is created.

In Java, when you create a new object (instance of a class), all instance variables (fields) are automatically initialized to default values. For numeric types like int, the default value is 0, and for object types, including reference types like Node, the default value is null.

In your Node class constructor, you are manually setting left and right to null, but if you don't provide any initialization for these fields, Java will still initialize them to null by default when you create a new Node object.

Similarly, in the buildTree method, even if you remove the lines that explicitly set newnode.left and newnode.right to null, Java will automatically set them to null when the Node object is created.

That's why you're not observing any difference in behavior or errors when you remove these lines. Java's default behavior takes care of it for you.

 */

No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...