Wavelet Trees | Introduction
Last Updated :
28 Apr, 2023
A wavelet tree is a data structure that recursively partitions a stream into two parts until we’re left with homogeneous data. The name derives from an analogy with the wavelet transform for signals, which recursively decomposes a signal into low-frequency and high-frequency components. Wavelet trees can be used to answer range queries efficiently.
Consider the problem to find number of elements in a range [L, R] of a given array A which are less than x. One way to solve this problem efficiently is using Persistent Segment Tree data structure. But we can also solve this easily using Wavelet Trees. Let us see how!
Constructing Wavelet TreesEvery node in a wavelet tree is represented by an array which is the subsequence of original array and a range [L, R]. Here [L, R] is the range in which elements of array falls. That is, ‘R’ denotes maximum element in the array and ‘L’ denotes the smallest element. So, the root node will contain the original array in which elements are in range [L, R]. Now we will calculate the middle of the range [L, R] and stable partition the array in two halves for the left and right childs. Therefore, the left child will contains elements that lies in range [L, mid] and right child will contain elements that lies in the range [mid+1, R].
Suppose we are given an array of integers. Now we compute the mid (Max + Min / 2) and form two children.
Left Children: Integers less than/equal to Mid
Right Children: Integers greater than Mid
We recursively perform this operation until all node of similar elements are formed.
Given array : 0 0 9 1 2 1 7 6 4 8 9 4 3 7 5 9 2 7 0 5 1 0
To construct a Wavelet Tree, let us see what will we need to store at each node. So at each node of the tree, we will store two arrays say S[] and freq[]. The array S[] will be a subsequence of the original array A[] and the array freq[] will store the count of the elements that will go to left and right childs of the node. That is, freq[i] will denote the count of elements from the first i elements of S[] that will go to left child. Therefore, count of elements that will go to right child can be easily calculated as (i – freq[i]).
Below example shows how to maintain freq[] array:
Array : 1 5 2 6 4 4
Mid = (1 + 6) / 2 = 3
Left Child : 1 2
Right Child : 5 6 4 4
To maintain frequency array, we will check if the element is less than Mid or not. If yes, then we will add 1 to last element of frequency array, else 0 and push back again.
For, above array :
Freq array :{1, 1, 2, 2, 2, 2}
It implies 1 element will go to left child of this node from index 1 and 2, and 2 elements will go to left child from indices 3 to 6. This can be easily depicted from the above given array.
To compute the number of elements moving to right subtree, we subtract freq[i] from i.
From index 1, 0 elements go to right subtree.
From index 2, 1 element go to right subtree.
From index 3, 1 element go to right subtree.
From index 4, 2 elements go to right subtree.
From index 5, 3 elements go to right subtree.
From index 6, 4 elements go to right subtree.
We can use the stable_partition function and lambda expression in C++ STL to easily stable partition the array around a pivot without distorting the order of elements in original sequence. It is highly recommended to go through the stable_partition and lambda expression articles before moving onto implementation.
Below is the implementation of construction of Wavelet Trees:
CPP
#include <bits/stdc++.h>
using namespace std;
#define N 100000
int arr[N];
class wavelet_tree {
public :
int low, high;
wavelet_tree *l, *r;
vector< int > freq;
wavelet_tree( int * from, int * to, int x, int y)
{
low = x, high = y;
if (from >= to)
return ;
if (high == low) {
freq.reserve(to - from + 1);
freq.push_back(0);
for ( auto it = from; it != to; it++)
freq.push_back(freq.back() + 1);
return ;
}
int mid = (low + high) / 2;
auto lessThanMid
= [mid]( int x) { return x <= mid; };
freq.reserve(to - from + 1);
freq.push_back(0);
for ( auto it = from; it != to; it++)
freq.push_back(freq.back() + lessThanMid(*it));
auto pivot
= stable_partition(from, to, lessThanMid);
l = new wavelet_tree(from, pivot, low, mid);
r = new wavelet_tree(pivot, to, mid + 1, high);
}
};
int main()
{
int size = 5, high = INT_MIN;
int arr[] = { 1, 2, 3, 4, 5 };
for ( int i = 0; i < size; i++)
high = max(high, arr[i]);
wavelet_tree obj(arr, arr + size, 1, high);
return 0;
}
|
Java
import java.util.*;
class WaveletTree {
int low, high;
WaveletTree l, r;
List<Integer> freq;
public WaveletTree( int [] arr, int low, int high)
{
this .low = low;
this .high = high;
if (low == high) {
freq = new ArrayList<>();
freq.add( 0 );
for ( int i = 0 ; i < arr.length; i++) {
freq.add(freq.get(i) + 1 );
}
return ;
}
int mid = (low + high) / 2 ;
freq = new ArrayList<>();
freq.add( 0 );
int [] leftArr = new int [arr.length];
int [] rightArr = new int [arr.length];
int leftIndex = 0 , rightIndex = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] <= mid) {
leftArr[leftIndex++] = arr[i];
freq.add(freq.get(i) + 1 );
}
else {
rightArr[rightIndex++] = arr[i];
freq.add(freq.get(i));
}
}
l = new WaveletTree(leftArr, low, mid);
r = new WaveletTree(rightArr, mid + 1 , high);
}
}
public class Main {
public static void main(String[] args)
{
int size = 5 , high = Integer.MIN_VALUE;
int [] arr = { 1 , 2 , 3 , 4 , 5 };
for ( int i = 0 ; i < size; i++)
high = Math.max(high, arr[i]);
WaveletTree obj = new WaveletTree(arr, 1 , high);
}
}
|
Javascript
const N = 100000;
const INT_MIN = -2147483647;
class wavelet_tree {
constructor(arr, low, high) {
this .low = low;
this .high = high;
if (low == high) {
this .freq = [];
this .freq.push(0);
for (let i = 0; i < arr.length; i++) {
this .freq.push( this .freq[i] + 1);
}
return ;
}
let mid = Math.floor((low + high) / 2);
this .freq = [];
this .freq.push(0);
let leftArr = new Array(arr.length);
let rightArr = new Array(arr.length);
let leftIndex = 0, rightIndex = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] <= mid) {
leftArr[leftIndex++] = arr[i];
this .freq.push( this .freq[i] + 1);
} else {
rightArr[rightIndex++] = arr[i];
this .freq.push( this .freq[i]);
}
}
this .l = new wavelet_tree(leftArr, low, mid);
this .r = new wavelet_tree(rightArr, mid + 1, high);
}
}
let size = 5;
let high = INT_MIN;
let arr = [1 , 2, 3, 4, 5];
for (let i = 0; i < size; i++)
high = Math.max(high, arr[i]);
let obj = new wavelet_tree(arr, 1, high);
|
Python3
class WaveletTree:
def __init__( self , from_idx, to_idx, low, high, arr):
self .low = low
self .high = high
if from_idx > = to_idx:
return
if self .high = = self .low:
self .freq = [ 0 ] * (to_idx - from_idx + 2 )
for i in range (from_idx, to_idx + 1 ):
self .freq[i - from_idx + 1 ] = self .freq[i - from_idx] + 1
return
mid = ( self .low + self .high) / / 2
self .freq = [ 0 ] * (to_idx - from_idx + 2 )
for i in range (from_idx, to_idx + 1 ):
self .freq[i - from_idx + 1 ] = self .freq[i - from_idx] + (arr[i] < = mid)
pivot = from_idx
while pivot < = to_idx and arr[pivot] < = mid:
pivot + = 1
self .l = WaveletTree(from_idx, pivot - 1 , self .low, mid, arr)
self .r = WaveletTree(pivot, to_idx, mid + 1 , self .high, arr)
def kOrLess( self , l, r, k):
if l > r or k < self .low:
return 0
if self .high < = k:
return r - l + 1
LtCount = self .freq[l - 1 ]
RtCount = self .freq[r]
return (
self .l.kOrLess(LtCount + 1 , RtCount, k) +
self .r.kOrLess(l - LtCount, r - RtCount, k)
)
size = 5
high = float ( '-inf' )
arr = [ 1 , 2 , 3 , 4 , 5 ]
for i in range (size):
high = max (high, arr[i])
obj = WaveletTree( 0 , size - 1 , 1 , high, arr)
print (obj.kOrLess( 1 , 3 , 2 ))
|
C#
using System;
using System.Collections.Generic;
class WaveletTree {
int low, high;
WaveletTree l, r;
List< int > freq;
public WaveletTree( int [] arr, int low, int high)
{
this .low = low;
this .high = high;
if (low == high) {
freq = new List< int >();
freq.Add(0);
for ( int i = 0; i < arr.Length; i++) {
freq.Add(freq[i] + 1);
}
return ;
}
int mid = (low + high) / 2;
freq = new List< int >();
freq.Add(0);
int [] leftArr = new int [arr.Length];
int [] rightArr = new int [arr.Length];
int leftIndex = 0, rightIndex = 0;
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] <= mid) {
leftArr[leftIndex++] = arr[i];
freq.Add(freq[i] + 1);
}
else {
rightArr[rightIndex++] = arr[i];
freq.Add(freq[i]);
}
}
l = new WaveletTree(leftArr, low, mid);
r = new WaveletTree(rightArr, mid + 1, high);
}
}
class Program {
static void Main( string [] args)
{
int size = 5, high = int .MinValue;
int [] arr = { 1, 2, 3, 4, 5 };
for ( int i = 0; i < size; i++)
high = Math.Max(high, arr[i]);
WaveletTree obj = new WaveletTree(arr, 1, high);
}
}
|
Height of the tree: O(log(max(A)) , where max(A) is the maximum element in the array A[].
Querying in Wavelet Trees
We have already constructed our wavelet tree for the given array. Now we will move on to our problem to calculate number of elements less than or equal to x in range [ L,R ] in the given array.
So, for each node we have a subsequence of original array, lowest and highest values present in the array and count of elements in left and right child.
Now,
If high <= x,
we return R - L + 1.
i.e. all the elements in the current range is less than x.
Otherwise, We will use variable LtCount = freq[ L-1 ] (i.e. elements going to left sub-tree from L-1) , RtCount = freq[ R ] (i.e. elements going to right sub-tree from R)
Now, we recursively call and add the return values of :
left sub-tree with range[ LtCount + 1, RtCount ] and,
right sub-tree with range[ L - Ltcount,R - RtCount ]
Below is the implementation in C++:
CPP
#include <bits/stdc++.h>
using namespace std;
#define N 100000
int arr[N];
class wavelet_tree {
public :
int low, high;
wavelet_tree* l, *r;
vector< int > freq;
wavelet_tree( int * from, int * to, int x, int y)
{
low = x, high = y;
if (from >= to)
return ;
if (high == low) {
freq.reserve(to - from + 1);
freq.push_back(0);
for ( auto it = from; it != to; it++)
freq.push_back(freq.back() + 1);
return ;
}
int mid = (low + high) / 2;
auto lessThanMid = [mid]( int x) {
return x <= mid;
};
freq.reserve(to - from + 1);
freq.push_back(0);
for ( auto it = from; it != to; it++)
freq.push_back(freq.back() + lessThanMid(*it));
auto pivot = stable_partition(from, to, lessThanMid);
l = new wavelet_tree(from, pivot, low, mid);
r = new wavelet_tree(pivot, to, mid + 1, high);
}
int kOrLess( int l, int r, int k)
{
if (l > r or k < low)
return 0;
if (high <= k)
return r - l + 1;
int LtCount = freq[l - 1];
int RtCount = freq[r];
return ( this ->l->kOrLess(LtCount + 1, RtCount, k) +
this ->r->kOrLess(l - LtCount, r - RtCount, k));
}
};
int main()
{
int size = 5, high = INT_MIN;
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < size; i++)
high = max(high, arr[i]);
wavelet_tree obj(arr, arr + size, 1, high);
cout << obj.kOrLess(0, 3, 2) << '\n' ;
return 0;
}
|
Python3
class WaveletTree:
def __init__( self , from_idx, to_idx, low, high, arr):
self .low = low
self .high = high
if from_idx > = to_idx:
return
if self .high = = self .low:
self .freq = [ 0 ] * (to_idx - from_idx + 2 )
for i in range (from_idx, to_idx + 1 ):
self .freq[i - from_idx + 1 ] = self .freq[i - from_idx] + 1
return
mid = ( self .low + self .high) / / 2
self .freq = [ 0 ] * (to_idx - from_idx + 2 )
for i in range (from_idx, to_idx + 1 ):
self .freq[i - from_idx + 1 ] = self .freq[i - from_idx] + (arr[i] < = mid)
pivot = from_idx
while pivot < = to_idx and arr[pivot] < = mid:
pivot + = 1
self .l = WaveletTree(from_idx, pivot - 1 , self .low, mid, arr)
self .r = WaveletTree(pivot, to_idx, mid + 1 , self .high, arr)
def kOrLess( self , l, r, k):
if l > r or k < self .low:
return 0
if self .high < = k:
return r - l + 1
LtCount = self .freq[l - 1 ]
RtCount = self .freq[r]
return (
self .l.kOrLess(LtCount + 1 , RtCount, k) +
self .r.kOrLess(l - LtCount, r - RtCount, k)
)
size = 5
high = float ( '-inf' )
arr = [ 1 , 2 , 3 , 4 , 5 ]
for i in range (size):
high = max (high, arr[i])
obj = WaveletTree( 0 , size - 1 , 1 , high, arr)
print (obj.kOrLess( 1 , 3 , 2 ))
|
Javascript
class WaveletTree {
constructor(from, to, x, y) {
this .low = x;
this .high = y;
if (from >= to) return ;
if ( this .high == this .low) {
this .freq = [0];
for (let it = from; it <= to; it++) this .freq.push( this .freq[ this .freq.length - 1] + 1);
return ;
}
let mid = Math.floor(( this .low + this .high) / 2);
let lessThanMid = x => x <= mid;
this .freq = [0];
for (let it = from; it <= to; it++) this .freq.push( this .freq[ this .freq.length - 1] + lessThanMid(arr[it]));
let pivot = from;
while (pivot <= to && lessThanMid(arr[pivot])) pivot++;
this .l = new WaveletTree(from, pivot - 1, this .low, mid);
this .r = new WaveletTree(pivot, to, mid + 1, this .high);
}
kOrLess(l, r, k) {
if (l > r || k < this .low) return 0;
if ( this .high <= k) return r - l + 1;
let LtCount = this .freq[l - 1];
let RtCount = this .freq[r];
return (
this .l.kOrLess(LtCount + 1, RtCount, k) +
this .r.kOrLess(l - LtCount, r - RtCount, k)
);
}
}
let size = 5,
high = Number.MIN_SAFE_INTEGER;
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < size; i++) high = Math.max(high, arr[i]);
let obj = new WaveletTree(0, size - 1, 1, high);
console.log(obj.kOrLess(0 + 1 , 3 + 1 , 2));
|
+
Time Complexity: O(log(max(A)) , where max(A) is the maximum element in the array A[].
In this post we have discussed about a single problem on range queries without update. In further we will be discussing on range updates also.
References :
Share your thoughts in the comments
Please Login to comment...