Data Structure III. Segment Trees

Similar documents
Organizing Spatial Data

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Binary Heaps in Dynamic Arrays

Algorithms. AVL Tree

Point Enclosure and the Interval Tree

Computational Geometry

Intro To Segment Trees. Victor Gao

Sorting and Selection

Exercise 1 : B-Trees [ =17pts]

V Advanced Data Structures

More about The Competition

Leftist Heaps and Skew Heaps. (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring / 41

Programming II (CS300)

V Advanced Data Structures

Recursion and Structural Induction

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

II (Sorting and) Order Statistics

Priority Queues and Binary Heaps

We assume uniform hashing (UH):

CS 337 Project 1: Minimum-Weight Binary Search Trees

1. Meshes. D7013E Lecture 14

CS525 Winter 2012 \ Class Assignment #2 Preparation

Red-Black trees are usually described as obeying the following rules :

Motivation for B-Trees

Math 485, Graph Theory: Homework #3

CSCI2100B Data Structures Trees

18.3 Deleting a key from a B-tree

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

CMSC 754 Computational Geometry 1

CMSC351 - Fall 2014, Homework #2

INF2220: algorithms and data structures Series 1

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Paths, Flowers and Vertex Cover

COMP 250 Fall recurrences 2 Oct. 13, 2017

Lecture 26. Introduction to Trees. Trees

B-Trees and External Memory

Total Points: 60. Duration: 1hr

COMP 352 FALL Tutorial 10

Computational Geometry

B-Trees and External Memory

Transform & Conquer. Presorting

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

13.4 Deletion in red-black trees

Lecture 16 AVL Trees

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

Exam Sample Questions CS3212: Algorithms and Data Structures

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Trees, Part 1: Unbalanced Trees

TREES. Trees - Introduction

W4231: Analysis of Algorithms

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Algorithms. Red-Black Trees

implementing the breadth-first search algorithm implementing the depth-first search algorithm

Binary Decision Diagrams

Data Structures Week #6. Special Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Lecture 3: B-Trees. October Lecture 3: B-Trees

Problem Set 5 Solutions

COMP3121/3821/9101/ s1 Assignment 1

Data Structures and Algorithms

Lecture 16 Notes AVL Trees

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Building Java Programs

Chapter 11: Graphs and Trees. March 23, 2008

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees, Binary Search Trees

The Resolution Algorithm

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Binary heaps (chapters ) Leftist heaps

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

March 20/2003 Jayakanth Srinivasan,

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

Balanced Search Trees. CS 3110 Fall 2010

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

THE EULER TOUR TECHNIQUE: EVALUATION OF TREE FUNCTIONS

Topic: Heaps and priority queues

Binary Trees

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Data Structures in Java

Ensures that no such path is more than twice as long as any other, so that the tree is approximately balanced

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

CSE 373 OCTOBER 25 TH B-TREES

CSE 530A. B+ Trees. Washington University Fall 2013

Chapter 4: Trees. 4.2 For node B :

Figure 4.1: The evolution of a rooted tree.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Massive Data Algorithmics

Algorithms and Data Structures CS-CO-412

Greedy Algorithms CHAPTER 16

Transcription:

Data Structure III Segment Trees

Section 1 Segment Trees

Introduction We introduce another data structural, segment trees, to support the interval operation. Compared with the block list, it is usually more efficient. We will illustrate the idea by showing how to support the following operations on an integer array A[1, 2,..., n]. Update (i, j, k). Setting A i, A i+1,..., A j to k. Sum (i, j). Return the value of j l=i A l. By using a segment tree, we can perform each operation in time O(log n), with an O(n) pre-processing (construction) time.

Definition The segment tree is a binary tree. Each node in the tree represents an interval [i, j] where 1 i j n. If the interval [i, j] represented by a node satisfies i j, then it has exactly two child nodes. The left node represents interval [i, i+j i+j 2 ], and the right node represents interval [ 2 + 1, j]. Otherwise, if some node represents an interval [i, i], then it is a leaf and has no child nodes.

Construction We let the root represent interval [1, n]. Then all other nodes can be constructed recursively. It can be shown that the height of the segment tree is O(log n) Moreover, there are at most 4n nodes in the segment tree, and hence the construction takes linear time, and linear space.

Covering An Interval Given any interval [i, j] for 1 i j n, there exists an algorithm that finds O(log n) nodes that partitions the interval [i, j], running in time O(log n). We describe the procedure COVER(r, i, j), that takes i, j as the input interval, and r as the root of the segment tree. It returns a set S of nodes that represent the partition of [i, j].

Covering An Interval: Algorithm Step 1. If r represents [i, j], then return {r}. Otherwise, go to Step 2. Step 2. Suppose r represents [L, R], and define M := L+R 2. If j M, then it means that [i, j] lies in the left child of r entirely, and we return COVER(r.leftChild, i, j). If M + 1 i, then it means that [i, j] lies in the right child of r entirely, and we return COVER(r.rightChild, i, j). Otherwise, return COVER(r.leftChild, i, M) COVER(r.rightChild, M + 1, j).

Covering An Interval: Analysis Obviously, the algorithm returns a set of nodes representing intervals that partition [i, j]. We shall show that the algorithm runs in O(log n) time, and hence the size of the output is O(log n). If the algorithm always goes into the first two if branches of Step 2, then it is immediate that the algorithm runs in O(log n) time.

Then we look at the case when the algorithm goes into the otherwise branch of the Step 2, in which case COVER(r.leftChild, i, M) and Cover(r.rightChild, M + 1, j) are invoked. We observe that if for all such cases, either r.leftchild represents [i, M], or r.rightchild represents [M + 1, j], then the algorithm still runs in O(log n).

It remains to consider the case that in the otherwise branch, neither r.leftchild represents [i, M], nor r.rightchild represents [M + 1, j]. We claim that this can only happen once among all the invocation of the algorithm. To see it, we consider the first time this case happends. Then r.leftchild represents an interval that ends at M, and r.rightchild represents an interval that starts at M + 1. One can verify that the subsequent invocations cannot fall in this case again.

Maintaining Sum Now we are to define some variable for each node in order to support the desired operations. We maintain a variable, sum, in each node. For a node u, u.sum denotes the sum of the elements in the interval that u represents.

Query Suppose we have maintained the sum variables for all nodes. Then for a query (i, j), we can invoke COVER(root, i, j) to find O(log n) nodes that represent a partition of [i, j], and then we sum up the sum variable in the nodes to get the answer.

Issues in Update However, when it comes to the update, it seems updating the O(log n) intervals returned by the COVER procedure is not sufficient. As an example, suppose we are going to update (1, n, x), which asks us to set every element in the segment tree to be x. To make every sum variable correct, we have to go through the tree, and it takes O(n).

Lazy Variables Can we just make some of the sum variables correct? Yes, we can. Actually, we can just update O(log n) nodes in the update proceudre. For each node, we give it an additional boolean variable lazy, and an additional integer variable lvalue which intends to mean lazy value. Whenever the lazy variable indicates true, then the sum value of the current node, as well as its child nodes, may not yet be correctly set, but they are supposed to be set to lvalue.

Push Down The Lazy Value For some node r, the procedure PUSHDOWN(r) is used to update the sum field of r as well as the other information for child nodes of r, using the lazy value of it. PUSHDOWN(r) If r.lazy = true, then set r.sum to be r.lvalue times the length of the interval that r represents; if r is not a leaf, then set the both the lazy variable of the two child nodes to be true, set both the lvalue variable of the two child nodes to be r.lvalue; set r.lazy = false.

Lazy Update Combining with the idea introduced in COVER, we describe the update procedure UPDATE(r, i, j, k) that runs in time O(log n), where r is the root node. The procedure will set A i, A i+1,..., A j to k. Step 1. If r represents the interval [i, j] exactly, then set r.lazy := true and r.lvalue := k. Return. Step 2. Otherwise, PUSHDOWN(r). Step 3. Suppose r represents interval [L, R], and define M := L+R 2. If j M then UPDATE(r.leftChild, i, j, k). If M + 1 i then UPDATE(r.rightChild, i, j, k). Otherwise, UPDATE(r.leftChild, i, M, k) and UPDATE(r.rightChild, M + 1, j, k). Step 4. PUSHUP(r).

Push Up The Update We elaborate the PUSHUP procedure used in Step 4. Since we update the sum of some descendants of r, we need to update the sum variable of r after updating the descendants. We use the PUSHUP procedure to do so, which is used in Step 5 of the UPDATE procedure. PUSHUP(r) If r is a leaf, then return. Otherwise, PUSHDOWN(r.leftChild), PUSHDOWN(r.rightChild), and set r.sum := r.leftchild.sum + r.rightchild.sum. We note that the PUSHDOWN in the procedure is necessary to get the correct sum value of the child nodes.

Query Procedure Since we use lazy update, we need to change the query procedure QUERY(r, i, j) accordingly, where r is the root, and the procedure returns the sum of elements of indices from i to j. Step 1. PUSHDOWN(r). Step 2. If r represents the interval [i, j] exactly, then return r.sum. Return. Step 3. Otherwise, suppose r represents interval [L, R], and define M := L+R 2. Step 4. If j M then return QUERY(r.leftChild, i, j). If M + 1 i then return QUERY(r.rightChild, i, j). Otherwise, return QUERY(r.leftChild, i, M) + QUERY(r.rightChild, M + 1, j).

Conclusion We have described the update and query procedure with lazy update technique. We note that the lazy update technique is necessary in order to achieve O(log n) time complexity. The segment tree is not limited to support the example operations discussed so far. To support other kinds of operations, we note that it is usually only necessary to change the PUSHUP and PUSHDOWN procedure as well as the variables defined in the node. You can explore how the segment trees support other kinds of operations in the exercises.