Data Structures13 min read

Binary Trees: Hierarchical Data Structures

Master binary trees - one of the most important data structures. Learn tree traversal, binary search trees, and when to use trees. Essential for understanding databases, file systems, and many algorithms.

Robert Kim
December 18, 2025
0.0k0

Trees are everywhere in computer science - file systems, databases, decision trees, and more. Binary trees are the simplest and most important type. Understanding them is crucial for many algorithms.

What is a Binary Tree?

A binary tree is a tree where each node has at most two children (left and right). It's hierarchical - perfect for representing relationships, decisions, or sorted data.

Tree Traversal

There are three main ways to traverse a tree: in-order (left, root, right), pre-order (root, left, right), and post-order (left, right, root). Each has different uses. I'll show you when to use each.

Binary Search Trees

BSTs are binary trees where left child < parent < right child. This makes searching, insertion, and deletion efficient (O(log n) in balanced trees). They're the foundation of many data structures.

Real-World Applications

Trees are used in file systems, databases (B-trees), expression parsing, decision trees in ML, and hierarchical data. Understanding trees helps you understand these systems.

Implementation

I'll walk you through building a binary tree and BST from scratch. You'll implement insertion, search, deletion, and traversal. This hands-on experience makes everything clear.

#Data Structures#Binary Trees#BST#Tree Traversal

Common Questions & Answers

Q1

What is a binary tree?

A

A binary tree is a tree data structure where each node has at most two children (left and right). It's hierarchical and used for representing relationships, sorted data (BST), or decision processes. Common operations include insertion, deletion, search, and traversal (in-order, pre-order, post-order).

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

class BinaryTree {
  constructor() {
    this.root = null;
  }
  
  // Insert (for BST)
  insert(value) {
    const node = new TreeNode(value);
    if (!this.root) {
      this.root = node;
      return;
    }
    
    let current = this.root;
    while (true) {
      if (value < current.value) {
        if (!current.left) {
          current.left = node;
          return;
        }
        current = current.left;
      } else {
        if (!current.right) {
          current.right = node;
          return;
        }
        current = current.right;
      }
    }
  }
  
  // In-order traversal
  inOrder(node = this.root) {
    if (node) {
      this.inOrder(node.left);
      console.log(node.value);
      this.inOrder(node.right);
    }
  }
}
Q2

What is a Binary Search Tree (BST)?

A

A BST is a binary tree where for each node, all values in left subtree are less than the node, and all values in right subtree are greater. This property enables efficient search (O(log n) in balanced tree), insertion, and deletion. Used in databases and sorted data structures.

javascript
class BST {
  constructor() {
    this.root = null;
  }
  
  // Search - O(log n) in balanced tree
  search(value) {
    let current = this.root;
    while (current) {
      if (value === current.value) return current;
      if (value < current.value) {
        current = current.left;
      } else {
        current = current.right;
      }
    }
    return null;
  }
  
  // Insert - O(log n)
  insert(value) {
    // Implementation from previous example
  }
  
  // Find minimum value
  findMin(node = this.root) {
    while (node.left) {
      node = node.left;
    }
    return node.value;
  }
}