This blog will soon be merged with

JavaByPatel

which explains each solution in detail, to visit new blog, click JavaByPatel

Thursday, 25 June 2015

To Find the Smallest and Largest Elements in the Binary Tree. (Note: Given tree is Binary Tree and not a Binary Search Tree.)


package tree;

import java.util.LinkedList;
import java.util.Queue;
/*
  Find Minimum and Maximum Node value in Binary Tree (Not in Binary Search Tree.)
  Input:
                  10 
                /    \
               2      15
              / \    /
             4   6  3  
                      
    Output:                     
         Maximum = 15
         Minimum = 2
 */
public class FindMaxMinElementInBinaryTree {
 private Node rootNode;

 public static void main(String[] args) {
  new FindMaxMinElementInBinaryTree();
 }

 public FindMaxMinElementInBinaryTree(){
  addNode(rootNode, 10); 
  addNode(rootNode, 2); 
  addNode(rootNode, 15); 
  addNode(rootNode, 4); 
  addNode(rootNode, 6); 
  addNode(rootNode, 3); 

  printTreeLevelOrder(rootNode);
  System.out.println();
  System.out.println("MAX : "+findMax(rootNode));
  System.out.println("MIN : "+findMin(rootNode));
 }
 
 private int findMax(Node rootNode) {
  if(rootNode==null)
   return Integer.MIN_VALUE;

  int left = findMax(rootNode.getLeft());
  int right = findMax(rootNode.getRight());

  if(rootNode.getData()>left && rootNode.getData()>right)
   return rootNode.getData();
  else if(left>right)
   return left;
  else
   return right;
 }

 private int findMin(Node rootNode) {
  if(rootNode==null)
   return Integer.MAX_VALUE;

  int left = findMin(rootNode.getLeft());
  int right = findMin(rootNode.getRight());
  if(rootNode.getData()<left && rootNode.getData()<right)
   return rootNode.getData();
  else if(left<right)
   return left;
  else
   return right;
 }

 private void printTreeLevelOrder(Node rootNode){
  if(rootNode==null)
   return;
  Queue<Node> queue = new LinkedList<Node>();
  queue.add(rootNode);

  while(!queue.isEmpty()){
   Node obj = (Node)queue.poll();
   System.out.print(obj.getData() + " ");
   if(obj.getLeft()!=null)
    queue.add(obj.getLeft());

   if(obj.getRight()!=null)
    queue.add(obj.getRight());
  }
 } 

 private void addNode(Node rootNode, int data){

  //First check is there any Nodes present.
  if(rootNode==null){
   // No Nodes are Present, create one and assign it to startNode
   Node temp1=new Node(data);
   this.rootNode=temp1;
  }else{
   //Nodes present, So check the proper position where to add New Node.

   //Checking whether Left child and Right child are present for root Node. 
   if(rootNode.getLeft()!=null && rootNode.getRight()!=null){
    //Left and Right Child exist, Also, we need to add ne Node in Sequential Fashion of Left and Right, 
    //We have to scan all Levels one by one to check a proper place for new Node.
    //Also, we for each and every node we need to check whether Left and Right Exist, 
    //So we need to store them for later Processing that is why we introduced Queue.

    Queue<Node> queue = new LinkedList<Node>();
    queue.add(rootNode);

    while(!queue.isEmpty()){
     Node obj = (Node)queue.poll();
     if(obj.getLeft()!=null && obj.getRight()!=null){
      queue.add(obj.getLeft());
      queue.add(obj.getRight());
     }else if(obj.getLeft()==null){
      Node temp1=new Node(data);
      obj.setLeft(temp1);
      break;
     }else{
      Node temp1=new Node(data);
      obj.setRight(temp1);
      break;
     }
    }

   }else{
    // We reach this condition when either Left or Right is Null, but not sure which side is Null.
    if(rootNode.getLeft()==null){
     Node temp1=new Node(data);
     rootNode.setLeft(temp1);
    }else{
     Node temp1=new Node(data);
     rootNode.setRight(temp1);
    }
   }
  }
 }
}