Implementing a Binary Tree in Java
Last Updated :
15 May, 2024
A binary tree is a hierarchical data structure composed of the nodes. Each node contains the value and references to its left child node and right child node, which are also binary trees that are possibly null. The structure resembles the tree with the nodes branching out from a central root, where each node have at most two children such as the left child node and the right child node.
The binary tree finds applications in different domains like computer science algorithms, database indexing, file systems etc. They offer efficient search, insertion and deletion operations when the appropriate balanced. In this Java, we will explore the basics of the binary tree. The implementation is focused on simplicity and clarity, it provides a solid foundation for understanding more advanced binary tree concepts and their applications.
Organization of Binary Tree in Java
Representation of Binary Tree:

Explanation of the Image:
- The root node of the binary tree is 8.
- The left child of the root has value 3 and it children are 1 and 6, and 6 has 2 children i.e. 4 and 7.
- The right child of the root has 10 and its children are 13 and 14.
- 1, 4, 7, 13 and 14 are leaf nodes which are without any children.
Implementation of Binary Tree:
Java
// Node Class
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
// BinaryTree Class
public class BinaryTree {
Node root;
public BinaryTree() {
root = null;
}
// Method to insert a new node with given key
public void insert(int key) {
root = insertRec(root, key);
}
// A recursive function to insert a new key in BST
private Node insertRec(Node root, int key) {
// If the tree is empty, return a new node
if (root == null) {
root = new Node(key);
return root;
}
// Otherwise, recur down the tree
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
// return the (unchanged) node pointer
return root;
}
// Method to print the tree inorder
public void inorder() {
inorderRec(root);
}
// A utility function to do inorder traversal of BST
private void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
// Method to search for a key in the tree
public boolean search(int key) {
return searchRec(root, key);
}
// A utility function to search for a key in BST
private boolean searchRec(Node root, int key) {
if (root == null)
return false;
if (root.key == key)
return true;
if (key < root.key)
return searchRec(root.left, key);
else
return searchRec(root.right, key);
}
// Method to find the minimum value in the tree
public int findMin() {
return findMinRec(root);
}
// A utility function to find the minimum value in BST
private int findMinRec(Node root) {
if (root == null)
throw new IllegalStateException("Tree is empty");
if (root.left == null)
return root.key;
return findMinRec(root.left);
}
// Method to find the maximum value in the tree
public int findMax() {
return findMaxRec(root);
}
// A utility function to find the maximum value in BST
private int findMaxRec(Node root) {
if (root == null)
throw new IllegalStateException("Tree is empty");
if (root.right == null)
return root.key;
return findMaxRec(root.right);
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
// Insert some nodes
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// Print inorder traversal of the tree
System.out.println("Inorder traversal:");
tree.inorder();
// Output: 20 30 40 50 60 70 80
// Search for a key
int searchKey = 40;
if (tree.search(searchKey))
System.out.println("\nKey " + searchKey + " found in the tree.");
else
System.out.println("\nKey " + searchKey + " not found in the tree.");
// Find minimum and maximum values
System.out.println("Minimum value in the tree: " + tree.findMin());
System.out.println("Maximum value in the tree: " + tree.findMax());
}
}
OutputInorder traversal:
20 30 40 50 60 70 80
Key 40 found in the tree.
Minimum value in the tree: 20
Maximum value in the tree: 80
Complexity of the above Program:
Operation
| Explanation
| Complexity
|
---|
Insertion
| - This operation is used to insert a new node into binary tree.
| - The time complexity of insertion operation is O(log n).
- The space complexity of insertion operation is O(1).
|
---|
Deletion
| - This operation is used to remove a node from binary tree.
| - The time complexity of deletion operation is O(log n).
- The space complexity of deletion operation is O(1).
|
---|
Traversal
| - This operation involves the 3 types of traversals such as: Inorder Traversal, Preorder Traversal, Post order Traversal.
- Inorder Traversal is defined as the visit the left subtree after that visit current node and then visit right subtree.
- Preorder Traversal is defined as it visit the current node after that visit left subtree and then visit right subtree.
- Postorder Traversal is defined as it visit left subtree after that visit right subtree and then visit current node.
| - The time complexity of traversal is O(n).
- The space complexity of traversal is O(log n).
|
---|
Searching
| - This operation is used to find the specific key or value in binary tree.
| - The time complexity of searching is O(log n).
- The space complexity of searching is O(1).
|
---|
Height/Depth Calculation
| - This operation is used to calculate the maximum height of binary tree.
| - The time complexity of height calculation is O(n).
- The space complexity of height calculation is O(log n).
|
---|
Balanced Checking
| - This operation is used to check the binary tree is balanced or not.
| - The time complexity of balanced checking is O(n).
- The space complexity of balanced checking is O(log n).
|
---|
Finding Minimum/Maximum
| - This operation is used to find the maximum value or minimum value in binary tree.
| - The time complexity of finding minimum/maximum is O(log n).
- The space complexity of finding minimum/maximum is O(1).
|
---|
Applications of Binary Tree
Binary trees find applications in different domains due to their efficient structure for the organizing and manipulating the data. Some of the basic applications of binary tree are:
- Binary Search Trees (BST)
- Expression Evaluation
- Binary Heaps
- Balanced Binary Trees
- Binary Tree Traversal Algorithms
- File Systems
- Game Trees
- Huffman Encoding
- Trie Data Structure
Similar Reads
Implement a Binary Heap in Java A Binary Heap is a Data Structure that takes the form of a binary tree but it is implemented by using an Array. It is primarily used to efficiently manage a collection of elements with priority-based operations like priority queue we have two types of binary heaps namely Min Heap and Max Heap. Basic
6 min read
Binary Tree (Array implementation) Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation o
6 min read
B-Tree Implementation in C++ In C++, B-trees are balanced tree data structures that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time. B-trees are generalizations of binary search trees (BST) in that a node can have more than two children. B-trees are optimized for systems
13 min read
Implementation of B+ Tree in C A B+ tree is a self-balancing tree data structure that maintains sorted data and allows searching, inserting, and deletion of data in logarithmic time. In B+ trees, the actual data is stored inside the leaf nodes and the internal nodes act as pointers to the leaf nodes. In this article, we will lear
15+ min read
Implementation of B-Tree in C The B-tree is a self-balancing ordered structured data that stores data in a set of pages and also allows efficient searching, insertion, and deletion operations. It has its origin in a generic form of binary search trees developed by Rudolf Bayer and Edward M. McCreight in 1972. It is capable of pr
5 min read
Implement Binary Search Tree(BST) Iterator Binary Search Trees (BSTs) are data structures, in computer science. They are commonly used for searching, insertion and deletion operations. In situations it becomes necessary to traverse a BST in an order. For example an in order traversal visits nodes in ascending order based on their values. Thi
6 min read
Java Program to Implement B+ Tree The B+ tree is a self-balancing tree data structure commonly used in database and file systems applications. It is an extension of B-Tree and maintains sorted data in a manner that allows us for efficient insertion, deletion and search operations. The B+Trees stores all data in a leaf node while int
6 min read
Inorder Tree Traversal in Binary Tree in C A binary Tree is a hierarchical data structure in which each node has at most two children and it can referred to as the left child and right child. Due to being a non-linear data structure, different traversal techniques are possible for it. Inorder tree traversal is one of the techniques used to v
3 min read
Binary Tree in C A binary tree is a non-linear hierarchical data structure in which each node has at most two children known as the left child and the right child. It can be visualized as a hierarchical structure where the topmost node is called the root node and the nodes at the bottom are called leaf nodes or leav
12 min read
B-Tree in Java A B-tree is a self-balanced tree data structure that will maintain the sorted data and allow for operations such as insertion, deletion and search operations. B-tree is particularly well-suited for systems that need to perform disk-based operations and it minimizes the number of disk accesses requir
6 min read