How to implement a binary tree?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. Binary trees are used in a variety of applications, including sorting and searching algorithms, expression parsers, and more. This article explains how to implement a binary tree step-by-step, utilizing examples and detailed explanations.
Binary Tree Structure
Before diving into the implementation, let's discuss the structure of a binary tree. Each node in a binary tree contains three main components:
- Data: This is the value stored in the node.
- Left Child: A reference to the left subtree.
- Right Child: A reference to the right subtree.
Here's an illustration of a basic node class in Python:
Building the Binary Tree
To construct a binary tree, we initiate a root node and then add more nodes based on specific conditions or algorithms. Here's a simple binary tree class:
Key Points for Insertion
- Root Node: If the root is
None, then the new node becomes the root. - Left Child: If the new data is less than the current node's data, traverse to the left.
- Right Child: If the new data is greater than or equal to the current node's data, traverse to the right.
Traversing the Binary Tree
Tree traversal algorithms are crucial for accessing and manipulating the data stored in a binary tree. The common methods include:
- In-order Traversal: Left -> Root -> Right
- Pre-order Traversal: Root -> Left -> Right
- Post-order Traversal: Left -> Right -> Root
- Level-order Traversal: Visit nodes level by level
Here’s how you could implement in-order traversal:
Tree Operations
Searching
Searching in a binary tree follows a similar pattern to insertion:
Deletion
Deleting a node from a binary tree is a bit more complex as it requires handling three cases:
- Node is a Leaf: Simply remove it.
- Node has one Child: Remove the node and replace it with its child.
- Node has Two Children: Replace the node with its in-order successor or predecessor, then delete the successor/predecessor.
Here's a basic representation of deleting a node:
Summary Table
Here's a table that summarizes the operations discussed along with their functionalities and implementations:
| Operation | Description | Implementation Approach |
| Insert | Adds data to the tree | Compare with current node, recurse left/right |
| In-order Traversal | Visits all nodes in ascending order | Recurse: Left, Root, Right |
| Search | Finds a node with a specific value | Compare with current node, recurse |
| Delete | Removes a node and restructures the tree as needed | Handle leaf, single child, and two children cases |
| Find Minimum | Finds the smallest value node | Traverse left until None |
Conclusion
Binary trees are fundamental in computer science for organizing data hierarchically. Implementing a binary tree involves understanding its structure and being able to perform essential operations like insertion, traversal, searching, and deletion. Mastering these operations provides a deep understanding of tree-based algorithms, which are widely used in numerous applications.
Related reading
- How to implement a Digg-like algorithm?
- How to implement a double linked list with only one pointer?
- How to implement a Least Frequently Used LFU cache?
- How to implement a Median-heap
- How to implement a Map with multiple keys?
- How to implement a queue with three stacks?
- How to implement a repeating shuffle that's random - but not too random
- How to implement a tree data-structure in Java?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.