Consider the array A = <4, 1, 3, 2, 16, 9, 10, 14, 8, 7>. After building heap from the array A, the depth of the heap and the right child of max-heap are_______ and _____ respectively. (Root is at level 0).
3, 10
A heap is a specialized tree-based data structure that satisfies the heap property. In a max-heap, for any given node C, if P is a parent node of C, then the value of P is greater than or equal to the value of C. The highest element is always at the root of the heap. A heap is a complete binary tree, meaning all levels except possibly the last are completely filled, and nodes at the last level are as far left as possible.
We are given an array A = <4, 1, 3, 2, 16, 9, 10, 14, 8, 7>. We need to perform two tasks:
The given array A has 10 elements. When an array is used to represent a complete binary tree, the elements are typically placed level by level from left to right. Using 0-based indexing for the array A = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]:
| Index | Value | Level (Root at 0) |
|---|---|---|
| 0 | 4 | 0 |
| 1, 2 | 1, 3 | 1 |
| 3, 4, 5, 6 | 2, 16, 9, 10 | 2 |
| 7, 8, 9 | 14, 8, 7 | 3 |
The tree structure formed by placing these elements in a complete binary tree form has nodes at levels 0, 1, 2, and 3. The maximum level number is 3.
The depth of a tree is the number of edges on the longest path from the root to a leaf. If the root is at level 0, this is equivalent to the maximum level number.
For a complete binary tree with \( n \) nodes, the depth (with root at level 0) is \( \lfloor \log_2 n \rfloor \). Here, \( n = 10 \).
\( \log_2 10 \approx 3.32 \)
Depth \( = \lfloor \log_2 10 \rfloor = \lfloor 3.32 \rfloor = 3 \).
So, the initial depth of the tree represented by array A is 3. Building a heap from this array involves rearranging elements while maintaining the complete binary tree structure, so the depth of the resulting heap will remain the same.
To build a max-heap from an array, we use the BUILD-MAX-HEAP procedure. This procedure works by running the MAX-HEAPIFY procedure on all non-leaf nodes in the tree, starting from the last non-leaf node and working up to the root.
Using 0-based indexing for array A = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7], the size of the heap is \( n=10 \). The indices of non-leaf nodes range from \( \lfloor n/2 \rfloor - 1 \) down to 0.
\( \lfloor 10/2 \rfloor - 1 = 5 - 1 = 4 \).
So we apply MAX-HEAPIFY to indices 4, 3, 2, 1, and 0.
Let's trace the MAX-HEAPIFY calls (values change as swaps occur):
After performing BUILD-MAX-HEAP, the array becomes:
[16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
This array represents the max-heap. Let's visualize its tree structure (using 0-based indexing):
Level 0: 16 (Index 0 - Root)
Level 1: 14 (Index 1 - Left Child of Root), 10 (Index 2 - Right Child of Root)
Level 2: 8 (Index 3), 7 (Index 4), 9 (Index 5), 3 (Index 6)
Level 3: 2 (Index 7), 4 (Index 8), 1 (Index 9)
The maximum level number is still 3. Thus, the depth of the max-heap is 3.
In the final max-heap array [16, 14, 10, 8, 7, 9, 3, 2, 4, 1], the root is at index 0, which is the element 16.
In a 0-based indexed array representation of a complete binary tree:
For the root node at index \( i=0 \):
Therefore, the right child of the root (16) in the max-heap is 10.
After building the max-heap from the array A, the depth of the heap is 3, and the right child of the root is 10.
The required values are 3 and 10.
| Term | Description |
|---|---|
| Max-Heap | A complete binary tree where parent nodes are greater than or equal to their children. |
| Heap Depth | The number of edges on the longest path from the root to a leaf. With root at level 0, it's the maximum level number. For \( n \) nodes, it's \( \lfloor \log_2 n \rfloor \). |
| BUILD-MAX-HEAP | An algorithm to convert an arbitrary array into a max-heap. It uses MAX-HEAPIFY. |
| MAX-HEAPIFY | A procedure to maintain the max-heap property. It corrects a single violation at a node assuming its subtrees are already max-heaps. |
| Complete Binary Tree | A binary tree where all levels except possibly the last are completely filled, and nodes at the last level are as far left as possible. Essential for array representation of heaps. |
Heaps are fundamental data structures used in various algorithms and applications.
What is the best case time complexity of radix sort algorithm?
Merge sort uses:
A sorting technique that guarantees that records with the same primary key occurs in the same
order in the sorted list as in the original unsorted list is said to beWhich of the following algorithm design approach is used in Quick sort algorithm?
Which of the following is best running time to sort n integers in the range 0 to n 2-1