Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 SEGMENT TREES

2 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]

3 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

4 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 є T r, if high[int[x]] – low[int[x]] > 1, then A left subtree T left[x] and a right subtree T right[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 є T y, int[x]  int[y]

5 SEGMENT TREES Leaf nodes of T y 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).

6 The Segment Tree T(1,17) 1-22-33-44-55-66-77-88-99-1010-1111-1212-1313-1414-1515-1616-17 1-33-55-77-99-1111-1313-1515-17 1-5 5-99-1313-17 1-99-17 1-17

7 The Segment Tree T(4,15) 7-88-910-1111-1213-1414-15 4-55-66-77-99-1010-1212-1313-15 4-6 6-99-1212-15 4-99-15 4-15

8 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)

9 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]

10 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)

11 Insertion to a Segment Tree CASE (1) is mutually exclusive with CASE (2) and CASE (3) (3) (1) (2)&(3) (3) (2) int[right[x]] int[x] int[left[x]] (2)&(3)

12 Operation of INSERT(root[T], i) Corresponds to a tour T of the following structure An initial path P INIT from the root to a fork node x * P INIT may be empty. (e.g. x * = root[T] ) Two paths P L & P R may stem form the fork node x * P L & P R are paths in T left[x*] & T right[x*], respectively Either, interval i is allocated entirely to the fork node x * In this case P L & P R are both empty. Or, all right children of nodes on P L, which are not on P L all left children of nodes on P R, which are not on P R identify the fragmentation(allocation) of the interval i

13 Operation of INSERT(root[T], i) For all x  P IN –Either (2) or (3) holds (not both) such that –Either i  int[left[x]] or i  int[right[x]] OR int[x] ii

14 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]] int[x * ] i i

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

16 Operation of INSERT(root[T], i) (3) holds for all nodes on P L since –high[i] > mid(int[x * ]) ≥ high[int[x]] > mid(int[x]) for all x  T left[x*] If only (3) holds for a node x  P L (x≠z), we have –mid(int[x]) < low[z] < high[int[x]] –P L will go right due to INSERT(right[x],i) int[x]

17 Operation of INSERT(root[T], i) If both (2) & (3) holds for a node x  P L (x≠z) –low[int[x]] < low[i] < mid(int[x]) –Path P L 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] high[int[left[x]]] int[x] int[right[x]] i

18 The Segment Tree T(1,17) 1-22-33-44-55-66-77-88-99-1010-1111-1212-1313-1414-1515-1616-17 1-33-55-77-99-1111-1313-1515-17 1-5 5-99-1313-17 1-99-17 1-17 a : [5,11] b : [7,13] i : [10,12] {a}{b} {a}

19 The Segment Tree T(1,17) 1-22-33-44-55-66-77-88-99-1010-1111-1212-1313-1414-1515-1616-17 1-33-55-77-99-1111-1313-1515-17 1-5 5-99-1313-17 1-99-17 1-17 mid = 5 mid = 9[5-17]


Download ppt "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."

Similar presentations


Ads by Google