Write a Java Program to Implement Binary Tree Data Structure

A binary tree is a data structure where each node can have at most two child nodes.

In this tutorial, we will learn how to implement a binary tree data structure in Java.


Node Class

The first step in implementing a binary tree is to define the node class.

Each node in a binary tree has three attributes: data, left child, and right child.

The data attribute stores the value of the node, while the left child and right child attributes point to the left and right child nodes, respectively.

public class Node 
{ 
int data; Node left; 
Node right;
public Node(int data) {
    this.data = data;
    left = null;
    right = null;
}

Binary Tree Class

The next step is to define the binary tree class.

The binary tree class will have only one attribute, which is the root node.

The root node is the topmost node in the binary tree.

public class BinaryTree 
{ 

Node root;

public BinaryTree() {
root = null;
}

Insert Method

The insert method is used to insert a node into the binary tree.

The insert method first checks if the root node is null.

If the root node is null, the new node becomes the root node.

Otherwise, the insert method traverses the binary tree recursively until it finds an empty spot to insert the new node.

public void insert(int data) 
{ 

root = insertNode(root, data);

 }

public Node insertNode(Node current, int data) 

{ 

if (current == null) 

{ 

return new Node(data); 

} 

if (data < current.data) 

{ 

current.left = insertNode(current.left, data); 

} 

else if (data > current.data) 

{ 

current.right = insertNode(current.right, data); 

} 

return current; 

}

Traversal Methods

There are three commonly used traversal methods for a binary tree: Inorder traversal, Preorder traversal, and Postorder traversal.

In inorder traversal, the left subtree is visited first, then the root node, and finally, the right subtree is visited.

In preorder traversal, the root node is visited first, then the left subtree, and finally, the right subtree is visited.

In postorder traversal, the left subtree is visited first, then the right subtree, and finally, the root node is visited.

public void inorderTraversal(Node current) 
{ 

if (current != null) { inorderTraversal(current.left); 

System.out.print(current.data + " "); 

inorderTraversal(current.right); 

} 

}

public void preorderTraversal(Node current) { if (current != null) 

{ 

System.out.print(current.data + " "); 

preorderTraversal(current.left); preorderTraversal(current.right); 

} 

}

public void postorderTraversal(Node current) 

{ 

if (current != null) 

{ 

postorderTraversal(current.left); 

postorderTraversal(current.right); 

System.out.print(current.data + " "); 

} 

}

Conclusion

In conclusion, implementing a binary tree data structure in Java is not difficult.

It involves defining the node class, binary tree class, insert method, and traversal methods.

By following the steps outlined in this tutorial, you can easily create and manipulate binary trees in Java.