SEGMENT TREES

1 / 19

# SEGMENT TREES - PowerPoint PPT Presentation

SEGMENT TREES. SEGMENT TREES. Originally introduced by Bentley(1977) Handle intervals on the real line whose end-points belong to a fixed set of N abscissae. A static structure with respect to the abscissae. i.e. Does not support insertions and deletions of abscissae.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'SEGMENT TREES' - heath

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### SEGMENT TREES

SEGMENT TREES
• Originally introduced by Bentley(1977)
• Handle intervals on the real line whose end-points belong to a fixed set of N abscissae.
• A static structure with respect to the abscissae. i.e. Does not support insertions and deletions of abscissae.
• The abscissae can be normalized by replacing each of them by its rank in their left-to-right order.
• Hence, without loss of generality, we may consider these abscissae as the integers in the range [1,N]
SEGMENT TREES
• Consider a set of N abscissae on the x-axis normalized to the integers [1,N] by their rank.
• These N abscissae determine N-1 elementary intervals [i,i+1], for i = 1,2,….,N-1
SEGMENT TREES
• 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 

• For each node x є Ty , int[x] int[y]
SEGMENT TREES
• Leaf nodes of Ty stores elementary intervals: [i,i+1] for i = low[int[y]] ,……, high[int[y]]-1
• Intervals of the nodes of T(L,R) :

Standard intervals of T(L,R)

• T(L,R) is balanced
• All leafs belong to two consecutive levels
• Heigth(depht) : h(T(L,R)) = lg(R-L)

h(T(1,N)) = lg(N-1)

• An arbitrary interval i  [1,N] will be partitioned into at most lg(N-1) + lg(N-1) - 2 = 2h(T)-2 standard intervals of T(1,N).
The Segment Tree T(1,17)

1-17

1-9

9-17

5-9

9-13

13-17

1-5

1-3

3-5

5-7

7-9

9-11

11-13

13-15

15-17

1-2

2-3

3-4

4-5

5-6

6-7

7-8

8-9

9-10

10-11

11-12

12-13

13-14

14-15

15-16

16-17

The Segment Tree T(4,15)

4-15

4-9

9-15

6-9

9-12

12-15

4-6

4-5

5-6

6-7

7-9

9-10

10-12

12-13

13-15

7-8

8-9

10-11

11-12

13-14

14-15

Insert Operation in Segment Trees
• Pseudocode for inserting an interval i to a segment tree T
• 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)

Insert Operation in Segment Trees
• Z[x] : Set of intervals allocated to node x

Set of intervals overlapping with the standard interval int[x]

• C[x] : Cardinality of the set I[x]

Number of intervals overlapping with the standard interval int[x]

Deletion in Segment Trees
• Pseudocode for deleting an interval i from a segment tree T
• Initial invocation : DELETE (root[T], i)

DELETE(x, i)

if low[i] ≤ low[int[x]] and high[i] ≥ hig[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)

Insertion to a Segment Tree

(1)

• CASE (1) is mutually exclusive with CASE (2) and CASE (3)

(3)

(3)

(2)&(3)

(2)&(3)

(2)&(3)

(2)

(2)

int[x]

int[left[x]]

int[right[x]]

Operation of INSERT(root[T], i)
• Corresponds to a tour T of the following structure
• An initial path PINITfrom the root to a fork nodex*

PINITmay be empty. (e.g. x* = root[T] )

• Two paths PL & PR may stem form the fork node x*

PL & PR are paths in Tleft[x*] & Tright[x*] , respectively

• Either, interval i is allocated entirely to the fork node x*

In this case PL & PR are both empty.

Or, all right children of nodes on PL, which are not on PL

all left children of nodes on PR, which are not on PR

identify the fragmentation(allocation) of the interval i

Operation of INSERT(root[T], i)
• For all x  PIN
• Either (2) or (3) holds (not both) such that
• Either i  int[left[x]] or i  int[right[x]]

i

i

OR

int[x]

int[x]

Operation of INSERT(root[T], i)
• At the fork node x*
• Either (1) holds such that int[x] = i
• Or both (2) & (3) holds such that
• low[int[x]] ≤ low[i] < mid(int[x]) < high[i] ≤ high[int[x]]

i

int[x*]

i

int[x*]

Operation of INSERT(root[T], i)
• Left path PL from the fork node x*
• Discussion for right path PR is similar (dual)
• Traversal on PL corresponds to locating the point low[i] in the standard intervals of Tleft[x*]
• Traversal continues until allocating node z where
• low[int[z]] = low[i]
• Search will terminate on PL due to (1) since:

i

int[z]

Operation of INSERT(root[T], i)
• (3) holds for all nodes on PL since
• high[i] > mid(int[x*]) ≥ high[int[x]] > mid(int[x])

for all x  Tleft[x*]

• If only (3) holds for a node x  PL (x≠z), we have
• mid(int[x]) < low[z] < high[int[x]]
• PL will go right due to INSERT(right[x],i)

int[x]

Operation of INSERT(root[T], i)
• If both (2) & (3) holds for a node x  PL (x≠z)
• low[int[x]] < low[i] < mid(int[x])
• Path PL will go left due to INSERT(left[x],i)
• Node right[x] will be allocated in INSERT(right[x],i) by (1) since low[i] < low[int[left[x]]] and high[i] > high[int[left[x]]]

i

int[x]

int[right[x]]

The Segment Tree T(1,17)

a : [5,11]

b : [7,13]

i : [10,12]

1-17

1-9

9-17

{a}

{b}

5-9

9-13

13-17

1-5

{b}

{a}

1-3

3-5

5-7

7-9

9-11

11-13

13-15

15-17

1-2

2-3

3-4

4-5

5-6

6-7

7-8

8-9

9-10

10-11

11-12

12-13

13-14

14-15

15-16

16-17

The Segment Tree T(1,17)

[5-17]

1-17

mid = 9

1-9

mid = 5

9-17

5-9

9-13

13-17

1-5

1-3

3-5

5-7

7-9

9-11

11-13

13-15

15-17

1-2

2-3

3-4

4-5

5-6

6-7

7-8

8-9

9-10

10-11

11-12

12-13

13-14

14-15

15-16

16-17