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.

Slides:



Advertisements
Similar presentations
INTERVAL TREE & SEGMENTATION TREE
Advertisements

Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006.
Introduction to Analysis of Algorithms CAS CS 330 Lecture 16 Shang-Hua Teng Thanks to Charles E. Leiserson and Silvio Micali of MIT for these slides.
Binary Search Trees Chapter 7 Objectives
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Chapter 61 Chapter 6 Index Structures for Files. Chapter 62 Indexes Indexes are additional auxiliary access structures with typically provide either faster.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Interval Trees.
Lecture X Augmenting Data Structures
Interval Trees CS302 Data Structures Modified from Dr Monica Nicolescu.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
2IL50 Data Structures Fall 2015 Lecture 9: Range Searching.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Binary Search Trees ©Robert E. Tarjan Dictionary: contains a set S of items, each with associated information. Operations: Access(x): Determine.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
CMPS 3130/6130 Computational Geometry Spring 2015
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Analysis of Algorithms CS 477/677
AA Trees.
Introduction to Algorithms
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
CMPS 3130/6130 Computational Geometry Spring 2017
Binary search tree. Removing a node
Binary Search Tree (BST)
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Red-Black Trees.
AVL Tree.
Tree.
Binary Tree Applications
Data Structures Balanced Trees CSCI
Red-Black Trees.
Height Balanced Trees 2-3 Trees.
CMSC 341 (Data Structures)
Chapter 14: Augmenting Data Structures
Algorithms and Data Structures Lecture VII
Binary Tree Traversals
AVL Search Tree put(9)
CS 583 Analysis of Algorithms
Introduction to Algorithms
Segment Tree and Its Usage for geometric Computations
Chapter 12&13: Binary Search Trees (BSTs)
Interval Trees CS302 Data Structures Modified from Dr Monica Nicolescu.
Data Structures Using C++ 2E
Presentation transcript:

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

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

The Segment Tree T(1,17)

The Segment Tree T(4,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 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)

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

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

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

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]

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]

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

The Segment Tree T(1,17) a : [5,11] b : [7,13] i : [10,12] {a}{b} {a}

The Segment Tree T(1,17) mid = 5 mid = 9[5-17]