Data Structures

What is Binary Tree?

A binary tree is a hierarchical data structure made of nodes, where each node holds a value and links to at most two children, called left and right. One node — the root — sits at the top, and every other node descends from it. Trees model hierarchy and enable fast ordered search.

Time Traversal visits every node once, so it is O(n). BST search, insert, and delete are O(h), where h is the tree's height.Space O(n) to store n nodes. A traversal uses O(h) call-stack or queue space, ranging from O(log n) when balanced to O(n) when skewed.Topic Data Structures

The shape: nodes, children, and leaves

A binary tree is a set of nodes connected top-down. One node is the root. Each node stores a value and has up to two links — a left child and a right child — either of which can be empty. Nodes with no children are called leaves. Think of a family tree, a tournament bracket, or the folders on your computer: structure that branches downward.

A few terms recur everywhere. A node's parent is the node directly above it, and its subtree is itself plus everything beneath it. Depth is how many edges separate a node from the root; height is the number of edges on the longest path from a node down to a leaf. These two numbers largely determine how fast tree operations run.

Traversals: four ways to visit every node

To do anything useful you have to walk the tree. Three depth-first orders differ only in when you handle the current node relative to its children: preorder (node, then left, then right), inorder (left, node, right), and postorder (left, right, node). Each is a three-line recursion built around the same base case: an empty node does nothing.

The fourth order, level-order (breadth-first), visits the tree row by row using a queue. The order you choose isn't arbitrary — inorder on a BST emits values in sorted order, preorder is handy for copying or serializing a tree, postorder suits freeing nodes or evaluating expression trees, and level-order handles anything 'closest first.' Watching the pointer move through each order step by step is usually the moment it clicks.

Binary search trees, height, and balance

A binary search tree (BST) adds one rule: for every node, all values in its left subtree are smaller and all values in its right subtree are larger. That invariant lets you search like a phone book — compare, then go left or right — discarding half the remaining nodes at each step.

Because you follow a single root-to-leaf path, search, insert, and delete cost O(h), where h is the height. A balanced tree keeps h near log n, giving O(log n). But insert values in already-sorted order and the tree degenerates into a straight chain: h = n, and every operation slows to O(n). Self-balancing variants like AVL and red-black trees rearrange nodes as you go to guarantee logarithmic height.

pythonA binary tree node plus an inorder traversal and a height helper (Python).
class Node:
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

def inorder(node):
    """Left, node, right — yields BST values in sorted order."""
    if node is None:
        return []
    return inorder(node.left) + [node.value] + inorder(node.right)

def height(node):
    """Edges on the longest root-to-leaf path; empty tree is -1."""
    if node is None:
        return -1
    return 1 + max(height(node.left), height(node.right))
Time complexityTraversal visits every node once, so it is O(n). BST search, insert, and delete are O(h), where h is the tree's height.
Space complexityO(n) to store n nodes. A traversal uses O(h) call-stack or queue space, ranging from O(log n) when balanced to O(n) when skewed.

In a balanced tree h ≈ log n, so BST operations are O(log n) on average. A degenerate tree — for example inserting sorted values — collapses into a chain, making h = n and operations O(n) in the worst case.

When to use it

  • When your data is naturally hierarchical — file systems, org charts, the HTML DOM, or decision trees.
  • When you need ordered keys with fast search, insert, and delete together, as in the map or set behind many standard libraries (a balanced BST).
  • When parsing or evaluating structured input, such as expression trees or abstract syntax trees.
  • When you want sorted iteration or range queries for free — an inorder walk of a BST returns keys in ascending order.

Watch out for

  • Confusing a binary tree with a binary search tree. A plain binary tree has no ordering guarantee; only a BST maintains left < node < right, and only then does search become logarithmic.
  • Assuming O(log n) always holds. An unbalanced BST — classically from inserting already-sorted data — degrades into a linked list with O(n) operations. Reach for a self-balancing tree when you need guarantees.
  • Forgetting the empty-node base case in recursion, or mixing up height and depth. Deep recursion on a skewed tree can also overflow the call stack.
In the interview

Trees are one of the most heavily tested interview topics, and most questions are variations on a few patterns: implement the traversals both recursively and iteratively, print a tree level by level with a queue, validate that a tree is a BST, compute height or diameter, find the lowest common ancestor, or check whether a tree is balanced or symmetric. Interviewers are watching for clean recursion with a correct base case, the instinct to reach for a stack or queue when an iterative solution is asked for, and an honest complexity analysis that distinguishes the balanced case from the O(n) skewed worst case. Being able to explain your recursive structure out loud matters as much as the final code.

Binary Tree: frequently asked questions

Is a binary tree hard to learn?

The structure itself is simple — just nodes with up to two children. The real hump is recursion, since most tree operations call themselves on the left and right subtrees. Once traversals click, the majority of tree problems follow the same recursive shape, and stepping through an animation of that recursion makes it far less abstract.

What is the difference between a binary tree and a binary search tree?

A binary tree only limits each node to at most two children; the values can be in any arrangement. A binary search tree adds an ordering rule — every value in the left subtree is smaller and every value in the right subtree is larger — which is what enables O(log n) search when the tree is balanced.

What is the time complexity of searching a binary tree?

In a plain binary tree with no ordering, search is O(n) because you may have to check every node. In a balanced binary search tree it drops to O(log n), since each comparison discards half the remaining nodes. An unbalanced BST falls back to O(n) in the worst case.

Which traversal gives sorted order?

Inorder traversal (left, node, right) of a binary search tree visits the nodes in ascending sorted order. This is a direct consequence of the BST ordering rule, and it is why inorder is the default choice when you want a sorted view of the keys.

See Binary Tree as an animated story

Reading the definition is one thing — watching binary tree run line by line, then explaining it to an AI interviewer, is how it actually sticks. That is what CodeStory does.