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;


[My Video Promotion]
  1. Implementing a Simple LIFO Stack in Java using LinkedList
  2. Implementing a Simple FIFO Queue in Java Using Linked List
  3. In Place Matrix (2D Array) Clockwise and Counterclockwise Rotation – Java Implementation
  4. Matrix (2D Array) Clockwise and Counterclockwise Rotation with Extra Buffer – Java Implementation
  5. Array Max Integer Finder (With Big O Analysis)
  6. A Basic Implementation of Binary Tree in Java
  7. A Basic Implementation of Generic Tree in Java
  8. Basic example of implementing Singly Linked List in Java
  9. How to find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree
  10. How to Add and Remove nodes in an unsorted Linked List
  11. Binary Tree Representation and Tree Traversal (In-Order, Pre-Order, Post-Order)
  12. Rotating a two dimensional integer array In-Place and using extra memory
  13. How to reverse a Singly Linked List iteratively and recursively
  14. Sorted Circular Linked List Implementation And Insert Operation
  15. Finding Mean Value Of An Integer Array In Java
  16. How to intersect two sets in Java using java.util.Set data structure.
  17. Bubble Sorting An Integer Array In Ascending and Descending Order
  18. How to split strings and separate the words with spaces
  19. How to easily sort characters in a String?
Tagged , , , . Bookmark the permalink.

One Response to How to easily sort characters in a String?

  1. Why visitors still use to read news papers when in this technological world all is available on net?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.