segment tree

23
Segment Tree 1

Upload: shohanur-rahman

Post on 18-Jul-2015

124 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Segment Tree

Segment Tree

1

Page 2: Segment Tree

Presented BY:

Shohanur Rahman

ID# 1321368042

CSE 225 (Data Structure)

North South University

Email: [email protected]

2

Page 3: Segment Tree

Segment Tree

Definition:

In computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It can be implemented as a dynamic structure.

3

Page 4: Segment Tree

Segment Trees

4

Page 5: Segment Tree

Sample Problem:

You are given n (1<=n<=100000) integers & 100000 query. Each query you have to change a value of an element Or you have to given the minimum value of a range.

5

Page 6: Segment Tree

Function Of segment Tree

6

Page 7: Segment Tree

Initial Functions:

We start with a segment arr[1 . . . n]. and every time we divide the current segment into two halves(if it has not yet become a segment of length 1), and then call the same procedure on both halves, and for each such segment we store the sum in corresponding node.

7

Page 8: Segment Tree

Initial Functions:

Page 9: Segment Tree

Initial Functions( in tree look):

9

Page 10: Segment Tree

Initial Functions(Algorithm):

#define mx 100001

int arr[mx];

int tree[mx*3];

void init(int node,int b,int e)

{

if(b==e)

{

tree[node]=arr[b];

return;

}

int Left=node*2;

int Right=node*2+1;

int mid=(b+e)/2;

init(Left,b,mid);

init(Right,mid+1,e);

tree[node]=tree[Left]+tree[Right];

} 10

Page 11: Segment Tree

Query Functions:

11

Page 12: Segment Tree

Query Functions:

Once the tree is initialized, how to get the sum using the initialized segment tree.Now,we use Query function.Here 3 types of cases can be happening.

12

Page 13: Segment Tree

Query Functions:

13

Page 14: Segment Tree

Query Functions:

case A:

if(b>=i&&e<=j);then the segment is :Releaventsegment.case B:

if (i > e || j < b);means current segment is outside of i-j,then its valuless.case C:

if case A and case B are not true and some of the parts is included in i-j , then we need to break the segment and take some of the parts.

14

Page 15: Segment Tree

Query Functions(Algorithm):

int query(int node,int b,int e,int i,int j)

{

if (i > e || j < b) return 0;

if (b >= i && e <= j) return tree[node];

int Left=node*2;

int Right=node*2+1;

int mid=(b+e)/2;

int p1=query(Left,b,mid,i,j);

int p2=query(Right,mid+1,e,i,j);

return p1+p2;

}15

Page 16: Segment Tree

Update Function:

Like tree initialization and query operations, update can also be done recursively. We are given an index which needs to update. Let new value be the value to be added. We start from root of the segment tree, and add new value to all nodes which have given index in their range. If a node doesn’t have given index in its range, we don’t make any changes to that node.

16

Page 17: Segment Tree

Update Function:

17

Page 18: Segment Tree

Update Function(Algorithm):

void update(int node,int b,int e,int i,int newvalue) {

if (i > e || i < b) return; if (b >= i && e <= i) {

tree[node]=newvalue;

return;

}int Left=node*2;

int Right=node*2+1;

int mid=(b+e)/2;

update(Left,b,mid,i,newvalue);

update(Right,mid+1,e,i,newvalue);

tree[node]=tree[Left]+tree[Right];

}18

Page 19: Segment Tree

Time Complexity:

Function Name: Time:

1.Initial Function O(n log n)

2.Query Function O(log n)

3.Update Function O(log n)

19

Page 20: Segment Tree

Applications:

Using segment trees we get an <O(N), O(log N)> algorithm. Segment trees are very powerful, not only because they can be used for RMQ(Range Minimum Query). They are a very flexible data structure, can solve even the dynamic version of RMQ problem, and have numerous applications in range searching problems.

20

Page 21: Segment Tree

21

Page 22: Segment Tree

Time to run code……..

22

Page 23: Segment Tree

Thank You !

23