Inorder traversal of binary tree - Java:

```
/**
* This class is used to create a binary tree and perform inorder traversal.
* @author
* @version 1.0
*/
public class BinaryTree {
/**
* This method is used to create a binary tree.
* @param root root of the tree.
* @param data data to be inserted in the tree.
*/
public void insert(Node root, int data) {
if (root == null) {
root = new Node(data);
} else if (data <= root.data) {
if (root.left != null) {
insert(root.left, data);
} else {
root.left = new Node(data);
}
} else {
if (root.right != null) {
insert(root.right, data);
} else {
root.right = new Node(data);
}
}
}
/**
* This method is used to perform inorder traversal of the binary tree.
* @param root root of the tree.
*/
public void inorderTraversal(Node root) {
if (root != null) {
inorderTraversal(root.left);
System.out.print(root.data + " ");
inorderTraversal(root.right);
}
}
/**
* This method is used to create a binary tree and perform inorder traversal.
* @param args command line arguments (ignored).
*/
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
Node root = null;
bt.insert(root, 10);
bt.insert(root, 20);
bt.insert(root, 30);
bt.insert(root, 40);
bt.insert(root, 50);
bt.insert(root, 60);
bt.inorderTraversal(root);
}
/**
* This class is used to create a node of the binary tree.
*/
class Node {
int data;
Node left;
Node right;
/**
* This constructor is used to create a node of the binary tree.
* @param data data to be inserted in the node.
*/
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
}
```

## Top comments (0)