25
SEGMENT TREE By Sindhuja Kumar M.Tech IT- 1 st year Advanced Data Structure

Segment tree

Embed Size (px)

Citation preview

Page 1: Segment tree

SEGMENT TREEBy

Sindhuja KumarM.Tech IT- 1st year

Advanced Data Structure

Page 2: Segment tree

Overview of Segment Tree

• What is Segment Tree?• Structure of Segment Tree• Operations of Segment Tree

• Construct • Update• Query

• Deletion of Segment Tree• Time complexity• Application

Page 3: Segment tree

What is Segment Tree?

• The Segment Tree (or Tree intervals) is a data structure that allows us to store information in the form of intervals, or segments. It can be used for making update/query operations upon array intervals in logarithmical time.

• Segment tree for the interval [i, j] in the following manner:– The first node will hold the information for the interval

[i, j]. – If i<j the left and right son will hold the information for

the intervals [i, (i+j)/2] and [(i+j)/2+1, j].

Page 4: Segment tree

Structure of Segment Tree

• Segment tree is a rooted binary tree. Each node x is assigned a static interval int[x]

• Recursive construction of T(L,R), where 1≤ L < R ≤ N are integers:– Root r : int[r] = [L,R]

– For each node x є Tr , if high[int[x]] – low[int[x]] > 1, then

• A left subtree Tleft[x] and a right subtree Tright[x] such that int[left[x]] = [ low[int[x]] , mid[int[x]] ]int[right[x]] = [ mid[int[x]] , high[int[x]] ] wheremid[int[x]] = (low[int[x]] + high[int[x]]) / 2

Page 5: Segment tree

Example of Segment tree• Segment Tree with array index 0 to 5

[0-5]

[3-5][0-2]

[2-2][0-1] [3-4] [5-5]

[0-0] [1-1] [3-3] [4-4]

Page 6: Segment tree

Example for Segment treeAfter the insertion of values in the tree..

36

9 27

4 5 16 11

1 3 7 9

[0-5]

[3-5][0-2]

[0-1]

[0-0] [1-1]

[3-4]

[3-3]

[5-5]

[4-4]

[2-2]

This node represent sum of index from 0 to 5

Height of the

segment tree is

O(log 2 n)

Page 7: Segment tree

Operations of Segment Tree

• A segment trees has only three operations: Construct, Update, Query.

• Construct tree: To init the tree segments or intervals values.• Update tree: To update value of an interval or segment.• Query tree: To retrieve the value of an interval or segment.

Page 8: Segment tree

Construct operation

• We can construct a segment tree either top-down or bottom-up. Top-down construction is recursive; the base cases are the leaf nodes.

• All levels of the constructed segment tree will be completely filled except the last level. Also, the tree will be a Full Binary Tree because we always divide segments in two halves at every level.

• Since the constructed tree is always full binary tree with n leaves,

there will be n-1 internal nodes. So total number of nodes will be 2*n – 1.

Page 9: Segment tree

Condition for Inserting the elements

• Initial Invocation : INSERT(root[T], i)INSERT(x, i)

if low[i] ≤ low[int[x]] and high[i] ≥ high[int[x]] then C[x] ← C[x] + 1Z[x] ← Z[x] {i}

elsemidx ← (low[int[x]] + high[int[x]]) / 2 if low[i] < midx then

INSERT( left[x], i)if high[i] ≥ midx then

INSERT(right[x], i)

Case 1

Case 2

Case 3

Page 10: Segment tree

Construct operation (INSERT)Example:• Insert 1,3,5,7,9,11.

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

Page 11: Segment tree

Update Operation

• Like tree construction and query operations, update can also be done recursively. We are given an index which needs to updated.

• Let diff be the value to be added. We start from root of the segment tree, and add diff 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.

Page 12: Segment tree

Update operationExample:• Update the node 9 to 10.

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-1]

Page 13: Segment tree

Update operation

• After updating of node 9 to 1037

9 28

4 5 17 11

1 3 7 10

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

Page 14: Segment tree

Query Operation

• To query a segment tree is to use it to determine a function of a range in the underlying array (in this case, the minimum element of that range).

• Condition:• If the node lies in the query interval, add it to answer .• If the node intersect with the query interval, go down.

Page 15: Segment tree

Pseudo-Code

Pseudo-Code for Query operation:

int query(node, l, r){ if range of node is within l and r return value in node elseif range of node is completely outside l and r return 0 }

Page 16: Segment tree

Query operation (Example 1)• Query the value in index of (3-5)

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

Since the right child contains the index (3-5), the query operation is

performed in right sub-tree

Initially the query operation is performed in the left child, since its index is (0-2) no further search is performed.

Page 17: Segment tree

Query operation (Example 2)• Query the value in index of (2-3)

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

Since the right child contains the interval (3-3), the query operation is also performed in right sub-tree

Initially the query operation is performed in the left child, it is found that index (2-2) is present in left sub tree so its value is queried..

Page 18: Segment tree

Query operation (Example 2)

• Cont..

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

In the next stage, the value of (2,2) is found in the right

child of left sub-tree

In the right sub-tree, the left child contain index (3-4)

so further the query operation is

performed

Page 19: Segment tree

Query operation (Example 2)

• Cont..

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

In the right sub-tree, the index of

(3,3) is found

Page 20: Segment tree

Query operation (Example 2)

• Cont..

36

9 27

4 5 16 11

1 3 7 9

[0-0] [1-1]

[2-2]

[3-3] [4-4]

[5-5]

[0-5]

[0-2]

[0-1]

[3-5]

[3-4]

The query value of index (2-3) is 12

Page 21: Segment tree

Deletion in Segment Trees

• Initial invocation : DELETE (root[T], i)DELETE(x, i)if low[i] ≤ low[int[x]] and high[i] ≥ high[int[x]] then C[x] ← C[x] - 1Z[x] ← Z[x] - {i}

elsemidx ← (low[int[x]] + high[int[x]]) / 2 if low[i] < midx thenDELETE( left[x], i)if high[i] ≥ midx thenDELETE(right[x], i)

Page 22: Segment tree

Delete operation• After deleting the values in the leaf node:

[0-5]

[3-5][0-2]

[2-2][0-1] [3-4] [5-5]

[0-0] [1-1] [3-3] [4-4]

Page 23: Segment tree

Time Complexity

• Construct operation - Interval can be added/deleted in O(n) time.

• Update operation – O(log n) time.

• Query operation – O(log n) time.

Page 24: Segment tree

Applications

• Applications of the segment tree are in the areas of – computational geometry, and –geographic information systems.

• Static and Dynamic RMQ(Range Minimum Query)

Page 25: Segment tree

THANK YOU…