A Basic Implementation of Binary Tree in Java

A Binary Tree simply can be defined in Java as a node that holds a value for itself and holds reference to a left child node and a right child node. The binary tree can have nodes that have a maximum of 2 children. As you can see in the example below, I have defined the value to be of type Comparable, so that it can be any object that can be compare for less than, greater than or equals. However, if you are going to only store either integers or Strings, for example, you can change the data type to int or String as needed.

The following is a very simple implementation of this concept.

Note: – if the binary tree has left nodes only or right nodes only, it becomes a singly linked list.

package com.icodejava.blog.published.datastructure;
* @author Kushal Paudyal
* Created on 12/5/2016
* Last Modified on 12/5/2016
* Binary Tree Node representation.
* - Node has a value, a left node and a right node.
* - Single node, when created, has left and right node as null.
class BinaryTreeNode {
Comparable value;
BinaryTreeNode left;
BinaryTreeNode right;

public BinaryTreeNode(Comparable value) {
this.value = value;
this.left = null;
this.right = null;

public Comparable getValue() {
return value;

public void setValue(Comparable value) {
this.value = value;

public BinaryTreeNode getLeft() {
return left;

public void setLeft(BinaryTreeNode left) {
this.left = left;

public BinaryTreeNode getRight() {
return right;

public void setRight(BinaryTreeNode right) {
this.right = right;