• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

An Easy to Grasp Explanation of the Tree Data Structure

laptop infographic online business 6087062

Introduction

A tree consists of nodes connected by edges in a hierarchical way. It has a root node at the top, with subsequent branches leading to child nodes. Trees are widely used in computer science and can be found in applications like file systems, databases, and AI search algorithms.

Basic Terminology

  • Node: A fundamental part of a tree that holds data.
  • Root: The topmost node in a tree, with no parent.
  • Parent: A node that has one or more child nodes.
  • Child: A node that is a descendant of another node.
  • Leaf: A node that has no children.
  • Edge: A connection between two nodes.

Tree Data Structure Types

  1. Binary Tree: A tree where each node has at most two children (left and right).
  2. Binary Search Tree (BST): A binary tree with the following property: the left child is less than the parent node, and the right child is greater.
  3. AVL Tree: A self-balancing binary search tree where the left and right subtrees differ in height by at most one.
  4. Heap: A special tree-based data structure that satisfies the heap property, used in implementing priority queues.

Binary Tree

A binary tree is one of the most common types of trees. In a binary tree, each node has at most two children, often referred to as the left child and the right child. Here’s an illustration of a simple binary tree:

markdownCopiaModifica        10
       /  \
      5    20
     / \   / \
    3   8 15  25

In this tree:

  • The root is 10.
  • The left child of 10 is 5, and the right child is 20.
  • The leaf nodes are 3, 8, 15, and 25.

Binary Tree Operations

Common operations in a binary tree include:

  • Insertion: Adding a node to the tree.
  • Traversal: Visiting all nodes in a specific order (pre-order, in-order, post-order).
  • Searching: Finding a node with a specific value.
  • Deletion: Removing a node from the tree.

Binary Search Tree (BST)

A Binary Search Tree is a type of binary tree having the left child smaller than the parent node, and the right child greater. This property makes searching for elements more efficient. Here’s an example of a Binary Search Tree:

markdownCopiaModifica        10
       /  \
      5    15
     / \   / \
    3   7 12  20

In this BST:

  • All nodes in the left subtree of 10 have values less than 10.
  • All nodes in the right subtree of 10 have values greater than 10.

Operations on a Binary Search Tree

  1. Search: Search for a node by comparing the search value with the node values.
  2. Insertion: Insert a new node by traversing the tree according to the binary search property.
  3. Deletion: Deleting a node can be tricky, as it depends on whether the node has no children, one child, or two children.

Java Code Example for Binary Search Tree

Here’s a simple Java implementation of a Binary Search Tree (BST) with the basic operations of insertion and search:

public class Tree {

    class TreeNode {
        int value;
        TreeNode left, right;

        TreeNode(int value) {
            this.value = value;
            left = right = null;
        }
    }

    private TreeNode root;

    public Tree() {
        root = null;
    }

    // Add a new node to the tree
    public void add(int value) {
        root = addRecursively(root, value);
    }

    // Helper method for recursive node addition
    private TreeNode addRecursively(TreeNode node, int nodeVal) {
        if (node == null) {
            return new TreeNode(nodeVal);
        }
        if (nodeVal < node.value) {
            node.left = addRecursively(node.left, nodeVal);
        } else if (nodeVal > node.value) {
            node.right = addRecursively(node.right, nodeVal);
        }
        return node;
    }

    // Check if a value exists in the tree
    public boolean contains(int value) {
        return containsRecursively(root, value);
    }

    // Helper method for recursive search
    private boolean containsRecursively(TreeNode node, int nodeVAl) {
        if (node == null) {
            return false;
        }
        if (nodeVAl == node.value) {
            return true;
        }
        if (nodeVAl < node.value) {
            return containsRecursively(node.left, nodeVAl);
        }
        return containsRecursively(node.right, nodeVAl);
    }

    // Perform an in-order traversal and print the values
    public void traverseInOrder() {
        inOrderRecursively(root);
    }

    private void inOrderRecursively(TreeNode node) {
        if (node != null) {
            inOrderRecursively(node.left);
            System.out.print(node.value + " ");
            inOrderRecursively(node.right);
        }
    }

    public static void main(String[] args) {
        Tree binaryTree = new Tree();
        binaryTree.add(10);
        binaryTree.add(5);
        binaryTree.add(15);
        binaryTree.add(3);
        binaryTree.add(7);

        System.out.println("In-order traversal:");
        binaryTree.traverseInOrder(); // Expected output: 3 5 7 10 15

        System.out.println("\nDoes the tree contain 7? " + binaryTree.contains(7)); // Expected output: true
    }
}

Explanation of the Code:

  • Node Class: Represents a node in the tree, storing data and references to the left and right child nodes.
  • Insert Function: Inserts a new node into the tree by recursively finding the correct position based on the value.
  • Search Function: Searches for a node in the tree by comparing values with the current node.
  • In-order Traversal: Prints the nodes of the tree in ascending order by recursively visiting the left subtree, the root, and the right subtree.

AVL Tree (Self-Balancing Binary Search Tree)

An AVL Tree is a self-balancing binary search tree where the difference between the heights of left and right subtrees cannot be more than one. When the balance factor of any node is violated, rotations are used to restore balance.

Balancing the AVL Tree

The balance factor is calculated as the difference in height between the left and right subtrees:

  • We can define the Balance Factor as: Height(Left Subtree) – Height(Right Subtree)

If the balance factor of any node becomes +2 or -2, the tree is rebalanced making use of rotations.

Types of Rotations:

  1. Right Rotation: Used when the left subtree is too tall.
  2. Left Rotation: Used when the right subtree is too tall.
  3. Left-Right Rotation: A combination of left and right rotations.
  4. Right-Left Rotation: A combination of right and left rotations.

Conclusion

The tree data structure is an essential concept in computer science that is used in many different areas, including file systems, databases, and algorithms. From simple binary trees to advanced structures like AVL trees, trees provide efficient solutions for organizing and searching data. Understanding how trees work and how to implement operations like insertion, search, and traversal is crucial for any developer.

Trees help in optimizing performance, especially when dealing with large datasets, by reducing the time complexity of search and insertion operations. As you explore tree data structures further, you’ll encounter more specialized variants like Red-Black Trees, B-Trees, and others that offer additional features and optimizations.

You can find the example code on Github.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions