You are on page 1of 31

Applications of BBST

Range Queries and Min Gap

Muralidhara V N
IIIT Bangalore
Range Query

0 1 2 3 4 5
4 8 2 5 1 6

RangeMaxQuery(A,0,4)=1;
RangeMinQuery(A,0,3)=2;

Update(A,i ,X) = Set A[i]=X;


Range Query

0 1 2 3 4 5
4 8 2 5 1 6

min
max 1 2 4

sum
26
4 4
1 0 5
0

12 12
3 6

1
1 3 3
5 5
8 1
5 5
BBST Structure

struct BST
{
long long int key, value, sum, max,
min;
struct BST *left, *right;

};
BBST Structure

Index are the keys in the BST.

The BBST can be built in linear time .


Build BBST in Linear Time

struct BST * CreateBBST (long long int A[], long long int l, long long
int r){
struct BST *node = NULL;
if (l <= r) {
long long int m = (l + r) / 2;
node = (struct BST *) malloc (sizeof (struct BST));
node->key = m;
node->value = A[m];
node->left = CreateBBST (A, l, m - 1);
node->right = CreateBBST (A, m + 1, r);
NodeUpdate (A, node);
} return node;
}
Build BBST in Linear Time
Void NodeUpdate (long long int A[], struct BST *node) {
if (node) {
node->max = node->min = node->key;
node->sum = node->value;
if (node->left) {
if (A[node->left->min] < A[node->min])
node->min = node->left->min;
if (A[node->left->max] > A[node->max])
node->max = node->left->max;
node->sum += node->left->sum;
}
Build BBST in Linear Time
if (node->right) {

if (A[node->right->min] < A[node->min])


node->min = node->right->min;

if (A[node->right->max] > A[node->max])


node->max = node->right->max;
node->sum += node->right->sum;

}
}

}
Build BBST in Linear Time
if (node->right) {

if (A[node->right->min] < A[node->min])


node->min = node->right->min;

if (A[node->right->max] > A[node->max])


node->max = node->right->max;
node->sum += node->right->sum;

}
}

}
Update in O(log n)
Void Update (long long int A[], struct BST *node, long long int i, long
long X){

if (node) {
if (node->key > i) Update (A, node->left, i, X);
else if (node->key < i) Update (A, node->right, i, X);
else {
A[i] = X;
node->value = X;
}
NodeUpdate (A, node);
}
}
Prefix Sum in O(log n)
long long int PreSum (struct BST *node, long long int i){
long long int sum = 0;

while (node){
if (node->key > i) node = node->left;
else if (node->key < i) {
if (node->left) sum += node->left->sum;
sum += node->value; node = node->right;}
else { if (node->left) sum += node->left->sum;
sum += node->value;
return sum;}
} return 0;
}
Range Min in O(log n)
long long int RangeMin (long long int A[], struct BST *node, long
long int l, long long r, long long int s, long long int e){
if (node) {
if (s >= l && e <= r) return node->min;
else if (e < l || r < s) return -1;
else {
long long int min, m1, m = (s + e) / 2;
if (node->key >= l && node->key <= r) min = node->key;
else min = -1;
m1 = RangeMin (A, node->right, l, r, m + 1, e);
if (min < 0) min = m1;
if (min > -1 && m1 > -1 && A[m1] < A[min])
min = m1;
Range Min in O(log n)
m1 = RangeMin (A, node->left, l, r, s, m - 1);
if (min < 0) min = m1;
if (min > -1 && m1 > -1 && A[m1] < A[min])
min = m1;
return min;
}

}
return -1;
}
Range Max Gap

long long int MaxGap (long long int A[], struct BST *node, long long
int l, long long int r, long long int n)
{
return
A[RangeMax (A, node, l, r, 0, n - 1)] -
A[RangeMin (A, node, l, r, 0, n - 1)];
}
BBST in O(log n)

• Update()
• PreSum()
• RangeSum()
• RangeMinQuery()
• RangeMaxQuery()
• RangeMaxGap() –maximum
difference among all the numbers in
the given range
RangeMinGap in O(n log n)

• RangeMinGap( i,j) find the minimum


difference, among all the numbers
between A[i] and A[j].

• Sort the numbers between A[i] and


A[j] and compare the consecutive
numbers.
Dynamic Data Set

• Add()
• Delete()
• Search()
• Mingap() minimum difference among
the numbers
• Maxgap() maximum difference
among the numbers – is the
difference between max and min.
Dynamic Data Set

2, 15, 32, 35, 55


• Mingap()=3
• Maxgap()=53
Dynamic Data Set

2, 15, 32, 35, 55


• Mingap()=3
• Maxgap()=53
• Delete(32) and Add(60)
• 2, 15, 35, 55,60
• Mingap()=5
• Maxgap()=58
Range Query

min
2
max 60 35
mingap
5
55 55
60
15 2 2

13 5
MAX

15 15 60 60
15
60
MAX
Structure of BST

struct BST
{
long long int key;
int height;
long long int count, c, min, max,
mingap;
struct BST *left, *right;
};
Max value at a node

Node->value

Node->max=Max Node->left->max

Node->right-
>max
Max value at a node

Node->value

Node->min=Min Node->left->min

Node->right-
>min
Max value at a node

node->value-node->left->max

node->right->min-node->value
node->
mingap=Min node->left->mingap

node->right->mingap
Update
Void Update (struct BST *node) {
node->max = node->min = node->key;
if (node->left && node->left->max > node->max)
node->max = node->left->max;
if (node->right && node->right->max > node->max)
node->max = node->right->max;

if (node->left && node->left->min < node->min)


node->min = node->left->min;
if (node->right && node->right->min < node->min)
node->min = node->right->min;
Update

node->mingap = INT_MAX;
if (node->left)
node->mingap =
Min (node->left->mingap,
Min (node->mingap, node->key - node->left->max));
if (node->right)
node->mingap =
Min (node->right->mingap,
Min (node->mingap, node->right->min - node->key));

node->height = Height (node);


node->count = Count (node);
}
Range Max Gap

long long int MaxGap (struct BST *node, long long int l, long long int
r) {
if (l <= r) {
if (!Search (node, l))
l = Successor (node, l)->key;
if (!Search (node, r))
r = Predecessor (node, r)->key;
return (r - l);
}
return -1;
}
Range Min Gap

long long int MinGap (struct BST *node, long long int l, long long int
r) {
if (l <= r){
if (!Search (node, l))
l = Successor (node, l)->key;
if (!Search (node, r))
r = Predecessor (node, r)->key;

return MinGap1 (node, l, r);


}
return INT_MAX;
}
Range Min Gap

long long int MinGap1 (struct BST *node, long long int l, long long
int r){
if (l <= r && node) {

if (node->key <= l)
return MinGap1 (node->right, l, r);

else if (node->key >= r)


return MinGap1 (node->left, l, r);
else
Range Min Gap
{
long long int min = INT_MAX;
min = Min (MinGap2 (node->left, l), MinGap3 (node->right, r));
if (node->left && node->left->key >= l)
min = Min (min, node->key - node->left->max);
if (node->right && node->right->key <= r)
min = Min (min, node->right->min - node->key);

return min;
}
}
return INT_MAX;
}
BBST in O(log n)

• Add()
• Delete()
• Search()
• RangeMaxGap()
• RangeMinGap()

• Maxgap and Mingap in the whole set


can be found on O(1)

You might also like