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 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Python Stacks, Queues, and Priority Queues in Practice (Tutorial)
- Common Python Data Structures (Guide) (Tutorial)
- Thinking Recursively in Python (Tutorial)
- Stacks and Queues: Selecting the Ideal Data Structure (Course)
- Recursion in Python: An Introduction (Tutorial)
- Recursion in Python (Course)
- How to Do a Binary Search in Python (Tutorial)
- Python Stacks, Queues, and Priority Queues in Practice (Quiz)
- Records and Sets: Selecting the Ideal Data Structure (Course)
- Dictionaries and Arrays: Selecting the Ideal Data Structure (Course)
- Common Python Data Structures (Guide) (Quiz)
- Thinking Recursively With Python (Course)
- Thinking Recursively in Python (Quiz)
- Recursion in Python: An Introduction (Quiz)
- Creating a Binary Search in Python (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.