Complete Binary Tree in Data Structures 🌳
A Complete Binary Tree (CBT) is a type of Binary Tree where:
✔ All levels except possibly the last are completely filled.
✔ All nodes are as left-aligned as possible on the last level.
📌 Example of a Complete Binary Tree
✅ Valid Complete Binary Tree:
🔹 All levels except the last are completely filled.
🔹 The last level is filled from left to right.
❌ Not a Complete Binary Tree:
🔹 The node 5
should have been placed before 3
's right child.
🔹 Nodes must be left-aligned on the last level.
📌 Properties of a Complete Binary Tree
1️⃣ Height of a Complete Binary Tree:
- For
n
nodes, the height h is: - Example: If
n = 6
, then
2️⃣ Number of Nodes:
- A complete binary tree with height
h
has at most: - Example: If height
h = 2
, max nodes =
3️⃣ Efficient Storage in Arrays:
- A Complete Binary Tree is ideal for Array Representation, since we can use index calculations:
📌 Complete Binary Tree Representation in C
🔹 Output:
📌 Advantages of a Complete Binary Tree
✅ Efficient Memory Usage – No wasted space like in sparse trees.
✅ Ideal for Heaps & Priority Queues – Used in Heap Sort & Dijkstra’s Algorithm.
✅ Easier Level-Order Traversal – Can be efficiently stored in an array.
📌 Disadvantages
❌ Insertion at the Last Level – Requires maintaining a queue or array indexing.
❌ Not Always Height Balanced – May still need balancing for some applications.
📌 Where is a Complete Binary Tree Used?
✔ Binary Heaps – Used in heap-based sorting & scheduling algorithms.
✔ Priority Queues – Implemented using Min/Max Heap structures.
✔ Huffman Coding Trees – Used in compression algorithms.
🌲 Complete Binary Trees are widely used in efficient data structures & algorithms
No comments:
Post a Comment