Binary Search Tree: Binary search tree is a hierarchical data structure which is used to store data.In a binary tree each node has maximum of two children.Binary Search tree shows a special feature given below:
- Value of Left child node should be less than its parent’s node value
- Value of right child node should be greater than its parent’s node value.
Important terms used in Binary tree:
- Root :Topmost node of the tree is called root node.
- Child Node: Child node is a node which is below a given node and connected through an edge in downward direction.
- Parent Node : Any node except root node which is above a given node and connected through an edge in upward direction.
- Leaf : Leaf node is the node which does not have any children
- Keys : Key represents value of a node.
- Path : Path represents sequence of nodes along the edges.
- Levels : Root node is at level 0 and next child node is at Level 1 ,so Level represents height of tree from the root node.
- Visiting : checking the value of node during traversal.
Binary search Tree implementation in Java
In the below program , we will implement Binary search tree using Java.
newNode() method will insert node in the binary tree, first inserted node is root node which is 6 in our example. Inside newNode() method we are checking whether new node value is less than or greater than root node value 6. If it is greater than 6 then keep this node at the left side of root node 6 otherwise keep the given node at the right side. In this way we are calling newNode method recursively to check and insert the given node at the correct place.
printTree() method will first print left nodes then root node and then right node using recursion.
public int data;
public Node left;
public Node right;
public class BinaryTree
static Node newNode(int data, Node node)
node = new Node();
node.data = data;
node.left = null;
else if(data < current.data)
static void printTree(Node root)
System.out.print(root.data +" ");
public static void main(String args)
Node rootNode = null;
1 2 3 5 6 7 8 10
6 is root node
Type of Traversals in binary Tree:
- Preorder Traversal– In Preorder traversal, first of all root node is visited then traverse the left subtree and at last traverse the right subtree
Visit Root–>Left Subtree traversal –> Right Tree Traversal
For binary tree given in above example,Preorder would be 6,1,2,3,5,7,8,10
- Inorder Traversal− In Inorder traversal, traverse the left subtree then visit the root node and at last traverse the right subtree
Left Subtree traversal –>Visit Root–>Right Tree Traversal
For binary tree given in above example,Preorder would be 1,2,3,5,6,7,8,10
- Postorder Traversal− − In Postorder traversal, traverse the right then visit the traverse the left subtree and at last visit the root node.
Right Tree Traversal->Left Subtree traversal –>Visit Root
For binary tree given in above example,Preorder would be 7,8,10,1,2,3,5,6
Use cases of Binary Search Tree:
1)Binary Search Tree is used in Search applications like dictionary
2) It is used in multilevel indexing in database