# Program: Find height of a Binary Search Tree (BST)

 Description: For a binary tree to be a binary search tree (BST), the data of all the nodes in the left sub-tree of the root node should be less than or equals to the data of the root. The data of all the nodes in the right subtree of the root node should be greater than the data of the root. This example shows how to find height of a binary search tree. Here is an example picture of binary search tree (BST):

 BstNode ```package com.java2novice.ds; public class BstNode { private BstNode left; private BstNode right; private Integer data; public BstNode(Integer data) { this.data = data; } public BstNode getLeft() { return left; } public void setLeft(BstNode left) { this.left = left; } public BstNode getRight() { return right; } public void setRight(BstNode right) { this.right = right; } public Integer getData() { return data; } } ```

 BinarySearchTreeImpl ```package com.java2novice.ds; public class BinarySearchTreeImpl { private BstNode root; public boolean isEmpty() { return (this.root == null); } public void insert(Integer data) { System.out.print("[input: "+data+"]"); if(root == null) { this.root = new BstNode(data); System.out.println(" -> inserted: "+data); return; } insertNode(this.root, data); System.out.print(" -> inserted: "+data); System.out.println(); } private BstNode insertNode(BstNode root, Integer data) { BstNode tmpNode = null; System.out.print(" ->"+root.getData()); if(root.getData() >= data) { System.out.print(" [L]"); if(root.getLeft() == null) { root.setLeft(new BstNode(data)); return root.getLeft(); } else { tmpNode = root.getLeft(); } } else { System.out.print(" [R]"); if(root.getRight() == null) { root.setRight(new BstNode(data)); return root.getRight(); } else { tmpNode = root.getRight(); } } return insertNode(tmpNode, data); } public Integer findHeight(){ return getNodeHeight(this.root); } private Integer getNodeHeight(BstNode node) { if(node == null) { return -1; } return Math.max(getNodeHeight(node.getLeft()), getNodeHeight(node.getRight()))+1; } public static void main(String a[]) { BinarySearchTreeImpl bst = new BinarySearchTreeImpl(); bst.insert(10); bst.insert(20); bst.insert(21); bst.insert(8); bst.insert(6); bst.insert(16); bst.insert(23); bst.insert(2); System.out.println("-------------------"); System.out.println("Height of the tree: "+bst.findHeight()); } } ```

 Output: ```[input: 10] -> inserted: 10 [input: 20] ->10 [R] -> inserted: 20 [input: 21] ->10 [R] ->20 [R] -> inserted: 21 [input: 8] ->10 [L] -> inserted: 8 [input: 6] ->10 [L] ->8 [L] -> inserted: 6 [input: 16] ->10 [R] ->20 [L] -> inserted: 16 [input: 23] ->10 [R] ->20 [R] ->21 [R] -> inserted: 23 [input: 2] ->10 [L] ->8 [L] ->6 [L] -> inserted: 2 ------------------- Height of the tree: 3```

#### List Of All Interview Programs:

Knowledge Centre
Stream and types of Streams
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:

Byte Streams: Provide a convenient means for handling input and output of bytes. Byte Streams classes are defined by using two abstract classes, namely InputStream and OutputStream.

Character Streams: Provide a convenient means for handling input & output of characters. Character Streams classes are defined by using two abstract classes, namely Reader and Writer.
Famous Quotations
It is amazing what you can accomplish if you do not care who gets the credit.
-- Harry Truman