Skip to content

Understanding Binary Tree and Its Usefulness

Published: at 01:58 PM

In software development, data structures play a crucial role in how efficiently we can store, access, and manage data. One of the lesser-known but widely used structures is the binary tree. While many developers may not interact with it directly, binary trees are fundamental in real-life applications such as SQL databases.

Table of contents

Open Table of contents

What is a Binary Tree?

A binary tree is a tree data structure where each node has at most two children: a left and a right child. Each node stores a value, and its children are also nodes of the binary tree. The topmost node is called the root, and nodes without children are called leaves.

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree where each node follows a specific order:

This structure allows for efficient searching, insertion, and deletion of values. A typical operation like searching in a BST has a time complexity of O(log n), making it much faster than linear search methods.

Real-life Applications of Binary Trees

One common use of binary trees is in SQL databases. When querying data, databases often use binary search algorithms to optimize retrieval. Data is structured in a way that allows quick searching by following the rules of a binary search tree. This reduces the time complexity for operations, resulting in faster queries and better performance.

We may not always notice it, but binary trees are used every day in systems like file directories, decision trees for AI, and compilers.

Example in JavaScript

Here’s a simple implementation of a binary search tree in JavaScript:

class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

class BinarySearchTree {
  constructor() {
    this.root = null;
  }

  // Insert a node into the tree
  insert(value) {
    const newNode = new Node(value);
    if (!this.root) {
      this.root = newNode;
      return;
    }

    let currentNode = this.root;
    while (true) {
      if (value < currentNode.value) {
        if (!currentNode.left) {
          currentNode.left = newNode;
          return;
        }
        currentNode = currentNode.left;
      } else {
        if (!currentNode.right) {
          currentNode.right = newNode;
          return;
        }
        currentNode = currentNode.right;
      }
    }
  }

  // Search for a value in the tree
  search(value) {
    let currentNode = this.root;
    while (currentNode) {
      if (value === currentNode.value) {
        return true;
      }
      currentNode =
        value < currentNode.value ? currentNode.left : currentNode.right;
    }
    return false;
  }
}

// Example usage
const bst = new BinarySearchTree();
bst.insert(10);
bst.insert(5);
bst.insert(15);
console.log(bst.search(5)); // true
console.log(bst.search(20)); // false

This simple BST class allows you to insert values into the tree and search for values in O(log n) time.

Conclusion

Understanding binary trees, especially binary search trees, is vital for efficient algorithm design. From SQL databases to file systems , binary trees help optimize many real-life applications by reducing the time it takes to search or manage data. Even if you don’t write code with binary trees often, you’re probably using their benefits every day.