1

I want to create a binary search tree tool in which it outputs all the nodes including the null In my current code, it only create nodes in the input array including its left and child then print

Is it possible to print all nodes including in a null like shown below? input = {23, 5, 2, 89, 56, 43} output = {23, 5, 89, 2, null, 56, null, null, null ,null ,null, null, 43, null}

public class Node {
    int value;
    Node right,left;
    Node(){
        value = 0;
        left = null;
        right = null;  
    }

    Node(int i) {
        value = i;
        left = null;
        right = null;
    }
    public void setLeft(Node newValue){
        this.left = newValue;
    }
    public void setRight(Node newValue){
        this.right = newValue;
    }
    public int getValue(){
        return value;
    }
    public String getValueStr(){
        return Integer.toString(value);
    }
    public void printAll(){
        System.out.println("Value: "+ this.value
                +"\nLeft: "+ this.left
                +"\nRight: "+ this.right);
    }
    public void addChildToArr(ArrayList<String> arr){
        arr.add(right.getValueStr());
        arr.add(this.left.getValueStr());
    }
    public String getChildRightStr(){
        if(right == null)
            return "null";
        else
            return this.right.getValueStr();
    }
    public String getChildLeftStr(){
        if(left == null)
            return "null";
        else
            return this.left.getValueStr();
    }
}

public class BST {
    private static Node root;
    ArrayList<Node> nodes = new ArrayList<>();
    public BST(){
        root = null;
    }
     public void insert(int data)
     {
         root = insert(root, data);
     }
     /* Function to insert data recursively */
     private Node insert(Node node, int data)
     {
         if (node == null)
             node = new Node(data);
         else
         {
             if (data <= node.getValue()){
                 node.left = insert(node.left, data);
                 //nodes.add(node.left);
             }
             else{
                 node.right = insert(node.right, data);
                 //nodes.add(node.left);
             }
         }
         if(!(nodes.contains(node)))
             nodes.add(node);
         return node;
     }

    public void printNodes(){
        for(int i = 0; i < nodes.size();i++){
            System.out.print(nodes.get(i).getChildLeftStr()+" ,"+nodes.get(i).getChildRightStr()+", ");
        }
        System.out.println("");
    }
    public void printNodeObj(){
        for(int i = 0; i < nodes.size();i++){
            System.out.println(nodes.get(i));
        }
    }
    public int countNodes()
     {
         return countNodes(root);
     }
     /* Function to count number of nodes recursively */
     private int countNodes(Node r)
     {
         if (r == null)
             return 0;
         else
         {
             int l = 1;
             l += countNodes(r.getLeft());
             l += countNodes(r.getRight());
             return l;
         }
     }
    public static void main(String[] args) {
        BST bst = new BST();
        int[] arr = {23,5,2,89,56,43,38,10,65,72};
        System.out.print("["+arr[0]+", ");
        for(int i = 0; i< arr.length;i++)
            bst.insert(arr[i]);
        bst.printNodes();
    }
}

Thanks for the help.

MrFapoh
  • 23
  • 5

1 Answers1

0

First of all you make a BST using the values

23, 5, 2, 89, 56, 43

you will not get the result as

23, 5, 89, 2, null, 56, null, null, null ,null ,null, null, 43, null

since the structure will be like following.

         23
        /  \
      5     89
     / \    / \
    2   n  56  n
   / \     / \
  n  n    43  n
         / \
         n  n

NB: n denotes null [empty nodes]

instead, you will get the following result if you do level-order traversal (as you mentioned in comment section):

23, 5, 89, 2, null, 56, null, null, null, 43, null, null, null

If you clear on that, you can do following things to print the node if it has value or even if it doesn't have.

  1. do put the root in queue, and process it.
  2. poll up the first inserted item from the queue.
  3. if the polled item having left child, proces it and put this item on queue, else print 'null'
  4. if the polled item having right child, process it and put this item on queue, else print 'null'
  5. continueue to step 2 until queue become empty.

code:

private static void print(Node root) {
    Queue<Node> queue = new LinkedList<Node>();
    queue.add(root);

    /* step 1 */
    System.out.print(root.val+" "); 
    while(!queue.isEmpty()) {
         /* step 2 */
        BST temp = queue.poll();

        /* step 3 */
        if(temp.left != null) { 
            System.out.print(temp.left.val+" ");
            queue.add(temp.left);
        }else {
            System.out.print(temp.left+" ");
        }

        /* step 4 */
        if(temp.right != null) {
            System.out.print(temp.right.val+" ");
            queue.add(temp.right);
        }else {
            System.out.print(temp.right+" ");
        }
    }
}

Note: do comment if you not getting something.

Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17