SEGMENT TREE
By
Sindhuja Kumar
M.Tech IT- 1st year
Advanced Data Structure
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
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].
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]] ] where
mid[int[x]] =  (low[int[x]] + high[int[x]]) / 2 
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]
Example for Segment tree
After 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)
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.
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.
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] + 1
Z[x] ← Z[x]  {i}
else
midx ←  (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
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]
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.
Update operation
Example:
• 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]
Update operation
• After updating of node 9 to 10
37
9 28
4 5 17 11
1 3 7 10
[0-0] [1-1]
[2-2]
[3-3] [4-4]
[5-5]
[0-5]
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.
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
}
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.
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..
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
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
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
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] - 1
Z[x] ← Z[x] - {i}
else
midx ←  (low[int[x]] + high[int[x]]) / 2 
if low[i] < midx then
DELETE( left[x], i)
if high[i] ≥ midx then
DELETE(right[x], i)
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]
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.
Applications
• Applications of the segment tree are in the areas of
–computational geometry, and
–geographic information systems.
• Static and Dynamic RMQ(Range Minimum Query)
THANK YOU…

Segment tree

  • 1.
    SEGMENT TREE By Sindhuja Kumar M.TechIT- 1st year Advanced Data Structure
  • 2.
    Overview of SegmentTree • What is Segment Tree? • Structure of Segment Tree • Operations of Segment Tree • Construct • Update • Query • Deletion of Segment Tree • Time complexity • Application
  • 3.
    What is SegmentTree? • 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].
  • 4.
    Structure of SegmentTree • 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]] ] where mid[int[x]] =  (low[int[x]] + high[int[x]]) / 2 
  • 5.
    Example of Segmenttree • 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]
  • 6.
    Example for Segmenttree After 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)
  • 7.
    Operations of SegmentTree • 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.
  • 8.
    Construct operation • Wecan 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.
  • 9.
    Condition for Insertingthe 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] + 1 Z[x] ← Z[x]  {i} else midx ←  (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
  • 10.
    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]
  • 11.
    Update Operation • Liketree 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.
  • 12.
    Update operation Example: • Updatethe 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]
  • 13.
    Update operation • Afterupdating of node 9 to 10 37 9 28 4 5 17 11 1 3 7 10 [0-0] [1-1] [2-2] [3-3] [4-4] [5-5] [0-5]
  • 14.
    Query Operation • Toquery 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.
  • 15.
    Pseudo-Code Pseudo-Code for Queryoperation: 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 }
  • 16.
    Query operation (Example1) • 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.
  • 17.
    Query operation (Example2) • 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..
  • 18.
    Query operation (Example2) • 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
  • 19.
    Query operation (Example2) • 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
  • 20.
    Query operation (Example2) • 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
  • 21.
    Deletion in SegmentTrees • 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] - 1 Z[x] ← Z[x] - {i} else midx ←  (low[int[x]] + high[int[x]]) / 2  if low[i] < midx then DELETE( left[x], i) if high[i] ≥ midx then DELETE(right[x], i)
  • 22.
    Delete operation • Afterdeleting 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]
  • 23.
    Time Complexity • Constructoperation - Interval can be added/deleted in O(n) time. • Update operation – O(log n) time. • Query operation – O(log n) time.
  • 24.
    Applications • Applications ofthe segment tree are in the areas of –computational geometry, and –geographic information systems. • Static and Dynamic RMQ(Range Minimum Query)
  • 25.