Skip to content

binary tree

A binary tree is a hierarchical data structure in which each node has at most two children, conventionally called the left child and the right child. One node is the root, and every other node descends from it through exactly one path, so the structure branches downward without cycles, like this:

A binary tree of circular nodes spanning four levels, each level a single color, from the root at the top down to the leaves at the bottom.
Each Node Branches to at Most Two Children

A few measures describe a binary tree’s shape. The depth of a node is its distance from the root, and the height of the tree is the depth of its deepest node. A tree that stays balanced keeps its height near log n for n nodes, which is what lets operations on it run quickly.

Common varieties include the binary search tree, which orders nodes so that a lookup can discard half the remaining nodes at each step, and the heap, which keeps the largest or smallest value at the root. Binary trees power tasks from expression parsing to priority queues.

A binary tree is a restricted kind of graph, one that is connected and acyclic. Traversing it is a natural use of recursion, since each subtree is itself a smaller binary tree.

The Python heapq Module: Using Heaps and Priority Queues

Tutorial

The Python heapq Module: Using Heaps and Priority Queues

In this step-by-step tutorial, you'll explore the heap and priority queue data structures. You'll learn what kinds of problems heaps and priority queues are useful for and how you can use the Python heapq module to solve them.

intermediate data-structures python stdlib

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Sept. 26, 2026 • Reviewed by Leodanis Pozo Ramos