Given a Complete Binary Tree as an array, the task is to print all of its levels in sorted order.
Examples:
Input: arr[] = {7, 6, 5, 4, 3, 2, 1}
The given tree looks like
7
/ \
6 5
/ \ / \
4 3 2 1
Output:
7
5 6
1 2 3 4
Input: arr[] = {5, 6, 4, 9, 2, 1}
The given tree looks like
5
/ \
6 4
/ \ /
9 2 1
Output:
5
4 6
1 2 9
Approach: A similar problem is discussed here
As the given tree is a Complete Binary Tree:
No. of nodes at a level l will be 2l where l ? 0
- Start traversing the array with level initialized as 0.
- Sort the elements which are the part of the current level and print the elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printSortedLevels( int arr[], int n)
{
int level = 0;
for ( int i = 0; i < n; level++) {
int cnt = ( int ) pow (2, level);
cnt -= 1;
int j = min(i + cnt, n - 1);
sort(arr + i, arr + j + 1);
while (i <= j) {
cout << arr[i] << " " ;
i++;
}
cout << endl;
}
}
int main()
{
int arr[] = { 5, 6, 4, 9, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
printSortedLevels(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static void printSortedLevels( int arr[], int n)
{
int level = 0 ;
for ( int i = 0 ; i < n; level++)
{
int cnt = ( int )Math.pow( 2 , level);
cnt -= 1 ;
int j = Math.min(i + cnt, n - 1 );
Arrays.sort(arr, i, j+ 1 );
while (i <= j)
{
System.out.print(arr[i] + " " );
i++;
}
System.out.println();
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 4 , 9 , 2 , 1 };
int n = arr.length;
printSortedLevels(arr, n);
}
}
|
Python3
from math import pow
def printSortedLevels(arr, n):
level = 0
i = 0
while (i < n):
cnt = int ( pow ( 2 , level))
cnt - = 1
j = min (i + cnt, n - 1 )
arr = arr[:i] + sorted (arr[i:j + 1 ]) + \
arr[j + 1 :]
while (i < = j):
print (arr[i], end = " " )
i + = 1
print ()
level + = 1
arr = [ 5 , 6 , 4 , 9 , 2 , 1 ]
n = len (arr)
printSortedLevels(arr, n)
|
C#
using System;
using System.Linq;
class GFG
{
static void printSortedLevels( int []arr, int n)
{
int level = 0;
for ( int i = 0; i < n; level++)
{
int cnt = ( int )Math.Pow(2, level);
cnt -= 1;
int j = Math.Min(i + cnt, n - 1);
Array.Sort(arr, i, j + 1 - i);
while (i <= j)
{
Console.Write(arr[i] + " " );
i++;
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int []arr = { 5, 6, 4, 9, 2, 1 };
int n = arr.Length;
printSortedLevels(arr, n);
}
}
|
Javascript
<script>
function partSort(arr, N, a, b)
{
let l = Math.min(a, b);
let r = Math.max(a, b);
let temp = new Array(r - l + 1);
temp.fill(0);
let j = 0;
for (let i = l; i <= r; i++) {
temp[j] = arr[i];
j++;
}
temp.sort( function (a, b){ return a - b});
j = 0;
for (let i = l; i <= r; i++) {
arr[i] = temp[j];
j++;
}
}
function printSortedLevels(arr, n)
{
let level = 0;
for (let i = 0; i < n; level++)
{
let cnt = Math.pow(2, level);
cnt -= 1;
let j = Math.min(i + cnt, n - 1);
partSort(arr, n, i, j + 1);
while (i <= j)
{
document.write(arr[i] + " " );
i++;
}
document.write( "</br>" );
}
}
let arr = [ 5, 6, 4, 9, 2, 1 ];
let n = arr.length;
printSortedLevels(arr, n);
</script>
|
Time complexity: O(nlogn) where n is no of nodes in binary tree
Auxiliary space: O(1) as it is using constant space