Intro To Segment Trees. Victor Gao
|
|
- Mervyn Conley
- 5 years ago
- Views:
Transcription
1 Intro To Segment Trees Victor Gao
2 OUTLINE Interval Notation Range Query & Modification Segment Tree - Analogy Building a Segment Tree Query & Modification Lazy Propagation Complexity Analysis Implementation Tricks
3 GENERAL INFORMATION This lecture is relatively comprehensive and covers most of the basic topics related to segment trees. You may be overwhelmed by amount of information I'm giving you today, but there is no need to panic. You are NOT expected to memorize or fully understand the details during the lecture. Pay more attention to the high level concepts and abstractions instead. Once you understand these, you'll be able to figure out the details pretty easily, even with yourself.
4 INTERVAL NOTATION We will be using the following notation: [left, right) Mathematically, this represents an interval that is closed on the left end and open on the right end It includes numbers from left to right - 1 Reasons to use this convention: Personal preference, you don t necessarily have to follow it It simplifies certain operations Being able to represent an empty interval in a less awkward way, for example, [0, 0)
5 RANGE QUERY & MODIFICATION Consider the following problem: Given an array of integers, we have these two operations: 1) calculate the sum of the numbers in interval [i, j) (Query) 2) add x to every number in interval [i, j) (Modify)
6 RANGE QUERY & MODIFICATION Consider the following problem: Given an array of integers, we have these two operations: 1) calculate the sum of the numbers in interval [i, j) (Query) 2) add x to every number in interval [i, j) (Modify) Example: for array [0, 1, 2, 3] (indices start from 0) 1) Query [0, 2) gives the sum = 1 2) Add (Modify) 4 to [1, 3), the array becomes [0, 5, 6, 3] 3) Query [0, 4) gives the sum = 14
7 RANGE QUERY & MODIFICATION - PRIMITIVE APPROACH Obviously, there is one primitive approach: For each query, iterate through all elements in the interval and calculate the sum. For each modification, modify all elements in the interval one by one.
8 RANGE QUERY & MODIFICATION - PRIMITIVE APPROACH Obviously, there is one primitive approach: For each query, iterate through all elements in the interval and calculate the sum. For each modification, modify all elements in the interval one by one. Time Complexity: Query: O(n) Modification: O(n)
9 RANGE QUERY & MODIFICATION - PRIMITIVE APPROACH Obviously, there is one primitive approach: For each query, iterate through all elements in the interval and calculate the sum. For each modification, modify all elements in the interval one by one. Time Complexity: SLOW Query: O(n) Modification: O(n)
10 RANGE QUERY & MODIFICATION - PRIMITIVE APPROACH Obviously, there is one primitive approach: For each query, iterate through all elements in the interval and calculate the sum. For each modification, modify all elements in the interval one by one. Time Complexity: SLOW Query: O(n) Modification: O(n) Data Structure: Faster Q & M Trade (not much) Space for Time
11 SEGMENT TREE - INTRO Core Idea: add nodes that store aggregate statistics. Analogy: number of students in a high school: class A: 21 class B: 22 grade 10: 61 class C: 18 class A: 24 class B: 20 grade 11: 44 total: 152 class A: 22 class B: 25 grade 12: 47
12 SEGMENT TREE - INTRO How to calculate the total number of students in the following three classes? class A: 21 class B: 22 grade 10: 61 class C: 18 class A: 24 class B: 20 grade 11: 44 total: 152 class A: 22 class B: 25 grade 12: 47
13 SEGMENT TREE - INTRO What should we if do when a transfer student joins grade 10, class B? class A: 21 class B: 22 grade 10: 61 class C: 18 class A: 24 class B: 20 grade 11: 44 total: 152 class A: 22 class B: 25 grade 12: 47
14 SEGMENT TREE - INTRO A segment tree looks like this. It's a binary tree with the leaf nodes storing statistics for individual array elements. The non-leaf nodes are the additional layers we add to store statistics of the combined intervals. [0, 4): 6 [0, 2): 1 [2, 4): 5 0: 0 1: 1 2: 2 3: 3 *In this case, we store the sum of the elements in the interval in each node
15 SEGMENT TREE - INITIALIZATION A segment tree can be built from an array recursively: To initialize a leaf, simply copy the data over. To initialize a non-leaf, Initialize its children first. Then set its value to the sum of the values of the two children. (Assume we care about the sum here.)
16 SEGMENT TREE - QUERY Query(i, j): returns the sum of the numbers in interval [i, j) What are Query(0, 1), Query(2, 4) and Query(0, 4)? What is Query(0, 3)? (Hint: the high school example) [0, 4): 6 [0, 2): 1 [2, 4): 5 0: 0 1: 1 2: 2 3: 3
17 SEGMENT TREE - QUERY Basic idea: big intervals are made up by smaller ones. A query on an interval can be calculated as the sum of queries over some smaller intervals (that is present on the tree). QUERY(cur, [a, b)): // cur is current node if [a, b) contains cur.interval then return cur.value if cur is not a leaf then l <- cur.left_child, r <- cur.right_child if [a, b) intersects both l.interval and r.interval then return QUERY(l, [a, b)) + QUERY(r, [a, b)) else if [a, b) intersects l.interval then return QUERY(l, [a, b)) else if [a, b) intersects r.interval then return QUERY(r, [a, b))
18 SEGMENT TREE - MODIFY (SINGLE POINT) Modify(i, x): add x to the i th element in the array. Basic Idea: increment not only the corresponding leaf, but also re-calculate the values stored in its ancestors. MODIFYPoint(leaf, x): // generic version leaf.value <- leaf.value + x p <- leaf.parent while p!= null do p.value <- (p.left_child.value + p.right_child.value) p <- p.parent MODIFYPoint(leaf, x): // compact version while p!= null do // only works for sum+add x p.value <- p.value + x p <- p.parent
19 SEGMENT TREE - MODIFY AN INTERVAL? Modifying a single element is easy. What if we want to modify an interval instead? Primitive approach: call ModifyPoint multiple times. Very inefficient.
20 SEGMENT TREE - MODIFY AN INTERVAL? Suppose we have a segment tree with 128 leaves. We're doing the following operations in sequence: Add 1 to all numbers in interval [2, 128) Add 3 to all numbers in interval [4, 88) Add -2 to all numbers in interval [17, 55) Add 10 to all numbers in interval [100, 112) Add 5 to all numbers in interval [44, 84) Query [47, 48) What do you observe?
21 SEGMENT TREE - LAZY PROPAGATION Basic idea: delay the modifications until a point that you absolutely have to do them. In other words, modifications do not need to take effect immediately. We apply the changes to a node when the node is accessed next time. To implement this idea, we add another field flag to each node. Flag is used to store (cache) a pending modification. Two things we need to do when we touch a node with a non-null flag (a pending modification). Update the value. Pass down the flag. (Why?)
22 SEGMENT TREE - LAZY PROPAGATION Assume we have the same segment tree that stores interval sums. With lazy propagation, whenever a node is accessed, we check if its flag is null (or a nonzero value, depending on the implementation). If the flag is null - nothing happens. If the flag is not null: Update the value. Update the child nodes' flags. Set flag to null.
23 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). [0, 4): 6 [0, 2): 1 [2, 4): 5 [0, 1): 0 [1, 2): 1 [2, 3): 2 [3, 4): 3
24 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Add 1 to all elements [0, 4): 6 1 [0, 2): 1 [2, 4): 5 [0, 1): 0 [1, 2): 1 [2, 3): 2 [3, 4): 3
25 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 6 1 [0, 2): 1 [2, 4): 5 [0, 1): 0 [1, 2): 1 [2, 3): 2 [3, 4): 3
26 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 10 [0, 2): 1 1 [2, 4): 5 1 [0, 1): 0 [1, 2): 1 [2, 3): 2 [3, 4): 3
27 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 10 [0, 2): 1 1 [2, 4): 5 1 [0, 1): 0 [1, 2): 1 [2, 3): 2 [3, 4): 3
28 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 10 [0, 2): 3 [2, 4): 5 1 [0, 1): 0 1 [1, 2): 1 1 [2, 3): 2 [3, 4): 3
29 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 10 [0, 2): 3 [2, 4): 5 1 [0, 1): 0 1 [1, 2): 1 1 [2, 3): 2 [3, 4): 3
30 SEGMENT TREE - LAZY PROPAGATION EXAMPLE Here's the same segment tree we had. Modify([l, r), x): add x to elements in interval [l, r). Query([l, r)): calculate the sum of elements in interval [l, r). Query [0, 1) [0, 4): 10 [0, 2): 3 [2, 4): 5 1 [0, 1): 1 [1, 2): 1 1 [2, 3): 2 [3, 4): 3
31 SEGMENT TREE - PROPAGATE Propagate is a utility function that tries to update a node. If the node is not a leaf, it also passes the flag to the next level. Propagate will be used in the lazy version of Query and Modify. Propagate(cur): if cur.flag!= 0 then cur.val <- cur.val + cur.interval.length*cur.flag if cur is not a leaf then l <- cur.left_child r <- cur.right_child l.flag <- l.flag + cur.flag r.flag <- r.flag + cur.flag cur.flag <- 0
32 SEGMENT TREE - QUERY (LAZY) Query needs to be revised in order to maintain the lazy flags. QUERY(cur, [a, b)): Propagate(cur) if [a, b) contains cur.interval then return cur.value if cur is not a leaf then l <- cur.left_child r <- cur.right_child if [a, b) intersects both l.interval and r.interval then return QUERY(l, [a, b)) + QUERY(r, [a, b)) else if [a, b) intersects l.interval then return QUERY(l, [a, b)) else if [a, b) intersects r.interval then return QUERY(r, [a, b))
33 SEGMENT TREE - MODIFY (LAZY) Modify(cur, [a, b), x): Propagate(cur) if [a, b) contains cur.interval then cur.flag <- x else if cur is not a leaf then l <- cur.left_child r <- cur.right_child if [a, b) intersects l.interval then Modify(l, [a, b)) if [a, b) intersects r.interval then Modify(r, [a, b)) Propagate(l) Propagate(r) cur.value <- l.value + r.value
34 SEGMENT TREE - MODIFY (LAZY) Modify(cur, [a, b), x): Propagate(cur) if [a, b) contains cur.interval then cur.flag <- x else if cur is not a leaf then l <- cur.left_child r <- cur.right_child if [a, b) intersects l.interval then Modify(l, [a, b)) if [a, b) intersects r.interval then Modify(r, [a, b)) Propagate(l) These are very important!!! (Why?) Propagate(r) cur.value <- l.value + r.value
35 SEGMENT TREE - WHEN TO USE LAZY PROPAGATION Lazy propagation is usually only needed when you need to support both ranged query and modification. What about ranged modification + single point query? It depends. Sometimes you need lazy propagation. Sometimes you can turn it into a single point modification + single point query problem by converting an array into differences. For example, [5, 2, 7, 4] => [5, -3, 5, -3]
36 SEGMENT TREE - COMPLEXITY ANALYSIS For a segment tree built from an n-element array, the total number of nodes is n + n/2 + n/ = 2n - 1. Therefore the space complexity is O(n) The time complexity of building a segment tree is O(n) The height of the tree is O(log(n)) Both Query and Modify do constant work at each node, and on each level, they visit at most two nodes. Therefore the time complexity of Q/M is O(log(n))
37 SEGMENT TREE - GENERALIZATION Previously we've been talking about a segment tree that calculates interval sums and allows adding x to elements in an interval. We can easily support different kinds of aggregate statistics and operations by abstracting our pseudocode a step further. Notice that there are several operations we used in Propagate, Modify and Query: Updating a value with a flag. Let's make it a function merge_val_flag(val, flag, interval) Updating a flag with a flag passed down from above. Let's make it a function merge_flag_flag(flag1, flag2) Merging query results. Let's make it a function merge_val_val(val1, val2)
38 SEGMENT TREE - PROPAGATE (GENERALIZED) Here's the generalized version of Propagate: Propagate(cur): if cur.flag!= null then cur.val <- merge_val_flag(cur.val, cur.flag, cur.interval) if cur is not a leaf then l <- cur.left_child r <- cur.right_child l.flag <- merge_flag_flag(l.flag, cur.flag) r.flag <- merge_flag_flag(r.flag, cur.flag) cur.flag <- null
39 SEGMENT TREE - QUERY (GENERALIZED) QUERY(cur, [a, b)): Propagate(cur) if [a, b) contains cur.interval then return cur.value if cur is not a leaf then l <- cur.left_child r <- cur.right_child if [a, b) intersects both l.interval and r.interval then return merge_val_val(query(l, [a, b)), QUERY(r, [a, b))) else if [a, b) intersects l.interval then return QUERY(l, [a, b)) else if [a, b) intersects r.interval then return QUERY(r, [a, b))
40 SEGMENT TREE - MODIFY (GENERALIZED) Modify(cur, [a, b), x): Propagate(cur) if [a, b) contains cur.interval then cur.flag <- x else if cur is not a leaf then l <- cur.left_child r <- cur.right_child if [a, b) intersects l.interval then Modify(l, [a, b)) if [a, b) intersects r.interval then Modify(r, [a, b)) Propagate(l) Propagate(r) cur.value <- merge_val_val(l.value, r.value)
41 SEGMENT TREE - GENERALIZATION Define the utility functions as follows: merge_val_flag(val, flag, interval) => flag merge_flag_flag(flag1, flag2) => flag2 merge_val_val(val1, val2) => max(val1, val2) Now we have a segment tree that calculates the maximum element in an interval, and allows setting all elements in an interval to a specified value. However it is very important that your value and flag only use O(1) space.
42 SEGMENT TREE - IMPLEMENTATION How would you implement a segment tree? One way is to have pointers pointing to the child nodes, like you would do to many other tree structures. [0, 2) value flag left child right child Disadvantages? [0, 1) value flag left child right child [1, 2) value flag left child right child Cache Unfriendly Additional Space
43 SEGMENT TREE - IMPLEMENTATION We can make the data structure faster and easier to implement by requiring that the length of the original array to be a power of 2. It has some nice properties: Since every node in the tree now either has 2 children, or is a leaf, the structure can be be seen as a full binary tree, and stored in a 1-D array, similar to binary heap. For node with index i, node ((i - 1)/2) is its parent, node (2i + 1) is its left child and node (2i + 2) is its right child. No need to store the pointers. No need to store the intervals in nodes, for they can be calculated during the recursion or less efficiently by the index of the node.
Balanced Binary Search Trees. Victor Gao
Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such
More informationLeftist Heaps and Skew Heaps. (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring / 41
Leftist Heaps and Skew Heaps (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring 2018 1 / 41 Leftist Heaps A binary heap provides O(log n) inserts and O(log n) deletes but suffers from
More informationData Structure III. Segment Trees
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 informationRange Minimum Queries Part Two
Range Minimum Queries Part Two Recap from Last Time The RMQ Problem The Range Minimum Query (RMQ) problem is the following: Given a fixed array A and two indices i j, what is the smallest element out of
More informationAdvanced Database Systems
Lecture IV Query Processing Kyumars Sheykh Esmaili Basic Steps in Query Processing 2 Query Optimization Many equivalent execution plans Choosing the best one Based on Heuristics, Cost Will be discussed
More informationLecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?
Lecture 32 No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 1 Outline Introduction
More informationBalanced Search Trees
Balanced Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have
More informationOperations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.
Priority Queue, Heap and Heap Sort In this time, we will study Priority queue, heap and heap sort. Heap is a data structure, which permits one to insert elements into a set and also to find the largest
More informationCSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)
Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will
More informationModule 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.
The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012
More informationCOMP 250 Fall binary trees Oct. 27, 2017
The order of a (rooted) tree is the maximum number of children of any node. A tree of order n is called an n-ary tree. It is very common to use trees of order 2. These are called binary trees. Binary Trees
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationCOMP 250 Winter 2010 Exercises 6 - trees March 2010
Questions 1. Consider the polynomial 5y 2 3y + 2. (a) Write the polynomial as an expression tree that obeys the usual ordering of operations. Hint: to clarify the ordering for yourself, first write the
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More informationLecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
Lecture 5 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: Randomized Search Trees by Aragon & Seidel, Algorithmica 1996, http://sims.berkeley.edu/~aragon/pubs/rst96.pdf;
More informationCMSC351 - Fall 2014, Homework #2
CMSC351 - Fall 2014, Homework #2 Due: October 8th at the start of class Name: Section: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,
More informationLECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS
Department of Computer Science University of Babylon LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS By Faculty of Science for Women( SCIW), University of Babylon, Iraq Samaher@uobabylon.edu.iq
More informationOutline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.
Outline 1 Definition Computer Science 331 Red-Black rees Mike Jacobson Department of Computer Science University of Calgary Lectures #20-22 2 Height-Balance 3 Searches 4 Rotations 5 s: Main Case 6 Partial
More informationData Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===
Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA email: dsa1@csientuedutw === Homework submission instructions === For Problem 1, submit your source code, a Makefile to compile
More informationAlgorithms and Data Structures
Lesson 3: trees and visits Luciano Bononi http://www.cs.unibo.it/~bononi/ (slide credits: these slides are a revised version of slides created by Dr. Gabriele D Angelo) International
More informationCSE 5311 Notes 4a: Priority Queues
Chart on p., CLRS (binary, Fibonacci heaps) CSE Notes a: Priority Queues (Last updated 9// : AM) MAKE-HEAP INSERT MINIMUM EXTRACT-MIN UNION (MELD/MERGE) DECREASE-KEY DELETE Applications - sorting (?),
More informationCS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.
CS251-SE1 Midterm 2 Tuesday 11/1 8:00pm 9:00pm There are 16 multiple-choice questions and 6 essay questions. Answer the multiple choice questions on your bubble sheet. Answer the essay questions in the
More informationCSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 8 (Based on Paul Kube course materials) CSE 100 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs
More informationOutline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.
Amortized Analysis Outline for Today Euler Tour Trees A quick bug fix from last time. Cartesian Trees Revisited Why could we construct them in time O(n)? Amortized Analysis Analyzing data structures over
More informationCSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016
CSE373: Data Structures & lgorithms Lecture 5: Dictionary DTs; Binary Trees Linda Shapiro Spring 2016 Today s Outline nnouncements - Homework 1 due TODY at 11:59 pm - Homework 2 out (paper and pencil assignment)
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:
More informationCSL 201 Data Structures Mid-Semester Exam minutes
CL 201 Data tructures Mid-emester Exam - 120 minutes Name: Roll Number: Please read the following instructions carefully This is a closed book, closed notes exam. Calculators are allowed. However laptops
More informationComputational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs
Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in
More informationCOMP 250 Fall priority queues, heaps 1 Nov. 9, 2018
COMP 250 Fall 2018 26 - priority queues, heaps 1 Nov. 9, 2018 Priority Queue Recall the definition of a queue. It is a collection where we remove the element that has been in the collection for the longest
More informationSorting and Selection
Sorting and Selection Introduction Divide and Conquer Merge-Sort Quick-Sort Radix-Sort Bucket-Sort 10-1 Introduction Assuming we have a sequence S storing a list of keyelement entries. The key of the element
More informationMaintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater
CS61B, Summer 2002 Lecture #8 Barath Raghavan UC Berkeley Topics: Binary Search Trees, Priority queues 1 Binary search trees (BSTs) Represented as ordinary binary trees Maintain binary search tree property
More informationLecture 6: Analysis of Algorithms (CS )
Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black
More informationSFU CMPT Lecture: Week 9
SFU CMPT-307 2008-2 1 Lecture: Week 9 SFU CMPT-307 2008-2 Lecture: Week 9 Ján Maňuch E-mail: jmanuch@sfu.ca Lecture on July 8, 2008, 5.30pm-8.20pm SFU CMPT-307 2008-2 2 Lecture: Week 9 Binary search trees
More informationCOMP 250 Fall recurrences 2 Oct. 13, 2017
COMP 250 Fall 2017 15 - recurrences 2 Oct. 13, 2017 Here we examine the recurrences for mergesort and quicksort. Mergesort Recall the mergesort algorithm: we divide the list of things to be sorted into
More informationLab Exercise 8 Binary Search Trees
Lab Exercise 8 Binary Search Trees A binary search tree is a binary tree of ordered nodes. The nodes are ordered by some key value, for example: alphabetic or numeric. The left subtree of every node (if
More informationLecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1
Lecture 34 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture33/*.* In order to compile these files, also need bintree.h from last class. Project 7 posted. Due next week Friday, but
More informationFigure 4.1: The evolution of a rooted tree.
106 CHAPTER 4. INDUCTION, RECURSION AND RECURRENCES 4.6 Rooted Trees 4.6.1 The idea of a rooted tree We talked about how a tree diagram helps us visualize merge sort or other divide and conquer algorithms.
More informationCMSC 341 Leftist Heaps
CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary
More informationCSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015 Where we are Studying the absolutely essential ADTs of computer science and classic data structures
More informationBinary Search Trees Treesort
Treesort CS 311 Data Structures and Algorithms Lecture Slides Friday, November 13, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009
More informationAn AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time
B + -TREES MOTIVATION An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations finish within O(log N) time The theoretical conclusion
More informationCS115 - Module 8 - Binary trees
Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Section 14. Binary arithmetic expressions Operators such as +,,, and take two arguments, so we call them binary operators.
More informationWriteup for first project of CMSC 420: Data Structures Section 0102, Summer Theme: Threaded AVL Trees
Writeup for first project of CMSC 420: Data Structures Section 0102, Summer 2017 Theme: Threaded AVL Trees Handout date: 06-01 On-time deadline: 06-09, 11:59pm Late deadline (30% penalty): 06-11, 11:59pm
More informationCSCE 2014 Final Exam Spring Version A
CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers
More informationCS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee
CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee 2 Today s Topics: 1. Binary tree 2. Heap Priority Queue Emergency Department waiting room operates as a priority queue: patients are sorted
More informationCSE 373 MAY 10 TH SPANNING TREES AND UNION FIND
CSE 373 MAY 0 TH SPANNING TREES AND UNION FIND COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend HW5 out tomorrow
More informationBalanced Trees Part One
Balanced Trees Part One Balanced Trees Balanced search trees are among the most useful and versatile data structures. Many programming languages ship with a balanced tree library. C++: std::map / std::set
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses
More informationModule 8: Range-Searching in Dictionaries for Points
Module 8: Range-Searching in Dictionaries for Points CS 240 Data Structures and Data Management T. Biedl K. Lanctot M. Sepehri S. Wild Based on lecture notes by many previous cs240 instructors David R.
More informationDiscussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked
More informationCS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))
CS61A Notes 02b Fake Plastic Trees Box and Pointer Diagrams QUESTIONS: Evaluate the following, and draw a box-and-pointer diagram for each. (Hint: It may be easier to draw the box-and-pointer diagram first.)
More information18.3 Deleting a key from a B-tree
18.3 Deleting a key from a B-tree B-TREE-DELETE deletes the key from the subtree rooted at We design it to guarantee that whenever it calls itself recursively on a node, the number of keys in is at least
More informationData Structure. IBPS SO (IT- Officer) Exam 2017
Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data
More information1 The range query problem
CS268: Geometric Algorithms Handout #12 Design and Analysis Original Handout #12 Stanford University Thursday, 19 May 1994 Original Lecture #12: Thursday, May 19, 1994 Topics: Range Searching with Partition
More informationBasic Data Structures (Version 7) Name:
Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)
More informationXML Query Processing. Announcements (March 31) Overview. CPS 216 Advanced Database Systems. Course project milestone 2 due today
XML Query Processing CPS 216 Advanced Database Systems Announcements (March 31) 2 Course project milestone 2 due today Hardcopy in class or otherwise email please I will be out of town next week No class
More informationCSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019
CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees Ruth Anderson Winter 2019 Today Dictionaries Trees 1/23/2019 2 Where we are Studying the absolutely essential ADTs of
More informationCS127: B-Trees. B-Trees
CS127: B-Trees B-Trees 1 Data Layout on Disk Track: one ring Sector: one pie-shaped piece. Block: intersection of a track and a sector. Disk Based Dictionary Structures Use a disk-based method when the
More informationFinal Examination CSE 100 UCSD (Practice)
Final Examination UCSD (Practice) RULES: 1. Don t start the exam until the instructor says to. 2. This is a closed-book, closed-notes, no-calculator exam. Don t refer to any materials other than the exam
More informationCS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.
CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section. 1. Trees a) For the binary tree shown below, what are the preorder, inorder and post order
More informationChapter 12: Indexing and Hashing. Basic Concepts
Chapter 12: Indexing and Hashing! Basic Concepts! Ordered Indices! B+-Tree Index Files! B-Tree Index Files! Static Hashing! Dynamic Hashing! Comparison of Ordered Indexing and Hashing! Index Definition
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 5 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017 In Lecture 4... Sorted Lists Circular Lists Linked Lists on Arrays Today 1 2 3 4 5 6 Assume that we want to memorize a
More informationUNIVERSITY REGULATIONS
CPSC 221: Algorithms and Data Structures Midterm Exam, 2013 February 15 Name: Student ID: Signature: Section (circle one): MWF(201) TTh(202) You have 60 minutes to solve the 5 problems on this exam. A
More informationCSCI Trees. Mark Redekopp David Kempe
CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows
More informationFriday Four Square! 4:15PM, Outside Gates
Binary Search Trees Friday Four Square! 4:15PM, Outside Gates Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set.
More information403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke
403: Algorithms and Data Structures Heaps Fall 20 UAlbany Computer Science Some slides borrowed by David Luebke Birdseye view plan For the next several lectures we will be looking at sorting and related
More informationCOMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University
COMP251: Algorithms and Data Structures Jérôme Waldispühl School of Computer Science McGill University About Me Jérôme Waldispühl Associate Professor of Computer Science I am conducting research in Bioinformatics
More informationQuestions from the material presented in this lecture
Advanced Data Structures Questions from the material presented in this lecture January 8, 2015 This material illustrates the kind of exercises and questions you may get at the final colloqium. L1. Introduction.
More informationTrees, Part 1: Unbalanced Trees
Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more
More informationCS102 Binary Search Trees
CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one
More informationChapter 12: Indexing and Hashing
Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree Index Files Static Hashing Dynamic Hashing Comparison of Ordered Indexing and Hashing Index Definition in SQL
More informationCS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial
Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for
More informationHeaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic
1 Heaps Outline and Required Reading: Heaps (.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Heap ADT 2 Heap binary tree (T) that stores a collection of keys at its internal nodes and satisfies
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 B Cynthia Lee Topics: Priority Queue Linked List implementation Sorted Unsorted Heap structure implementation TODAY S TOPICS NOT ON THE MIDTERM 2 Some priority queue
More informationData Structures. Oliver-Matis Lill. June 6, 2017
June 6, 2017 Session Structure First I will hold a short lecture on the subject Rest of the time is spent on problem solving Try to nish the standard problems at home if necessary Topics Session subjects
More information11 Data Structures Foundations of Computer Science Cengage Learning
11 Data Structures 11.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define a data structure. Define an array as a data structure
More informationChapter 12: Query Processing. Chapter 12: Query Processing
Chapter 12: Query Processing Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 12: Query Processing Overview Measures of Query Cost Selection Operation Sorting Join
More informationLecture 19 Sorting Goodrich, Tamassia
Lecture 19 Sorting 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Outline Review 3 simple sorting algorithms: 1. selection Sort (in previous course) 2. insertion Sort (in previous
More informationBinary Heaps in Dynamic Arrays
Yufei Tao ITEE University of Queensland We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting
More informationLecture 21: Red-Black Trees
15-150 Lecture 21: Red-Black Trees Lecture by Dan Licata April 3, 2012 Last time, we talked about the signature for dictionaries: signature ORDERED = sig type t val compare : t * t -> order signature DICT
More informationData Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 18 Tries Today we are going to be talking about another data
More informationAlgorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationDatabase System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use
Chapter 11: Indexing and Hashing Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B + -Tree Index Files Static
More informationChapter 12: Query Processing
Chapter 12: Query Processing Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Overview Chapter 12: Query Processing Measures of Query Cost Selection Operation Sorting Join
More informationWeek 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.
Week 8 Binarys 1 2 of 3 4 of 5 6 7 General remarks We consider, another important data structure. We learn how to use them, by making efficient queries. And we learn how to build them. Reading from CLRS
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
More informationUnit 8: Analysis of Algorithms 1: Searching
P Computer Science Unit 8: nalysis of lgorithms 1: Searching Topics: I. Sigma and Big-O notation II. Linear Search III. Binary Search Materials: I. Rawlins 1.6 II. Rawlins 2.1 III. Rawlins 2.3 IV. Sigma
More informationTree traversals and binary trees
Tree traversals and binary trees Comp Sci 1575 Data Structures Valgrind Execute valgrind followed by any flags you might want, and then your typical way to launch at the command line in Linux. Assuming
More informationCOMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first
COMP 0 Priority Queues and Heaps RECAP RECAP-TODAY Tree Structures (in particular Binary Search Trees (BST)) BSTs idea nice way to implement a Set, Bag, or Map TODAY Priority Queue = variation on Queue
More informationCS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya
CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees Issued: 24. 08. 2016 Exercise
More informationEE 368. Weeks 5 (Notes)
EE 368 Weeks 5 (Notes) 1 Chapter 5: Trees Skip pages 273-281, Section 5.6 - If A is the root of a tree and B is the root of a subtree of that tree, then A is B s parent (or father or mother) and B is A
More informationTREES. Trees - Introduction
TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)
More informationWeek 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.
Week 8 Binarys 1 2 3 4 5 6 7 General remarks We consider, another important data structure. We learn how to use them, by making efficient queries. And we learn how to build them. Reading from CLRS for
More informationCS301 - Data Structures Glossary By
CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm
More informationDelhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:
Serial : 1PT_CS_A+C_Programming & Data Structure_230918 Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: E-mail: info@madeeasy.in Ph: 011-45124612 CLASS TEST 2018-19
More informationCMSC 754 Computational Geometry 1
CMSC 754 Computational Geometry 1 David M. Mount Department of Computer Science University of Maryland Fall 2005 1 Copyright, David M. Mount, 2005, Dept. of Computer Science, University of Maryland, College
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 3 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 2... Algorithm Analysis Dynamic Array Iterator Today 1 2 3 Singly Iterator An iterator is a structure that
More informationCS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya
CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree
More information