Binary trees are fundamental hierarchical data structures crucial in computer science for organizing and storing data efficiently. This topic explores their basic definitions, properties, common traversal methods, and introduces specialized binary tree variants like Binary Search Trees, AVL Trees, and Heaps. Understanding binary trees is key to mastering more complex data structures and algorithms.
A binary tree, denoted as , is a hierarchical data structure defined recursively. It is either empty, or it consists of:
Crucially, the children of any node in a binary tree are ordered into a left child and a right child. A node can have:
Here's a simple visual representation of a binary tree:
A (Root)
/ \
B C
/ / \
D E FBinary tree traversal refers to the process of visiting each node in the tree exactly once in a systematic way. There are three common methods, each defined by the order in which the root, left subtree, and right subtree are processed.
Consider the following binary tree for our examples:
10
/ \
5 15
/ \ \
3 7 20In Preorder traversal, the policy is to visit the Root node first, then recursively traverse its Left subtree, and finally recursively traverse its Right subtree.
For the example tree above, Preorder traversal would yield:
In Inorder traversal, the policy is to recursively traverse the Left subtree first, then visit the Root node, and finally recursively traverse its Right subtree.
For the example tree above, Inorder traversal would yield: Note: For a Binary Search Tree, Inorder traversal always produces elements in non-decreasing order.
In Postorder traversal, the policy is to recursively traverse the Left subtree first, then recursively traverse its Right subtree, and finally visit the Root node.
For the example tree above, Postorder traversal would yield:
#include <iostream>
// Define a structure for a tree node
struct Node {
int data;
Node* left;
Node* right;
// Constructor to create a new node
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
// Function for Preorder Traversal (Root -> Left -> Right)
void preorderTraversal(Node* node) {
if (node == nullptr) {
return;
}
std::cout << node->data << " "; // 1. Visit root
preorderTraversal(node->left); // 2. Traverse left subtree
A Binary Search Tree (BST) is a specialized type of binary tree that maintains a specific ordering property to facilitate efficient search, insertion, and deletion operations.
The key property of a BST is:
Here's an example of a BST:
8
/ \
3 10
/ \ \
1 6 14
/ /
4 13Common operations performed on BSTs include:
An AVL tree is a specialized type of Binary Search Tree that is self-balancing. This means it automatically adjusts its structure (through rotations) after insertions or deletions to maintain a roughly balanced height, ensuring that operations like search, insertion, and deletion remain efficient with time complexity.
The defining characteristic of an AVL tree is its balance factor. For any node in an AVL tree, the difference between the height of its left subtree and the height of its right subtree cannot be more than one.
The Balance Factor (BF) for a node is calculated as:
For a tree to be considered balanced (an AVL tree), every node in the tree must satisfy the following condition:
If the balance factor of any node falls outside this range (i.e., less than -1 or greater than 1), the tree is unbalanced at that node, and a series of rotations (single or double) are performed to restore the AVL property. An empty tree's height is typically considered -1.
Consider the example from the source (where 0, 1, 2 refer to node heights, and -1 an empty subtree):
Tree A (AVL) Tree B (AVL)
2 (BF=1-0=1) 2 (BF=1-0=1)
/ \ / \
1 0 1 0
/ \ / \
0 0 0 0
/ \ / \
-1 -1 -1 -1In 'Tree A', the root node (height 2) has a left child (height 1) and a right child (height 0). The balance factor of the root is , which is within . All other nodes similarly satisfy the balance factor constraint, making it a valid AVL tree.
A heap is a complete binary tree that satisfies the heap property. Unlike a Binary Search Tree, a heap does not maintain a sorted order across the entire tree, but rather a specific ordering relationship between parent and child nodes. There are two main types of heaps:
The source material implies a Min-Heap based on its examples ("The value in the root is the smallest of the tree", "Node value < child value").
A crucial characteristic of a heap is that it must be a complete binary tree. A complete binary tree is a binary tree in which all levels are completely filled, except possibly the last level, and all nodes in the last level are as far left as possible. This property makes heaps highly efficient for array-based implementations.
Example of a Min-Heap:
1
/ \
2 4
/ \ / \
5 7 8 11Insert an Item:
Example: Insert 1 into a heap
2 2 1
/ \ / \ / \
4 3 4 3 2 3
/ \ / \ / \
5 7 5 7 4 7
/ / /
1 (Add as leaf) 1 (Swap up) 5 (Swap up)Remove an Item (Min element for Min-Heap):
Example: Remove 1 from a heap
1 7 2
/ \ / \ / \
2 3 2 3 4 3
/ \ / \ / \ / / \ /
4 7 8 11 4 8 11 5 8 11
Because a heap is a complete binary tree, it can be efficiently represented using an array without explicit pointers for children, as the positions of children and parents can be calculated mathematically.
This array-based representation simplifies memory management, improves cache performance due to contiguous memory allocation, and is widely used.
Tree:
1
/ \
2 4
/ \ / \
5 7 8 11
Array Representation:
[ 1 | 2 | 4 | 5 | 7 | 8 | 11 ]
0 1 2 3 4 5 6A hierarchical data structure where each node has at most two children, referred to as the left child and the right child. It is recursively defined as either empty or having a root, left subtree, and right subtree.
Systematic methods (Preorder, Inorder, Postorder) to visit every node in a tree exactly once, each following a specific pattern for processing the root, left subtree, and right subtree.
A special type of binary tree where for every node, all values in its left subtree are strictly less than its value, and all values in its right subtree are strictly greater, enabling efficient search and ordered retrieval.
A self-balancing Binary Search Tree where for every node, the difference in heights between its left and right subtrees (balance factor) is at most one. This property guarantees $O(\log n)$ time complexity for fundamental operations.
A complete binary tree that satisfies the heap property: for a min-heap, the value of each node is less than or equal to the values of its children, ensuring the smallest element is always at the root. Max-heaps have the opposite property.
Test your understanding with 5 questions
Which of the following best defines a binary tree?
What is the correct order of visiting nodes for an Inorder traversal?
In a Binary Search Tree (BST), what property must hold for any given node?
What is the allowed range for the balance factor of any node in an AVL tree?
Which of the following is a characteristic of a Min-Heap?
6 Modules
6 Modules