Search⌘ K

What is a Binary Tree?

Learn about binary trees, a fundamental tree data structure where each node has up to two children. Explore different types such as complete, full, and perfect binary trees, and understand their properties and node counts. This lesson prepares you to recognize binary tree structures and their applications in coding interviews using JavaScript.

Introduction #

A binary tree is a tree in which each node has between 0-2 children. They’re called the left and right children of the node. The figure below shows what a Binary Tree looks like.

%0 1 1 2 2 1->2 3 3 1->3 4 4 2->4 null 5 2->null

Types of Binary Trees

Complete Binary Trees

A complete binary tree is a binary tree in which all the levels of the tree are fully filled, except for perhaps the last level which can be filled from left to right. The total number of nodes in a complete binary tree can be expressed as:

2htotal number of nodes2(h+1)12^{h} \leq \text{total number of nodes} \leq 2^{(h+1)}-1 ...