Skip to main content

What is a binary tree?

A binary tree is a data structure in which each node can have at most two children:

  • a left child
  • a right child

Structure

javascript
A / \ B C / \ D E

Here:

  • A is the root,
  • B and C are children of A,
  • B has two children (D and E),
  • C is a leaf (no children).

Features

  • Each node has up to 2 edges (unlike a general tree, where it can have more).
  • Simplifies the implementation of search, sorting, and traversal.

Main types

  1. Complete - every level is filled except possibly the last, and the elements in it are arranged left to right.
  2. Perfect - every level is fully filled.
  3. Balanced - the height of the left and right subtrees differs by no more than 1.
  4. Binary search tree (BST) - the left subtree contains elements smaller than the parent, the right subtree contains larger ones.

Summary

A binary tree is a hierarchical structure in which each node is connected to at most two children, which makes it the basis for many search, sorting, and data-storage algorithms.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.