Data Structures · Binary Trees and Traversals
Introduction to Binary Trees and Traversing vs Searching
Overview
A binary tree is a tree in which every node has at most two children.
Working with such a tree requires two distinct operations: traversing, which visits every node to see all the information stored, and searching, which looks for one desired node and stops as soon as it is found.
Key concepts
- Binary tree: any tree in which every node has at most two children. A node may have 0, 1, or 2 children; it cannot have 3 or more.
- Traversing: visiting every node of the data structure to see the information present in every node, i.e. to look at everything in the structure. It is not stopped early.
- Searching: looking for one particular (desired) node; as soon as that desired element is found, the process stops.
- Applications: programs store information in a binary tree and later want to see what information is present in the tree; this act of visiting every node is traversing.
- Traversal methods: the most important and most popular binary-tree traversal methods are inorder, preorder, and postorder.
How it works
- Distinguish the goal: to see everything in the structure, traverse it; to locate one desired node, search for it.
- When traversing, visit every node and do not stop on finding one.
- When searching, stop as soon as the desired element is found.
- For binary trees, use one of the main traversal methods: inorder, preorder, or postorder.
- In queue operations, never insert when the queue is full (overflow condition) and never delete when there is no element (underflow condition); a gap is kept in the queue so that these overflow and underflow conditions can be understood.
Comparison
| Aspect | Traversing | Searching |
|---|---|---|
| Purpose | See all information present in the data structure | Find one particular desired node |
| Stopping rule | Visits every node (does not stop on finding one) | Stops as soon as the desired element is found |
| Coverage | Whole data structure | Only until the target is located |
Revision summary
- Binary tree = tree where each node has at most two children (0, 1, or 2; never 3 or more).
- Traversing = visit/see every node; searching = find a specific node and stop.
- The three most important binary-tree traversal methods: inorder, preorder, postorder.
- Overflow (insert when full) and underflow (delete when empty) conditions are why a gap is kept in the queue.
Previous-year exam insights
- For any binary tree, if n0 = number of leaf nodes (degree 0), n1 = nodes with one child, n2 = nodes with two children, then n0 = n2 + 1 and total nodes n = n0 + n1 + n2 = 2n2 + n1 + 1.
Asked inGATE 2025GATE 2006GATE 2010 - For a full/complete k-ary tree (every internal node has exactly k children), if I is the number of internal nodes and L the number of leaves, then L = (k-1)I + 1, equivalently total nodes = kI + 1.
Asked inGATE 2007GATE 2005GATE 1998GATE 2002 - A binary tree of height h (height = number of edges on the longest root-to-leaf path) has at most nodes and at least h + 1 nodes;
it has at most leaves and at least 1 leaf.
Asked inGATE 2024GATE 2007 - A binary tree can be uniquely reconstructed from its inorder traversal together with either its preorder or its postorder traversal; preorder plus postorder alone is not sufficient for a general binary tree.
Asked inGATE 1998 - In a tree with n nodes there are exactly n - 1 edges; a rooted tree with n nodes has n - 1 edges regardless of the number of children per node.
Asked inGATE 1998 - A full binary tree with L leaves has exactly L - 1 internal nodes and 2L - 1 total nodes; the distance between two leaves equals depth(a) + depth(b) - 2*depth(LCA(a,b)).
Asked inGATE 2019