Print all the leaf nodes of Binary Heap
Given an array of N elements which denotes the array representation of binary heap, the task is to find the leaf nodes of this binary heap.
Examples:
Input:
arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 4 5 6 7
Explanation:
1
/ \
2 3
/ \ / \
4 5 6 7
Leaf nodes of the Binary Heap are:
4 5 6 7
Input:
arr[] = {1, 2, 3, 4, 5,
6, 7, 8, 9, 10}
Output: 6 7 8 9 10
Explanation:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ /
8 9 10
Leaf Nodes of the Binary Heap are:
6 7 8 9 10
Approach: The key observation in the problem is that the every leaf node of the Binary Heap will be at the Height H or H -1, If H is the height of the Binary Heap. Therefore, the leaf nodes can be computed as follows:
- Calculate the total height of the binary heap.
- Traverse the array in reverse order and compare the height of each node to the compute height H of the Binary Heap.
- If the height of the current node is H, then add the current node to the leaf nodes.
- Otherwise, If the height of current node is H-1 and there are no child nodes, then also add the node as leaf node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int height( int N) { return ( int ) floor (log2(N + 1)); }
void prLeafNodes(vector< int > arrlist)
{
for ( int i = arrlist.size() - 1; i >= -0; i--)
cout << arrlist[i] << " " ;
}
void findLeafNodes( int arr[], int n)
{
int h = height(n);
vector< int > arrlist;
for ( int i = n - 1; i >= 0; i--) {
if (height(i + 1) == h)
arrlist.push_back(arr[i]);
else if (height(i + 1) == h - 1
&& n <= ((2 * i) + 1))
arrlist.push_back(arr[i]);
else
break ;
}
prLeafNodes(arrlist);
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
findLeafNodes(arr, n);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG {
static int height( int N)
{
return ( int )Math.ceil(
Math.log(N + 1 )
/ Math.log( 2 ))
- 1 ;
}
static void findLeafNodes(
int arr[], int n)
{
int h = height(n);
ArrayList<Integer> arrlist
= new ArrayList<>();
for ( int i = n - 1 ; i >= 0 ; i--) {
if (height(i + 1 ) == h) {
arrlist.add(arr[i]);
}
else if (height(i + 1 ) == h - 1
&& n <= (( 2 * i) + 1 )) {
arrlist.add(arr[i]);
}
else {
break ;
}
}
printLeafNodes(arrlist);
}
static void printLeafNodes(
ArrayList<Integer> arrlist)
{
for ( int i = arrlist.size() - 1 ;
i >= 0 ; i--) {
System.out.print(
arrlist.get(i) + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 ,
6 , 7 , 8 , 9 , 10 };
findLeafNodes(arr, arr.length);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int height( int N)
{
return ( int )Math.Ceiling(
Math.Log(N + 1) /
Math.Log(2)) - 1;
}
static void findLeafNodes( int []arr,
int n)
{
int h = height(n);
List< int > arrlist = new List< int >();
for ( int i = n - 1; i >= 0; i--)
{
if (height(i + 1) == h)
{
arrlist.Add(arr[i]);
}
else if (height(i + 1) == h - 1 &&
n <= ((2 * i) + 1))
{
arrlist.Add(arr[i]);
}
else
{
break ;
}
}
printLeafNodes(arrlist);
}
static void printLeafNodes(List< int > arrlist)
{
for ( int i = arrlist.Count - 1; i >= 0; i--)
{
Console.Write(arrlist[i] + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5,
6, 7, 8, 9, 10 };
findLeafNodes(arr, arr.Length);
}
}
|
Python3
import math
def height(N):
return math.log(N + 1 ) / / math.log( 2 )
def findLeafNodes(arr, n):
h = height(n)
arrlist = []
for i in range (n - 1 , - 1 , - 1 ):
if (height(i + 1 ) = = h):
arrlist.append(arr[i])
elif (height(i + 1 ) = = h - 1 and n < = (( 2 * i) + 1 )):
arrlist.append(arr[i])
else :
break
prLeafNodes(arrlist)
def prLeafNodes(arrlist):
for i in range ( len (arrlist) - 1 , - 1 , - 1 ):
print (arrlist[i],end = " " )
arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
findLeafNodes(arr, len (arr))
|
Javascript
<script>
function height(N){
return Math.floor(Math.log(N + 1) / Math.log(2))
}
function findLeafNodes(arr, n){
let h = height(n)
let arrlist = []
for (let i = n - 1;i >= 0 ;i--){
if (height(i + 1) == h)
arrlist.push(arr[i])
else if (height(i + 1) == h - 1 && n <= ((2 * i) + 1))
arrlist.push(arr[i])
else
break
}
prLeafNodes(arrlist)
}
function prLeafNodes(arrlist){
for (let i = arrlist.length - 1 ; i>=-0; i--)
document.write(arrlist[i], " " )
}
let arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
findLeafNodes(arr, arr.length)
</script>
|
Performance Analysis:
- Time Complexity: O(L), where L is the number of leaf nodes.
- Auxiliary Space: O(1)
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...