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. */ @SuppressWarnings("rawtypes") 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; } }

## More from: Data Structure

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