Java binary tree code

Binary Tree are the specialized tree that has two possible branches i.e left and right branch.

Java binary tree code


Binary Tree are the specialized tree that has two possible branches i.e left and right branch. These tree are useful when you build a parse of tree especially in mathematics and Boolean. The java binary tree find its application in games. Binary Tree is basic concept of data structure.

Understand with Example

In this Tutorial we want to describe you a code that helps you in understanding Java binary tree code. For this we have a class name BinayTreeExample.Inside the main method we instantiate Binary Tree Example class to a memory, that call run( ) method. Inside this class  we have a static node class and have two static node variable Node left, Node right and int value to store the respective node value. The Node constructor create a node object that accept a node value as argument that can be object or reference. The run method ( ) create an instance of node class start with node value of 25.The System.out.println print the value  of the node by  calling -

rootnode.value - The rootnode.value return you the value of the node.

In the same way you insert the different value in the node using insert  ( ) method.. The insert method accept a node and int as argument value. The conditional operator is used to evaluate the position of various node, if the value of the node is less than the root node then, the system.out.println print the value of node to the left of root node, Incase the value of node is greater than root node, the println print the node value to the right of it.

public class BinaryTreeExample {

  public static void main(String[] args)

  new BinaryTreeExample().run();

  static class Node 


  Node left;
  Node right;
  int value;

  public Node(int value) {
  this.value = value;

  public void run() {
  Node rootnode = new Node(25);
  System.out.println("Building tree with rootvalue 
+ rootnode.value);
  insert(rootnode, 11);
  insert(rootnode, 15);
  insert(rootnode, 16);
  insert(rootnode, 23);
  insert(rootnode, 79);
  System.out.println("Traversing tree in order");


  public void insert(Node node, int value) {
  if (value < node.value) {
  if (node.left != null) {
  insert(node.left, value);
  else {
  System.out.println("  Inserted " + value + 
    " to left of node " + node.value);
  node.left = new Node(value);
  else if (value > node.value) {
  if (node.right != null) {
  insert(node.right, value);
  else {
  System.out.println("  Inserted " + value + 
  to right of node " 
+ node.value);
  node.right = new Node(value);

  public void printInOrder(Node node) {
  if (node != null) {
  System.out.println("  Traversed " + node.value);

Output of the program

  Building tree with root value 25
  Inserted 11 to left of node 25
  Inserted 15 to right of node 11
  Inserted 16 to right of node 15
  Inserted 23 to right of node 16
  Inserted 79 to right of node 25

Traversing tree in order
  Traversed 11
  Traversed 15
  Traversed 16
  Traversed 23
  Traversed 25
  Traversed 79

Download Source code

Share on Google+Share on Google+

Java binary tree code

Posted on: November 6, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java binary tree code   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
October 4, 2011

I dont understand how is it traversing the subtree.
October 29, 2011
binary tree

found it useful.....well written.....easy to understand
Swaroop Kumar
May 30, 2012
Out of memory error

Your program has a static nested class Node which has left, right member variables of type Node. Doesn't this cause out of memory error while initializing an object of type Node as vm tries to allocate memory to Node objects recursively.
Aatish Parab
January 6, 2013
changes in public void printInOrder(Node node)

public void printInOrder(Node node) { if (node.left != null) { printInOrder(node.left); } else { System.out.println(" Traversed " + node.value); printInOrder(node.right); }
December 26, 2013
delete particular node in java

how about the method to delete PARTICULAR NODE in the same coding . help me :))
February 4, 2014
binary tree by using scanner

code binary tree by using scanner
Alan Barsalote Jr.
February 6, 2014
Nice :D

It was very understandable for a newbie like me :D Thanks Fella ,
Sourav Pati
May 20, 2014
Binary Tree

The above implementation is actually a binary search tree, cause all the left node contain smaller value than their parent node.