Binary Tree in Data Structure
A binary tree is a hierarchical data structure of nodes where each node has at most two children, called the left child and the right child. It supports tree traversals, search trees, and heaps, and can be represented using linked nodes or arrays.
Theory & Explanation
Binary Tree Representation: Binary Search Tree, Strictly Binary Tree, Complete Binary Tree & Extended Binary Trees
1. Binary Tree Representation
A Binary Tree is a hierarchical data structure where each node has at most two children—a left child and a right child. In memory, a binary tree is represented in two primary ways:
- Linked (Pointer/Reference) Representation: Each node is dynamically allocated as a structure/object containing
data, aleftchild pointer, and arightchild pointer (NULL/Nonewhen absent). Flexible for arbitrary shapes and frequent insertions/deletions without shifting elements. - Sequential (Array) Representation: Stores tree nodes level by level in a 1D array. For 0-based indexing, a node at index
ihas its left child at2i + 1, right child at2i + 2, and parent at(i - 1) / 2(for 1-based indexing, left is2i, right is2i + 1, and parent isi / 2). Compact and fast for complete binary trees/heaps, though wasteful on sparse or skewed trees.
Graphical Representation: Linked vs. Array Representation of a Binary Tree
2. Special Types of Binary Trees
A. Binary Search Tree (BST)
A Binary Search Tree is a binary tree in which every node satisfies the BST ordering property: all values in its left subtree are strictly less than the node's key, and all values in its right subtree are strictly greater than the node's key (with no duplicate keys in standard definitions).
- Example: Root
50; left child30(with left20, right40); right child70(with left60, right80). An inorder traversal of a BST always produces keys in ascending order (20, 30, 40, 50, 60, 70, 80). - Complexity: Search, insertion, and deletion take
O(log n)average time for balanced trees, andO(n)worst time if skewed.
B. Strictly Binary Tree (Full / Proper Binary Tree)
A Strictly Binary Tree (also called a Full or Proper Binary Tree) is a binary tree in which every non-leaf (internal) node has exactly two children—that is, every node has either 0 or 2 children (no node has only one child).
- Example: Root
1with children2and3; node2has children4and5;3, 4, 5are leaves (each has 0 children, while1and2each have 2 children). - Exam Formula: If a strictly binary tree has
nleaf (external) nodes, it hasn - 1internal nodes and2n - 1total nodes.
C. Complete Binary Tree
A Complete Binary Tree is a binary tree in which all levels except possibly the last are completely filled, and all nodes in the last level are placed as far to the left as possible (no missing slots before the last node at the bottom level).
- Example: A tree with 5 nodes: root
1(level 0), children2and3(level 1, full), and children4and5under2(level 2, filled left-to-right). - Exam Note: Every perfect binary tree is complete, and every complete binary tree maps cleanly to a contiguous array without gaps; used as the underlying shape of binary heaps.
D. Extended Binary Trees (2-Tree)
An Extended Binary Tree (also called a 2-Tree) is obtained by taking any binary tree and replacing every NULL (empty) child link with a special dummy/external node (often drawn as a square or box), called an external node (or failure node), while original tree nodes become internal nodes.
- Example: Suppose node
3in a binary tree has no left or right child. In the extended binary tree, two external square nodes are attached as its left and right children so that every original node now has exactly two children. - Exam Formulas: If an extended binary tree has
Iinternal nodes andEexternal nodes, thenE = I + 1. Its Internal Path Length (IPL= sum of depths of all internal nodes) and External Path Length (EPL= sum of depths of all external nodes) satisfy:EPL = IPL + 2I.
Graphical Representation: BST, Strictly, Complete & Extended Binary Trees
e.g., 50 → L: 30, R: 70.
Inorder is sorted.
Leaves =
L → Total = 2L - 1.
Fits array indices 0..n-1.
NULL link gets a □ external node.E = I + 1, EPL = IPL + 2I.
3. C Program: Binary Search Tree (Insert & Inorder Traversal)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node left;
struct Node right;
};
struct Node createNode(int key) {
struct Node node = (struct Node)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
return node;
}
struct Node insertBST(struct Node root, int key) {
if (root == NULL) {
return createNode(key);
}
if (key < root->key) {
root->left = insertBST(root->left, key);
} else if (key > root->key) {
root->right = insertBST(root->right, key);
}
return root;
}
void inorder(struct Node root) {
if (root == NULL) return;
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
int main() {
struct Node *root = NULL;
int keys[] = {50, 30, 70, 20, 40, 60, 80};
int n = sizeof(keys) / sizeof(keys[0]);
for (int i = 0; i < n; i++) {
root = insertBST(root, keys[i]);
}
printf("BST Inorder Traversal (Sorted): ");
inorder(root);
printf("\n");
return 0;
}
Expected Output (C):
BST Inorder Traversal (Sorted): 20 30 40 50 60 70 80
4. Python Program: Binary Search Tree (Insert & Inorder Traversal)
class BSTNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert_bst(root, key):
if root is None:
return BSTNode(key)
if key < root.key:
root.left = insert_bst(root.left, key)
elif key > root.key:
root.right = insert_bst(root.right, key)
return root
def inorder(root):
if root is None:
return []
return inorder(root.left) + [root.key] + inorder(root.right)
if __name__ == "__main__":
keys = [50, 30, 70, 20, 40, 60, 80]
root = None
for k in keys:
root = insert_bst(root, k)
print("BST Inorder Traversal (Sorted):", inorder(root))
Expected Output (Python):
BST Inorder Traversal (Sorted): [20, 30, 40, 50, 60, 70, 80]
