Binary Tree
A Binary Tree is one of the most important non-linear data structures in computer science. It is called a “binary” tree because each node in the tree can have a maximum of two children only.
These two children are specifically known as:
- Left Child
- Right Child
Unlike linear data structures such as arrays and linked lists where elements are arranged sequentially, binary trees organize data hierarchically.
Binary trees are widely used in:
- Searching algorithms
- Expression evaluation
- Database indexing
- Operating systems
- Artificial intelligence
- Decision-making systems
- Compiler design
Many advanced data structures are built using binary trees, such as:
- Binary Search Tree (BST)
- AVL Tree
- Heap
- Expression Tree
- Huffman Tree
Formal Definition of Binary Tree
A Binary Tree is defined as:
A finite collection of nodes which is either:
- Empty, OR
- Consists of:
- A root node
- A left subtree
- A right subtree
Both left and right subtrees are themselves binary trees.
This recursive definition is very important because every smaller subtree inside a binary tree also behaves like a binary tree.
Structure of a Binary Tree
Each node in a binary tree contains three parts:
- Data field
- Pointer/reference to left child
- Pointer/reference to right child
Example:
A
/ \
B C
/ \ \
D E FExplanation:
Ais the root node.Bis the left child ofA.Cis the right child ofA.DandEare children ofB.Fis the right child ofC.
Important Characteristics of Binary Tree
1. Maximum Two Children
Each node can have:
- Zero child
- One child
- Two children
But no node can have more than two children.
2. Ordered Structure
The left child and right child are treated differently.
Example:
A
/ \
B Cis different from:
A
/ \
C Bbecause positions matter.
3. Recursive Nature
Every subtree of a binary tree is also a binary tree.
Example:
A
/ \
B C
/ \
D EThe subtree rooted at B is itself another binary tree.
4. Hierarchical Structure
Binary trees represent parent-child relationships naturally.
Binary Tree Terminology
Understanding terminology is very important for solving numerical and theoretical questions.
1. Root Node
The topmost node of the binary tree is called the root node.
Example:
A
/ \
B CHere: A = Root Node
Properties:
- A binary tree can have only one root.
- The root has no parent.
2. Parent Node
A node that has child nodes is called a parent node.
Example:
A
/ \
B CHere:
Ais the parent ofBandC.
3. Child Node
A node directly connected below another node is called a child node.
Example:
BandCare children ofA.
4. Sibling Nodes
Nodes having the same parent are called sibling nodes.
Example:
A
/ \
B CB and C are siblings because they share the same parent.
5. Leaf Node
A node having no children is called a leaf node.
Example:
A
/ \
B C
/ \
D ELeaf nodes are: D, E, C
Leaf nodes are also called terminal nodes.
6. Internal Node
A node having at least one child is called an internal node.
Example: A and B
7. Edge
The connection between two nodes is called an edge.
Example: A → B
A binary tree with n nodes always contains: n - 1 edges
Reason:
- Root node does not need any edge.
- Every additional node requires one edge to connect to the tree.
8. Degree of a Node
The number of children of a node is called its degree.
Example:
A
/ \
B C- Degree of
A= 2 - Degree of
B= 0 - Degree of
C= 0
Maximum degree in a binary tree is always 2.
9. Depth of a Node
Depth is the distance from the root node to a particular node.
Example:
A
/ \
B C
/
DDepths:
- Depth of
A= 0 - Depth of
B= 1 - Depth of
D= 2
10. Level of a Node
Level indicates the layer number of a node.
Example:
| Node | Level |
|---|---|
| A | 0 |
| B, C | 1 |
| D | 2 |
11. Height of Tree
Height is the number of edges in the longest path from root to leaf node.
Example:
A
/ \
B C
/
DLongest path: A → B → D
Height = 2
12. Subtree
A smaller tree formed from a node and its descendants is called a subtree.
Example:
B
/ \
D Eis a subtree.
Types of Binary Trees
Different binary trees are designed for different purposes.
1. Strictly Binary Tree
A binary tree where every node has either:
- 0 children
OR - 2 children
No node can have only one child.
Example:
A
/ \
B C
/ \
D EValid Strictly Binary Tree.
Invalid Example:
A
/
Bbecause node A has only one child.
2. Full Binary Tree
A Full Binary Tree is often used interchangeably with Strictly Binary Tree.
Properties:
- Every node has either 0 or 2 children.
Example:
A
/ \
B C
/ \ / \
D E F G3. Perfect Binary Tree
A Perfect Binary Tree is a full binary tree where:
- All internal nodes have exactly two children.
- All leaf nodes are at the same level.
Example:
A
/ \
B C
/ \ / \
D E F GProperties:
- Total nodes = 2^(h+1) - 1
- Leaf nodes = 2^h
where h is the height.
4. Complete Binary Tree
A Complete Binary Tree is a binary tree where:
- All levels are fully filled except possibly the last level.
- Last level nodes are filled from left to right.
Example:
A
/ \
B C
/ \ /
D E FThis is complete because nodes are filled left to right.
5. Degenerate or Skewed Binary Tree
A tree where each parent has only one child.
Example:
A
\
B
\
CThis structure behaves similarly to a linked list.
Types:
- Left Skewed Tree
- Right Skewed Tree
Binary Tree Operations
Several important operations are performed on binary trees.
1. Insertion Operation
Insertion means adding a new node into the binary tree.
In a general binary tree, insertion usually follows: Level-order insertion
This means:
- Fill nodes level by level
- From left to right
Example:
Initial tree:
1
/ \
2 3Insert 4
Step-by-step:
- Left child of
2is empty. - Insert
4there.
Result:
1
/ \
2 3
/
42. Traversal Operation
Traversal means visiting all nodes of the tree.
There are four major traversal methods.
A. Inorder Traversal
Order: Left → Root → Right
Example:
A
/ \
B C
/ \
D ETraversal: D → B → E → A → C
Important:
- In BST, inorder traversal gives sorted order.
B. Preorder Traversal
Order: Root → Left → Right
Traversal: A → B → D → E → C
Used for:
- Copying trees
- Expression trees
C. Postorder Traversal
Order: Left → Right → Root
Traversal: D → E → B → C → A
Used in:
- Deletion operations
- Expression evaluation
D. Level-order Traversal
Traversal level by level using queue.
Example: A → B → C → D → E
3. Search Operation
Searching means finding whether a value exists inside the tree.
Approaches:
- DFS Traversal
- BFS Traversal
In ordinary binary trees:
- Entire tree may need traversal.
In BST:
- Searching becomes faster.
4. Deletion Operation
Deletion removes a node from the tree.
Case 1: Deleting Leaf Node
Simply remove the node.
Example:
A
/
BDelete B.
Case 2: Node with One Child
Replace node with its child.
Case 3: Node with Two Children
Replace with:
- Inorder successor or
- Inorder predecessor
Example:
50
/ \
30 70Delete 50.
Replace with 70.
5. Height Calculation
Height = longest path from root to leaf.
Example: A → B → D
Height = 2
6. Counting Nodes
- Total Nodes: Count all nodes recursively.
- Leaf Nodes: Nodes without children.
- Internal Nodes: Nodes having at least one child.
7. Mirror Operation
Swap left and right subtrees.
Example:
Before:
1
/ \
2 3After mirror:
1
/ \
3 28. Balanced Tree Checking
A binary tree is balanced if:
Height difference between left and right subtree ≤ 1Balanced trees improve performance.
9. Finding Maximum and Minimum
In normal binary tree:
- Entire tree traversal required.
In BST:
- Leftmost node = minimum
- Rightmost node = maximum
10. Diameter of Tree
Diameter is the length of the longest path between any two nodes.
Example:
D → B → A → CRules for Creating General Binary Tree
When creating a general binary tree:
- Each node can have 0, 1, or 2 children.
- No sorting rule exists.
- Insertion usually follows level-order strategy.
Level-order Construction Example
Given data:
12, 18, 14, 15, 13, 7, 9, 11, 8, 3Construction steps:
12becomes root.18becomes left child of12.14becomes right child of12.15becomes left child of18.13becomes right child of18.7becomes left child of14.9becomes right child of14.11becomes left child of15.8becomes right child of15.3becomes left child of13.
Final tree:
12
/ \
18 14
/ \ / \
15 13 7 9
/ \ /
11 8 3Binary Tree Representation
Binary trees are represented in memory using two methods.
1. Array Representation
Nodes are stored level by level inside arrays.
Rules:
- Parent at index
i - Left child =
2i + 1 - Right child =
2i + 2
Example:
1
/ \
2 3Array:
Index: 0 1 2
Value: 1 2 3Advantages:
- Fast indexing
- Good for complete trees
Disadvantages:
- Memory waste in sparse trees
2. Linked Representation
Each node contains:
- Data
- Left pointer
- Right pointer
Structure:
struct Node {
int data;
struct Node* left;
struct Node* right;
};Advantages:
- Flexible
- Efficient for sparse trees
Disadvantages:
- Extra pointer memory required
Applications of Binary Tree
Binary trees are used in:
- Binary Search Trees
- Heaps
- Expression Trees
- Decision Trees
- Syntax Trees
- Huffman Coding
- Artificial Intelligence
- Database systems
Advantages of Binary Tree
- Efficient searching
- Hierarchical representation
- Dynamic memory allocation
- Efficient traversal techniques
- Foundation for advanced structures
Disadvantages of Binary Tree
- Complex implementation
- Extra memory for pointers
- Unbalanced trees reduce efficiency
- Recursive algorithms may increase overhead
Time Complexity
| Operation | Average Case | Worst Case |
|---|---|---|
| Search | O(log n) | O(n) |
| Insertion | O(log n) | O(n) |
| Deletion | O(log n) | O(n) |
Worst case occurs in skewed trees.
Numerical Questions on General Binary Tree Creation
- Create a general binary tree using the following level-order data:
25, 30, 15, 20, 10, 35, 40- Draw the tree.
- Identify root, leaf, and internal nodes.
- Given the data:
50, 20, 70, 10, 30, 60, 90, 5- Construct a general binary tree in level order.
- Show the left and right child for each node.
- Construct a general binary tree from the following data sequence:
8, 12, 6, 10, 5, 7- Draw the tree.
- Write the preorder, inorder, and postorder traversals.
- Insert the following values into a general binary tree using level-order strategy:
1, 2, 3, 4, 5, 6, 7, 8- Draw the resulting binary tree.
- Count the number of:
- Leaf nodes
- Internal nodes
- Total nodes
- Height of the tree
- Given numerical data:
100, 200, 300, 400, 500, 600- Build a general binary tree using level-order.
- Represent the tree as an array (binary tree in array form).
- Given the data values:
11, 22, 33, 44, 55, 66, 77, 88, 99- Construct the binary tree using level-order.
- Perform inorder traversal and list the result.
- Create a general binary tree from the data:
10, 5, 15, 2, 7, 12, 20, 1- Draw the tree.
- What is the level of node 12?
- What is the parent of node 1?
Conclusion
A Binary Tree is one of the most fundamental and powerful non-linear data structures in computer science. It organizes data hierarchically and allows efficient searching, insertion, deletion, and traversal operations.
Understanding binary trees is extremely important because many advanced data structures and algorithms are based on them. Mastering binary trees builds a strong foundation for topics like Binary Search Trees, AVL Trees, Heaps, Graphs, and Artificial Intelligence systems.