What is a Binary Tree?
A binary tree is one of the most fundamental and important structures in data structures. Just as building a house starts with laying the foundation, understanding binary trees will help you easily master more complex structures (such as heaps, red-black trees, etc.) later.
Imagine a binary tree as a “branching tree”—each “branching point” is a “node”, and each node can have at most two “child nodes”. The left child is called the “left child node”, and the right one is the “right child node”. Nodes with no child nodes are called “leaf nodes” (just like the leaves of a tree).
For example: There is an apple tree in your yard. Each apple can be seen as a “node”, and the branches connecting the apples to the trunk are “edges”. If the trunk is the “root node”, it may split into two branches (left and right). Each branch may have multiple apples (child nodes), but each branch can have at most two apples (since each node in a binary tree can have at most two child nodes).
Parts List of a Binary Tree: Basic Terminology¶
To draw a binary tree, you first need to know its “parts”:
- Node: The basic unit of the tree, which can store data (such as numbers, strings, etc.).
- Root Node: The “topmost” node of the tree, with no parent node, and the starting point of the tree.
- Leaf Node: A node with no child nodes, the “bottommost” of the tree.
- Child Nodes: Nodes that “grow” from a parent node to the next level (left child node or right child node).
- Parent Node: A node that has child nodes (e.g., the root node is the parent node of its “left child node”).
- Left Subtree/Right Subtree: The left child node of a node and all its descendants form the “left subtree”, and the right child node and all its descendants form the “right subtree”.
Drawing Binary Trees Step by Step: From Simple to Complex¶
Now, let’s use the “drawing method” to master the drawing of binary trees step by step. Follow the steps, and you’ll find it super simple!
Step 1: The Simplest Binary Tree—Only the Root Node¶
First, draw a “circle” to represent the root node, and write a number inside (e.g., 1). This tree has only the root node, no child nodes, so it is also a leaf node.
1
Step 2: Root Node Has Only a Left Child¶
Draw a small circle (left child node) slightly to the left directly below the root node, and write a number inside (e.g., 2). Temporarily do not draw anything on the right side of the root node (indicating no right child node).
1
/
2
Step 3: Root Node Has Only a Right Child¶
Draw a small circle (right child node) slightly to the right directly below the root node, and write a number inside (e.g., 2). Do not draw anything on the left side.
1
\
2
Step 4: Root Node Has Both Left and Right Child Nodes¶
Draw the left child node (e.g., 2) slightly to the left below the root node, and the right child node (e.g., 3) slightly to the right.
1
/ \
2 3
Step 5: Add Child Nodes to the Left Child¶
Take the tree from Step 2 as an example. The left child node 2 can continue to grow left child (4) and right child (5). At this time, the tree structure is:
1
/ \
2 3
/ \
4 5
Note here: Each node can have at most two child nodes—do not draw more. For example, if node 2 has three child nodes drawn below it, it is no longer a binary tree!
Step 6: Add Child Nodes to the Right Child¶
Continuing from Step 4, add left child (6) and right child (7) to the right child node 3:
1
/ \
2 3
/ \ / \
4 5 6 7
Step 7: Identify “Incorrect” Drawings That Are Not Binary Trees¶
Error Example 1: A node has three child nodes (e.g., the root node 1 has three branches below it):
1
/|\
2 3 4 // This is not a binary tree!
Error Example 2: Child node positions are messy (e.g., the right child of the left child is higher than the right child of the root node)—try to maintain levels when drawing: the root node is at the top, child nodes are on the next level, grandchildren are on the child’s next level, and so on.
Binary Tree “Authentication”: How to Determine If It’s a Binary Tree?¶
A structure is a binary tree if and only if two conditions are met:
- Each node can have at most 0, 1, or 2 child nodes (no more than 2).
- The positions of all child nodes are ordered (left child and right child are clearly distinguished; their order cannot be swapped without changing the tree structure).
Combining Binary Tree Traversal with Drawing (Simple Version)¶
Now that you know how to draw trees, how to reverse-engineer the tree structure from a “traversal result”? Here we briefly introduce three traversal methods (we’ll go deeper later):
- Pre-order Traversal (Root → Left → Right): Draw the root node first, then the left subtree, and finally the right subtree.
- In-order Traversal (Left → Root → Right): Draw the left subtree first, then the root node, and finally the right subtree.
- Post-order Traversal (Left → Right → Root): Draw the left subtree first, then the right subtree, and finally the root node.
Example: Suppose the pre-order traversal result is 1 2 4 5 3 6 7, how to draw the tree?
- The first element of pre-order is the root node (1), so draw 1 first.
- The remaining elements of pre-order are the left subtree (2 4 5) and the right subtree (3 6 7).
- The first element of the left subtree is 2, so 2 is the left child of 1.
- The remaining elements after 2 in the left subtree are 4 and 5. 4 is the left child of 2, and 5 is the right child of 2.
- The first element of the right subtree is 3, so 3 is the right child of 1.
- The remaining elements after 3 in the right subtree are 6 and 7. 6 is the left child of 3, and 7 is the right child of 3.
The final tree drawn is consistent with the example from Step 4 plus Step 5:
1
/ \
2 3
/ \ / \
4 5 6 7
Summary: Drawing Trees Is the “Golden Key” to Understanding Binary Trees¶
Drawing trees is the core of learning binary trees—through drawing, you can intuitively see the position of each node, the number of child nodes, and the hierarchical relationship. At first, it may seem difficult, but after drawing a few simple trees (e.g., only root, only left/right child, three-level tree), you’ll quickly master it.
Remember: Binary trees are the “foundation” for complex structures like heaps, binary search trees, and Huffman trees. The process of drawing trees not only helps you understand the basics but also lays the foundation for subsequent algorithms (such as sorting and searching).
Now, take a piece of paper and try to draw a binary tree that includes the root, left and right children, and grandchild nodes. Drawing one or two examples will reveal that binary trees are actually quite simple!