Saturday, August 18, 2012

Binary Tree Functions implementation using Java

package com.practice;


public class TreeNode {

TreeNode leftReference;
TreeNode rightReference;
int nodeValue;

TreeNode(int nodeValue){
this.nodeValue=nodeValue;
}

}


package com.practice;

public class BinaryTree {

  public static void main(String args[]){

  // Tree root node.
        int rootValue = 40;
        TreeNode rootnode = new TreeNode(rootValue);
        System.out.println("root node created. " + rootnode.nodeValue);
     
        // Inserting elements into binary tree
        insertion(rootnode, 45);
        insertion(rootnode, 13);
        insertion(rootnode, 78);
        insertion(rootnode, 33);
        insertion(rootnode, 16);
        System.out.println("Binary Tree");

        printTree(rootnode);

        System.out.println("Depth of Tree"+printDepth(rootnode));
     
        printPreOrderTravel(rootnode);
        printInOrderTravel(rootnode);
        printPostOrderTravel(rootnode);
     
        System.out.println("Get number of Nodes"+getNumberOfNodes(rootnode));
    }

   // Get the number of Nodes in binary tree
    private static int getNumberOfNodes(TreeNode rootnode) {
    if(rootnode== null)
    return 0;
    else
    return 1+ getNumberOfNodes(rootnode.leftReference) + getNumberOfNodes(rootnode.rightReference);
    }

//PostOrder Travel
private static void printPostOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
printPreOrderTravel(rootnode.leftReference);
printPreOrderTravel(rootnode.rightReference);
System.out.println(rootnode.nodeValue);
}
}

//PreOrder Travel
  private static void printInOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
printPreOrderTravel(rootnode.leftReference);
System.out.println(rootnode.nodeValue);
printPreOrderTravel(rootnode.rightReference);
}
  }
 
   //PreOrder Travel
  private static void printPreOrderTravel(TreeNode rootnode) {
if(rootnode!=null){
System.out.println(rootnode.nodeValue);
printPreOrderTravel(rootnode.leftReference);
printPreOrderTravel(rootnode.rightReference);
}
  }

  //Finding depth of binary Tree
public static int  printDepth(TreeNode rootnode) {

  if(rootnode==null)
  return 0;
  else
  return 1+ Math.max(printDepth(rootnode.leftReference),printDepth(rootnode.rightReference));
}

//Inserting node into binary Tree
public static void insertion(TreeNode parentNode,int nodeValue){
  if(nodeValue<parentNode.nodeValue){
  if(parentNode.leftReference!=null)
  insertion(parentNode.leftReference,nodeValue);  // Go more depth to right
  else
  parentNode.leftReference=new TreeNode(nodeValue);
  }
  else if(nodeValue >parentNode.nodeValue){
  if(parentNode.rightReference!=null)
  insertion(parentNode.rightReference,nodeValue);  // Go more depth to left
  else
  parentNode.rightReference=new TreeNode(nodeValue);
  }
  }
   
  // Printing Tree contents
    public static void printTree(TreeNode node) {
        if (node != null) {
            printTree(node.leftReference);
            System.out.println("Node" + node.nodeValue);
            printTree(node.rightReference);
        }
    }
}

No comments:

Post a Comment

Thanks for your valuable comments