# Introduction to Binary Trees in Java

Binary Trees are fundamental structures in computer science, forming the basis for more advanced data structures and algorithms. They are used extensively in databases, parsers, and other hierarchical data representation systems.

In this blog, we'll discuss a simple implementation of a binary tree in Java and explore how to construct and display a basic tree structure.

## **What is a Binary Tree?**

A binary tree is a hierarchical data structure where each node has at most two children:

1. **Left Child**
    
2. **Right Child**
    

This structure allows efficient organization and manipulation of data, making it a versatile tool in software development.

## Code Walkthrough

Below is the implementation of a basic binary tree in Java.

1. **Node Class**
    
    ```java
    class Node {
        int data;
        Node leftChild;
        Node rightChild;
        public Node(int data){
            this.data = data;
            this.leftChild = null;
            this.rightChild = null;
        }
        public int getData(){
            return this.data;
        }
    }
    ```
    
    #### **Explanation:**
    
    * **Attributes:**
        
        * `data`: Stores the value of the node.
            
        * `leftChild` and `rightChild`: Pointers to the left and right child nodes.
            
    * **Constructor:**
        
        * Initializes the node with the given `data` and sets child references to `null`.
            
    * **Accessor Method:**
        
        * `getData()` Returns the node’s value.
            
2. **BinaryTree Class**
    
    ```java
    class BinaryTree {
        Node rootNode;
        public BinaryTree(){
            this.rootNode = null;
        }
        public BinaryTree(int data){
            this.rootNode = new Node(data);
        }
    }
    ```
    
    #### **Explanation:**
    
    * **Attributes:**
        
        * `rootNode`: Represents the root of the tree.
            
    * **Constructors:**
        
        * A no-argument constructor initializes the tree with a `null` root.
            
        * A parameterized constructor allows direct initialization of the tree with a root node containing the specified data.
            
3. **Main Method**
    
    ```java
    class TestClass {
        public static void main(String[] args){
            BinaryTree tree = new BinaryTree();
            tree.rootNode = new Node(1);
            tree.rootNode.leftChild = new Node(2);
            tree.rootNode.rightChild = new Node(3);
    
            System.out.println("Tree is:");
            System.out.println(tree.rootNode.getData());
            System.out.print(tree.rootNode.leftChild.getData() + " ");
            System.out.println(tree.rootNode.rightChild.getData());
        }
    }
    ```
    
    #### **Steps:**
    
    1. **Create a BinaryTree Instance:**
        
        * A new tree object is created using the default constructor.
            
    2. **Construct the Tree:**
        
        * Manually set the `rootNode` And its children as follows:
            
            ```bash
                     1
                   /   \
                  2     3
            ```
            
    3. **Print the Tree:**
        
        * The root node and its children are printed using the `getData()` method.
            

## Output

When the code is executed, the following output is displayed:

```bash
Tree is:
1
2 3
```

This output represents the structure of the tree:

* Root: `1`
    
* Left Child: `2`
    
* Right Child: `3`
    

## **Key Features of This Implementation**

1. **Simplicity:**
    
    * The tree is built manually, making it easy to understand and visualize.
        
2. **Extensibility:**
    
    * The `BinaryTree` and `Node` classes can be extended to include additional functionalities like traversal, insertion, and deletion.
        
3. **Separation of Concerns:**
    
    * The `Node` the class handles individual nodes, while the `BinaryTree` the class manages the overall tree structure.
        

## **Applications of Binary Trees**

1. **Data Representation:**
    
    * Hierarchical data like file systems or organizational charts.
        
2. **Search Trees:**
    
    * Binary Search Trees (BST) enhance search operations.
        
3. **Expression Parsing:**
    
    * Binary Trees can represent mathematical expressions for compilers.
        

## Conclusion

This implementation demonstrates the foundational concepts of binary trees in Java. By understanding the basic structure and functionality, you can extend this to include traversal algorithms, dynamic tree construction, and more.

Binary trees are an indispensable tool in computer science. Mastering their implementation and usage will greatly enhance your problem-solving skills.

Happy coding! 😊

## **References**

1. Find the code for the above explanation [**here**](https://github.com/tanaytoshniwal/Data-Structures-Algorithms/blob/master/Data%20Structures/BinaryTree/BinaryTree.java).
