|
| 1 | +<h1 align='center'>Is - Binary - Tree - Heap</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Is Binary Tree Heap](https://www.geeksforgeeks.org/problems/is-binary-tree-heap/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | +**Problem Statement**: |
| 12 | +The task is to determine if a given binary tree is a **max-heap**. A binary tree satisfies the properties of a max-heap if: |
| 13 | +1. It is a **complete binary tree** (CBT): All levels are fully filled except possibly the last level, and nodes at the last level are as far left as possible. |
| 14 | +2. It follows the **max-heap order property**: For each node, its value is greater than or equal to the values of its children. |
| 15 | + |
| 16 | +**Examples**: |
| 17 | + |
| 18 | +1. **Max-Heap Example**: |
| 19 | + ``` |
| 20 | + 10 |
| 21 | + / \ |
| 22 | + 9 8 |
| 23 | + / \ / \ |
| 24 | + 7 6 5 4 |
| 25 | + ``` |
| 26 | + This tree is a max-heap since it is complete and each parent node’s value is greater than its children’s values. |
| 27 | + |
| 28 | +2. **Not a Max-Heap Example**: |
| 29 | + ``` |
| 30 | + 10 |
| 31 | + / \ |
| 32 | + 9 15 |
| 33 | + / \ / \ |
| 34 | + 7 6 5 4 |
| 35 | + ``` |
| 36 | + This tree is not a max-heap because node `15` is greater than its parent `10`, violating the max-heap property. |
| 37 | + |
| 38 | +**Approach**: |
| 39 | +1. **Check if the tree is complete** using `isCBT`. |
| 40 | + - Calculate the total number of nodes using `countNodes`. |
| 41 | + - Traverse the tree, ensuring each node is positioned as expected for a complete binary tree. |
| 42 | +2. **Check if the tree satisfies the max-heap property** using `isMaxOrder`. |
| 43 | + - For each node, compare it with its left and right children (if they exist). |
| 44 | + - Ensure each parent node’s value is greater than its children's values. |
| 45 | + |
| 46 | +## Problem Solution |
| 47 | +```cpp |
| 48 | +class Solution { |
| 49 | + public: |
| 50 | + int countNodes(struct Node* root){ |
| 51 | + if(root == NULL) return 0; |
| 52 | + |
| 53 | + return 1 + countNodes(root -> left) + countNodes(root -> right); |
| 54 | + } |
| 55 | + |
| 56 | + bool isCBT(struct Node* root, int index, int count){ |
| 57 | + if(root == NULL) return true; |
| 58 | + |
| 59 | + if(index >= count) return false; |
| 60 | + |
| 61 | + else { |
| 62 | + bool left = isCBT(root -> left, 2 * index + 1, count); |
| 63 | + bool right = isCBT(root -> right, 2 * index + 2, count); |
| 64 | + |
| 65 | + return left && right; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + bool isMaxOrder(struct Node* root){ |
| 70 | + if(root -> left == NULL && root -> right == NULL) return true; |
| 71 | + |
| 72 | + if(root -> right == NULL) return (root -> data > root -> left -> data); |
| 73 | + else { |
| 74 | + bool left = isMaxOrder(root -> left); |
| 75 | + bool right = isMaxOrder(root -> right); |
| 76 | + |
| 77 | + return (left && right && |
| 78 | + (root -> data > root -> left -> data && root -> data > root -> right -> data)); |
| 79 | + } |
| 80 | + } |
| 81 | + bool isHeap(struct Node* root) { |
| 82 | + int index = 0; |
| 83 | + int totalCount = countNodes(root); |
| 84 | + |
| 85 | + if(isCBT(root, index, totalCount) && isMaxOrder(root)) return true; |
| 86 | + |
| 87 | + return false; |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +
|
| 92 | +## Problem Solution Explanation |
| 93 | +
|
| 94 | +```cpp |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + int countNodes(struct Node* root){ |
| 98 | + if(root == NULL) return 0; |
| 99 | + |
| 100 | + return 1 + countNodes(root -> left) + countNodes(root -> right); |
| 101 | + } |
| 102 | +``` |
| 103 | +- **`countNodes` Function**: This function calculates the total number of nodes in the tree. |
| 104 | + - If `root` is `NULL`, it returns `0`, indicating no nodes. |
| 105 | + - Otherwise, it recursively counts nodes in the left and right subtrees, adding `1` for the current node. |
| 106 | + |
| 107 | +```cpp |
| 108 | + bool isCBT(struct Node* root, int index, int count){ |
| 109 | + if(root == NULL) return true; |
| 110 | + |
| 111 | + if(index >= count) return false; |
| 112 | + |
| 113 | + else { |
| 114 | + bool left = isCBT(root -> left, 2 * index + 1, count); |
| 115 | + bool right = isCBT(root -> right, 2 * index + 2, count); |
| 116 | + |
| 117 | + return left && right; |
| 118 | + } |
| 119 | + } |
| 120 | +``` |
| 121 | +- **`isCBT` Function**: This function recursively checks if the binary tree is a complete binary tree (CBT). |
| 122 | + - If `root` is `NULL`, it returns `true`, as an empty tree is considered complete. |
| 123 | + - If `index >= count`, it returns `false` because this implies that the node’s position is invalid for a CBT. |
| 124 | + - Recursively checks left and right subtrees with updated indices (`2 * index + 1` for the left child and `2 * index + 2` for the right child). |
| 125 | + - Returns `true` if both left and right subtrees are complete. |
| 126 | +
|
| 127 | +```cpp |
| 128 | + bool isMaxOrder(struct Node* root){ |
| 129 | + if(root -> left == NULL && root -> right == NULL) return true; |
| 130 | + |
| 131 | + if(root -> right == NULL) return (root -> data > root -> left -> data); |
| 132 | + else { |
| 133 | + bool left = isMaxOrder(root -> left); |
| 134 | + bool right = isMaxOrder(root -> right); |
| 135 | + |
| 136 | + return (left && right && |
| 137 | + (root -> data > root -> left -> data && root -> data > root -> right -> data)); |
| 138 | + } |
| 139 | + } |
| 140 | +``` |
| 141 | +- **`isMaxOrder` Function**: This function checks if the tree satisfies the max-heap property. |
| 142 | + - If the node has no children (`left` and `right` are `NULL`), it returns `true`, as a leaf node satisfies the max-heap property. |
| 143 | + - If the node has only a left child, it checks that `root->data` is greater than `root->left->data`. |
| 144 | + - If the node has both children, it checks recursively that both left and right subtrees satisfy max-order and that `root->data` is greater than both child values. |
| 145 | + |
| 146 | +```cpp |
| 147 | + bool isHeap(struct Node* root) { |
| 148 | + int index = 0; |
| 149 | + int totalCount = countNodes(root); |
| 150 | + |
| 151 | + if(isCBT(root, index, totalCount) && isMaxOrder(root)) return true; |
| 152 | + |
| 153 | + return false; |
| 154 | + } |
| 155 | +}; |
| 156 | +``` |
| 157 | +- **`isHeap` Function**: This function combines the previous checks to determine if the tree is a max-heap. |
| 158 | + - Calls `countNodes` to get the total count of nodes. |
| 159 | + - Calls `isCBT` to check completeness and `isMaxOrder` to check the max-heap order. |
| 160 | + - Returns `true` if both checks are `true`. |
| 161 | +
|
| 162 | +### Step 3: Examples and Explanation |
| 163 | +
|
| 164 | +1. **Example 1**: |
| 165 | + - **Tree**: |
| 166 | + ``` |
| 167 | + 20 |
| 168 | + / \ |
| 169 | + 15 18 |
| 170 | + / \ / |
| 171 | + 10 13 9 |
| 172 | + ``` |
| 173 | + - **Process**: |
| 174 | + - `countNodes` returns `6`. |
| 175 | + - `isCBT` confirms that the tree structure meets completeness. |
| 176 | + - `isMaxOrder` confirms that each parent node is greater than its children. |
| 177 | + - **Output**: **`true`** (The tree is a max-heap). |
| 178 | +
|
| 179 | +2. **Example 2**: |
| 180 | + - **Tree**: |
| 181 | + ``` |
| 182 | + 20 |
| 183 | + / \ |
| 184 | + 15 30 |
| 185 | + / \ / |
| 186 | + 10 13 9 |
| 187 | + ``` |
| 188 | + - **Process**: |
| 189 | + - `countNodes` returns `6`. |
| 190 | + - `isCBT` confirms the tree is complete. |
| 191 | + - `isMaxOrder` identifies node `30` as greater than its parent `20`, violating the max-heap property. |
| 192 | + - **Output**: **`false`** (The tree is not a max-heap). |
| 193 | +
|
| 194 | +### Step 4: Time and Space Complexity |
| 195 | +
|
| 196 | +- **Time Complexity**: |
| 197 | + - `countNodes` traverses each node, so its time complexity is **O(N)**. |
| 198 | + - `isCBT` also traverses each node for the completeness check, with **O(N)** complexity. |
| 199 | + - `isMaxOrder` traverses each node to validate the max-heap property, with **O(N)** complexity. |
| 200 | + - Overall time complexity: **O(N)**. |
| 201 | +
|
| 202 | +- **Space Complexity**: |
| 203 | + - The recursion depth is proportional to the height of the tree. |
| 204 | + - In the worst case, the height of the tree could be `log N` (balanced tree) or `N` (skewed tree). |
| 205 | + - Overall space complexity: **O(H)**, where `H` is the height of the tree. |
| 206 | +
|
| 207 | +### Step 5: Additional Recommendations |
| 208 | +
|
| 209 | +- **Edge Cases**: Handle cases where the tree is empty or consists of only one node. |
| 210 | +- **Alternative Approach**: Use level-order traversal to verify completeness and heap order simultaneously for efficiency. This approach could reduce the need for separate recursive checks. |
| 211 | +- **Practice**: Manually trace through different tree examples to better understand heap properties and recursive checks. |
0 commit comments