package tree; /* Find height of Binary Tree. Input: 50 -----------------------> height is 5 / \ / \ 20 80 ------------------> height is 2 / \ / \ 10 30 70 100 --------------> height is 0 / 5 --------------------------------> height is 1 / 1 ----------------------------------> height is 0 There are two conventions to define height of Binary Tree 1) Number of nodes on longest path from root to the deepest node. ie number of nodes present from root node to the deepest node present in tree. 2) Number of edges on longest path from root to the deepest node. ie number of connecting edges present from root node to the deepest node present in tree. In this post, the first convention is followed. Note: Height of Binary Tree is height of Root Node. Output: 5 */ public class FindHeightOfBinaryTree { private Node rootNode; public static void main(String[] args) { new FindHeightOfBinaryTree(); } public FindHeightOfBinaryTree(){ addNode(rootNode, 50); addNode(rootNode, 20); addNode(rootNode, 80); addNode(rootNode, 10); addNode(rootNode, 30); addNode(rootNode, 70); addNode(rootNode, 100); addNode(rootNode, 5); addNode(rootNode, 1); printTreeInOrder(rootNode); System.out.println(); System.out.println("Height of Tree is (Edges) :"+calculateHeightConsideringEdges(rootNode)); System.out.println("Height of Tree is (Nodes) :"+calculateHeightConsideringNodes(rootNode)); } //Assumption that empty tree has height 0. private int calculateHeightConsideringNodes(Node rootNode) { if(rootNode==null){ return 0; } int leftHeight = calculateHeightConsideringNodes(rootNode.getLeft()); int rightHeight = calculateHeightConsideringNodes(rootNode.getRight()); if(leftHeight>rightHeight){ return leftHeight+1; }else{ return rightHeight+1; } } //Assumption that empty tree has height -1. private int calculateHeightConsideringEdges(Node rootNode) { if(rootNode==null){ return -1; } int leftHeight = calculateHeightConsideringEdges(rootNode.getLeft()); int rightHeight = calculateHeightConsideringEdges(rootNode.getRight()); if(leftHeight>rightHeight){ return leftHeight+1; }else{ return rightHeight+1; } } private void printTreeInOrder(Node rootNode){ if(rootNode==null) return; printTreeInOrder(rootNode.getLeft()); System.out.print(rootNode.getData() + " "); printTreeInOrder(rootNode.getRight()); } private void addNode(Node rootNode, int data){ if(rootNode==null){ Node temp1 = new Node(data); this.rootNode=temp1; }else{ addNodeInProperPlace(rootNode, data); } } private void addNodeInProperPlace(Node rootNode, int data){ if(data>rootNode.getData()){ if(rootNode.getRight()!=null){ addNode(rootNode.getRight(), data); }else{ Node temp1 = new Node(data); rootNode.setRight(temp1); } }else if(data<rootNode.getData()){ if(rootNode.getLeft()!=null){ addNode(rootNode.getLeft(), data); }else{ Node temp1 = new Node(data); rootNode.setLeft(temp1); } } } }
Questions on Stack, Queues, Linkedlist, Binary Trees, Sorting, Searching, Graphs etc with solution using Java Language.
Saturday, 27 June 2015
Find height of Binary Tree. OR Find the Maximum Depth of Binary Tree.
Labels:
Binary Search Tree,
Binary Tree