SlideShare a Scribd company logo
1 of 30
Chapter 15
Augmenting Data Structures
Introduction
• “Text book” data structures are sufficient for
many tasks, but not all
• Rarely need to create new data structures
• Often sufficient to “augment” an existing
data structure with additional information
and operations
• Not always straightforward - must be able to
maintain added information with existing
operations
§15.1 Dynamic Order Statistics
• Recall from Chapter 10 that from an
unordered set, we can retrieve the ith order
statistic from a set of n elements in O(n) time
• Red-black trees can be augmented to allow
for fast retrieval of order statistics
• We shall also allow for quick determination
of the rank of an element
Order Statistic Trees
• Standard red-black tree
with an additional
size field (the bottom
number in each node)
• size contains # of
nodes in subtree rooted
at x, including x
• If nil->size = 0, then:
x->size = x->left->size +
x->right->size + 1
14
7
10
4
7
2
3
1
12
1
16
2
14
1
17
12
19
2
21
4
21
1
20
1
28
1
35
1
39
1
38
3
30
5
47
1
41
7
26
20
Retrieving Elements of a Given Rank
ostree::Select(node *x, int i)
{
int r = x->left->size + 1;
if ( i == r )
return x;
else if ( i < r )
return Select(x->left, i);
else
return Select(x->right, i-r);
}
• x->left->size contains the
number of nodes that come
before x in an inorder tree walk
– x’s rank within it’s subtree is
therefore x->left->size + 1
• Recursive selection similar to the
algorithms we saw in Chapter 10
– If x is correct order stat, return
– If x > correct order stat, recurse
left
– If x < correct order stat, recurse
right, looking for the (i-r)th order
statistic in the right subtree
What Is The 16th Order Statistic?
14
7
10
4
7
2
3
1
12
1
16
2
14
1
17
12
19
2
21
4
21
1
20
1
28
1
35
1
39
1
38
3
30
5
47
1
41
7
26
20
Analysis of ostree::Select
• Each level of recursion descends one
level of the OS tree
– Therefore, ostree::Select is at worst O(h),
where h is the height of the tree
– Since the height of the tree is known to be
O(lgn), ostree::Select has running time
O(lgn)
Determining the Rank of an Element
ostree::Rank(node *x)
{
int r = x->left->size + 1;
node *y = x;
while ( y != root )
{
if ( y == y->parent->right)
r += y->parent->left->size + 1;
y = y->parent;
}
return r;
}
• The rank of a node x =
the # of nodes that
precede it, + 1 for itself
• r is maintained as the
rank of x in the subtree
rooted at y - which
denotes our position in
the tree
– To start, r is the rank of x
in it’s subtree
Determining the Rank of an Element
ostree::Rank(node *x)
{
int r = x->left->size + 1;
node *y = x;
while ( y != root )
{
if ( y == y->parent->right)
r += y->parent->left->size + 1;
y = y->parent;
}
return r;
}
• Each loop ascends the
tree, and calculates x’s
rank in that subtree
– If y is a left child, the
rank is unchanged
– If y is a right child, the
rank is equal to the size
of the left subtree, plus 1
for the parent node
What Is The Rank of 28?
14
7
10
4
7
2
3
1
12
1
16
2
14
1
17
12
19
2
21
4
21
1
20
1
28
1
35
1
39
1
38
3
30
5
47
1
41
7
26
20
Analysis of ostree::Rank
• Each loop ascends one level of the OS
tree
– Therefore, ostree::Rank is at worst O(h),
where h is the height of the tree
– Since the height of the tree is known to be
O(lgn), ostree::Rank has running time
O(lgn)
Maintaining Subtree Sizes
• ostree::Select & ostree::Rank are only
useful if we can efficiently maintain the
size field
• To be truly efficient, these fields must
be maintained through the basic
maintenance operations of the tree
Maintaining Subtree Sizes
• Insertion
– Recall the two operations: bst::Insert, and performing
rotations
• bst::Insert
– As we descend the tree to perform insertion, increment size
field of all traversed nodes
• Rotation
– Only the size fields of the rotated nodes are affected
– The new parent node simply assumes the size of the old
parent node
– The rotated node must now assume calculate it’s size as the
sum of it’s children, plus one
Maintaining Subtree Sizes
93
19
42
11
6 4
7
x
y
93
12
42
19
6
4 7
y
x
RightRotate(y)
LeftRotate(x)
Size Maintenance Through Rotation:
y->size = x->size;
x->size = x->left->size + x->right->size + 1;
What is total added cost to rotation?
Maintaining Subtree Sizes
• Deletion
– Also has two phases: one to delete the node, the
other to maintain the tree with at most three
rotations
– We already know the added cost of rotation
– When we splice out a node, we can traverse up the
tree, and decrement one from every node along
it’s path
• This requires O(lgn) additional time
Maintaining Subtree Sizes
• Analysis:
– Insertion is changed by at most O(1)
– Deletion is changed by at most O(lgn)
– Thus, the total asymptotic running time of
insertion and deletion is unchanged at
O(lgn)
§15.2 How To Augment A Data Structure
• Four steps:
– Choosing an underlying data structure;
– Determining additional information to be
maintained in the underlying data structure;
– Verifying that the additional information can be
maintained by the basic modifying operations on
the underlying data structure; and
– Developing new operations
• Note: this isn’t a “formula”, but a good
starting point
Augmenting Red-Black Trees for
Order Statistics
• Step 1:
– We chose red-black trees as the underlying data structure
due to efficient support of other dynamic-set operations
• Step 2:
– Augmented nodes with the size field, to allow the desired
operations to be more efficient
• Step 3:
– We ensured that insert and delete can maintain the new field
and still operate in O(lgn) time
• Step 4:
– We developed ostree::Select and ostree::Rank
Why Augment Red-Black Trees?
• From Theorem 15.1:
– If the new field can computed and
maintained using only the information in
nodes x, x->left, and x->right, then we can
maintain the values of the new field in all
nodes during insertion and deletion
without asymptotically affecting the O(lgn)
performance of these operations
§15.3 Interval Trees
• An interval is a pair of real numbers used to specify a
range of values
– A closed interval [t1, t2] specifies a range that includes the
endpoints
– An open interval (t1, t2) specifies a range that excludes the
endpoints
– A half-open interval [t1, t2) or (t1, t2] excludes one of the
endpoints
• E.g., consider a log that stores events sorted by time
– We may want to query the log to find out what happened during a
given time interval
Intervals
• Assume intervals are represented as
structs with two fields: lo and hi
• Consider two intervals x and y
– Any two intervals must satisfy the interval
trichotomy:
•x and y overlap (x.lo <= y.hi && y.lo <= x.hi)
•x lies completely to the left of y (x.hi < y.lo)
•x lies completely to the right of y (y.hi < x.lo)
Interval Trees
• Interval Tree is a red-black tree that maintains
a dynamic set of nodes
– Each node contains an interval
• Support these operations:
– Insertion - adds an element to the tree
– Deletion - removes an element from the tree
– Search - searches for an interval that overlaps the
requested interval
Interval Trees
[16,21]
30
[8,9]
23
[5,8]
10
[15,23]
23
[0,3]
3
[6,10]
10
[25,30]
30
[17,19]
20
[26,26]
26
[19,20]
20
An interval tree sorted by the low endpoint of each interval
Interval Trees
• The interval tree stores intervals, and is
sorted by the low endpoint
• Each node contains an additional field, max,
which is the maximum value of any interval
endpoint stored in its subtree
– Maintained through insertion/deletion with this
O(1) statement:
x->max = max(x->interval->hi, x->left->max, x->right->max)
– What about through rotations? The same
operation applies, to the new parent node
Interval Trees: New Operations
• The only new operation is the Search operation:
intervalTree::Search(interval i)
{
node *x = root;
while ( x != NULL && !Overlap(x->interval, i) )
{
if ( x->left != NULL &&
x->left->max >= i->lo )
x = x->left;
else
x = x->right;
}
}
Interval Tree Search
[16,21]
30
[8,9]
23
[5,8]
10
[15,23]
23
[0,3]
3
[6,10]
10
[25,30]
30
[17,19]
20
[26,26]
26
[19,20]
20
• Search this tree for
the interval [22,25]
• Now search for
[11,14]
• What is the
asymptotic growth
of Search?
• Why does it work?
Interval Tree Search
• Interval tree search algorithm finds the
first overlapping interval
• How could we find all overlapping
intervals?
Why Interval Tree Search Works
• Recall this part of the interval tree search algorithm:
if ( x->left != NULL && x->left->max >= i->lo )
x = x->left;
else
x = x->right;
• If the else is executed, then the left branch is NULL, or the lo
end of the interval we’re searching for is to the right of highest
hi endpoint in the left subtree, so if an interval exists it must be
in the right subtree
• If the first branch is executed, then the max value in the left
subtree is greater than lo value of the interval we’re searching
for - so there can be an overlapping interval in the left subtree
Why Interval Tree Search Works
• Why aren’t there any in the right subtree?
– Assume there are no overlapping intervals
– The tree is sorted by the lo endpoint
– All nodes in the right subtree have lo endpoints > all nodes
in the right subtree
– Since i->lo < x->left->max, then for some node in the left
subtree, there’s a node with hi endpoint = x->left->max, and
lo endpoint < all lo endpoints in right subtree
– If there are no overlapping intervals, it follows that i->lo <
that node’s lo interval (otherwise it would overlap), so
therefore if an overlapping interval existed it would have to
be in the left subtree
Assignment
• Page 286: 15.1-3, 15.1-5
• Page 295: 15.3-2, 15.3-3, 15.3-5, 15.3-6

More Related Content

What's hot

Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C LanguageSurbhi Yadav
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
 
Frequent itemset mining using pattern growth method
Frequent itemset mining using pattern growth methodFrequent itemset mining using pattern growth method
Frequent itemset mining using pattern growth methodShani729
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 dnikhilarora2211
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
The comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithmThe comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithmdeepti92pawar
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 
Preparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representationPreparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representationAndres Mendez-Vazquez
 

What's hot (20)

Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Frequent itemset mining using pattern growth method
Frequent itemset mining using pattern growth methodFrequent itemset mining using pattern growth method
Frequent itemset mining using pattern growth method
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Apriori algorithm
Apriori algorithmApriori algorithm
Apriori algorithm
 
Tree
TreeTree
Tree
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Priority queue
Priority queuePriority queue
Priority queue
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Row major and column major in 2 d
Row major and column major in 2 dRow major and column major in 2 d
Row major and column major in 2 d
 
Stacks
StacksStacks
Stacks
 
Dynamic Itemset Counting
Dynamic Itemset CountingDynamic Itemset Counting
Dynamic Itemset Counting
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
The comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithmThe comparative study of apriori and FP-growth algorithm
The comparative study of apriori and FP-growth algorithm
 
Array in C
Array in CArray in C
Array in C
 
Array
ArrayArray
Array
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
Preparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representationPreparation Data Structures 06 arrays representation
Preparation Data Structures 06 arrays representation
 

Viewers also liked

TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structureddewithaman10
 
Dynamic DFS in Undirected Graph using Segment Tree
Dynamic DFS in Undirected Graph using Segment TreeDynamic DFS in Undirected Graph using Segment Tree
Dynamic DFS in Undirected Graph using Segment TreeSiraj Memon
 
Priority Lands September 25 2008
Priority Lands September 25 2008Priority Lands September 25 2008
Priority Lands September 25 2008ecopatriot80
 
Problems with-planting-tress-interval
Problems with-planting-tress-intervalProblems with-planting-tress-interval
Problems with-planting-tress-intervalKathleen Ong
 
A Parallel Data Distribution Management Algorithm
A Parallel Data Distribution Management AlgorithmA Parallel Data Distribution Management Algorithm
A Parallel Data Distribution Management AlgorithmGabriele D'Angelo
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Alp Çoker
 
数据结构回顾
数据结构回顾数据结构回顾
数据结构回顾Zehua HONG
 
23 priority queue
23 priority queue23 priority queue
23 priority queueGodo Dodo
 
Spanning Tree Bridge Root Priority value & Extended System ID
Spanning Tree Bridge Root Priority value & Extended System IDSpanning Tree Bridge Root Priority value & Extended System ID
Spanning Tree Bridge Root Priority value & Extended System IDNetProtocol Xpert
 
brand building and service marketing at banyan tree hotels and resorts
brand building and service marketing at banyan tree hotels and resortsbrand building and service marketing at banyan tree hotels and resorts
brand building and service marketing at banyan tree hotels and resortssaurabh
 
Linear regression and correlation analysis ppt @ bec doms
Linear regression and correlation analysis ppt @ bec domsLinear regression and correlation analysis ppt @ bec doms
Linear regression and correlation analysis ppt @ bec domsBabasab Patil
 
Introduction to Natural Language Processing
Introduction to Natural Language ProcessingIntroduction to Natural Language Processing
Introduction to Natural Language Processingrohitnayak
 
23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMSkoolkampus
 

Viewers also liked (20)

TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structure
 
Dynamic DFS in Undirected Graph using Segment Tree
Dynamic DFS in Undirected Graph using Segment TreeDynamic DFS in Undirected Graph using Segment Tree
Dynamic DFS in Undirected Graph using Segment Tree
 
TripleTree eDiscovery
TripleTree  eDiscoveryTripleTree  eDiscovery
TripleTree eDiscovery
 
K d tree_cpp
K d tree_cppK d tree_cpp
K d tree_cpp
 
Priority Lands September 25 2008
Priority Lands September 25 2008Priority Lands September 25 2008
Priority Lands September 25 2008
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 
Problems with-planting-tress-interval
Problems with-planting-tress-intervalProblems with-planting-tress-interval
Problems with-planting-tress-interval
 
Segment tree
Segment treeSegment tree
Segment tree
 
Segment tree
Segment treeSegment tree
Segment tree
 
A Parallel Data Distribution Management Algorithm
A Parallel Data Distribution Management AlgorithmA Parallel Data Distribution Management Algorithm
A Parallel Data Distribution Management Algorithm
 
Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )Game Tree ( Oyun Ağaçları )
Game Tree ( Oyun Ağaçları )
 
Segment tree
Segment treeSegment tree
Segment tree
 
STP
STPSTP
STP
 
数据结构回顾
数据结构回顾数据结构回顾
数据结构回顾
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
 
Spanning Tree Bridge Root Priority value & Extended System ID
Spanning Tree Bridge Root Priority value & Extended System IDSpanning Tree Bridge Root Priority value & Extended System ID
Spanning Tree Bridge Root Priority value & Extended System ID
 
brand building and service marketing at banyan tree hotels and resorts
brand building and service marketing at banyan tree hotels and resortsbrand building and service marketing at banyan tree hotels and resorts
brand building and service marketing at banyan tree hotels and resorts
 
Linear regression and correlation analysis ppt @ bec doms
Linear regression and correlation analysis ppt @ bec domsLinear regression and correlation analysis ppt @ bec doms
Linear regression and correlation analysis ppt @ bec doms
 
Introduction to Natural Language Processing
Introduction to Natural Language ProcessingIntroduction to Natural Language Processing
Introduction to Natural Language Processing
 
23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS23. Advanced Datatypes and New Application in DBMS
23. Advanced Datatypes and New Application in DBMS
 

Similar to Chapter 15

splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Support vector machine
Support vector machineSupport vector machine
Support vector machinePrasenjit Dey
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptxVandanaBharti21
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdfssusere19c741
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.pptAshok280385
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in Onejehan1987
 
Lecture 9-online
Lecture 9-onlineLecture 9-online
Lecture 9-onlinelifebreath
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using javaNarayan Sau
 

Similar to Chapter 15 (20)

splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Queues
QueuesQueues
Queues
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Support vector machine
Support vector machineSupport vector machine
Support vector machine
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptxWeek2-stacks-queues.pptx
Week2-stacks-queues.pptx
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt1.1 introduction to Data Structures.ppt
1.1 introduction to Data Structures.ppt
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Lecture 9-online
Lecture 9-onlineLecture 9-online
Lecture 9-online
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Sorting
SortingSorting
Sorting
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Isolation Forest
Isolation ForestIsolation Forest
Isolation Forest
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 

More from ashish bansal (14)

Data struters
Data strutersData struters
Data struters
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Cis435 week04
Cis435 week04Cis435 week04
Cis435 week04
 
Cis435 week03
Cis435 week03Cis435 week03
Cis435 week03
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
5 searching
5 searching5 searching
5 searching
 
4 recursion
4 recursion4 recursion
4 recursion
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 

Recently uploaded

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

Chapter 15

  • 2. Introduction • “Text book” data structures are sufficient for many tasks, but not all • Rarely need to create new data structures • Often sufficient to “augment” an existing data structure with additional information and operations • Not always straightforward - must be able to maintain added information with existing operations
  • 3. §15.1 Dynamic Order Statistics • Recall from Chapter 10 that from an unordered set, we can retrieve the ith order statistic from a set of n elements in O(n) time • Red-black trees can be augmented to allow for fast retrieval of order statistics • We shall also allow for quick determination of the rank of an element
  • 4. Order Statistic Trees • Standard red-black tree with an additional size field (the bottom number in each node) • size contains # of nodes in subtree rooted at x, including x • If nil->size = 0, then: x->size = x->left->size + x->right->size + 1 14 7 10 4 7 2 3 1 12 1 16 2 14 1 17 12 19 2 21 4 21 1 20 1 28 1 35 1 39 1 38 3 30 5 47 1 41 7 26 20
  • 5. Retrieving Elements of a Given Rank ostree::Select(node *x, int i) { int r = x->left->size + 1; if ( i == r ) return x; else if ( i < r ) return Select(x->left, i); else return Select(x->right, i-r); } • x->left->size contains the number of nodes that come before x in an inorder tree walk – x’s rank within it’s subtree is therefore x->left->size + 1 • Recursive selection similar to the algorithms we saw in Chapter 10 – If x is correct order stat, return – If x > correct order stat, recurse left – If x < correct order stat, recurse right, looking for the (i-r)th order statistic in the right subtree
  • 6. What Is The 16th Order Statistic? 14 7 10 4 7 2 3 1 12 1 16 2 14 1 17 12 19 2 21 4 21 1 20 1 28 1 35 1 39 1 38 3 30 5 47 1 41 7 26 20
  • 7. Analysis of ostree::Select • Each level of recursion descends one level of the OS tree – Therefore, ostree::Select is at worst O(h), where h is the height of the tree – Since the height of the tree is known to be O(lgn), ostree::Select has running time O(lgn)
  • 8. Determining the Rank of an Element ostree::Rank(node *x) { int r = x->left->size + 1; node *y = x; while ( y != root ) { if ( y == y->parent->right) r += y->parent->left->size + 1; y = y->parent; } return r; } • The rank of a node x = the # of nodes that precede it, + 1 for itself • r is maintained as the rank of x in the subtree rooted at y - which denotes our position in the tree – To start, r is the rank of x in it’s subtree
  • 9. Determining the Rank of an Element ostree::Rank(node *x) { int r = x->left->size + 1; node *y = x; while ( y != root ) { if ( y == y->parent->right) r += y->parent->left->size + 1; y = y->parent; } return r; } • Each loop ascends the tree, and calculates x’s rank in that subtree – If y is a left child, the rank is unchanged – If y is a right child, the rank is equal to the size of the left subtree, plus 1 for the parent node
  • 10. What Is The Rank of 28? 14 7 10 4 7 2 3 1 12 1 16 2 14 1 17 12 19 2 21 4 21 1 20 1 28 1 35 1 39 1 38 3 30 5 47 1 41 7 26 20
  • 11. Analysis of ostree::Rank • Each loop ascends one level of the OS tree – Therefore, ostree::Rank is at worst O(h), where h is the height of the tree – Since the height of the tree is known to be O(lgn), ostree::Rank has running time O(lgn)
  • 12. Maintaining Subtree Sizes • ostree::Select & ostree::Rank are only useful if we can efficiently maintain the size field • To be truly efficient, these fields must be maintained through the basic maintenance operations of the tree
  • 13. Maintaining Subtree Sizes • Insertion – Recall the two operations: bst::Insert, and performing rotations • bst::Insert – As we descend the tree to perform insertion, increment size field of all traversed nodes • Rotation – Only the size fields of the rotated nodes are affected – The new parent node simply assumes the size of the old parent node – The rotated node must now assume calculate it’s size as the sum of it’s children, plus one
  • 14. Maintaining Subtree Sizes 93 19 42 11 6 4 7 x y 93 12 42 19 6 4 7 y x RightRotate(y) LeftRotate(x) Size Maintenance Through Rotation: y->size = x->size; x->size = x->left->size + x->right->size + 1; What is total added cost to rotation?
  • 15. Maintaining Subtree Sizes • Deletion – Also has two phases: one to delete the node, the other to maintain the tree with at most three rotations – We already know the added cost of rotation – When we splice out a node, we can traverse up the tree, and decrement one from every node along it’s path • This requires O(lgn) additional time
  • 16. Maintaining Subtree Sizes • Analysis: – Insertion is changed by at most O(1) – Deletion is changed by at most O(lgn) – Thus, the total asymptotic running time of insertion and deletion is unchanged at O(lgn)
  • 17. §15.2 How To Augment A Data Structure • Four steps: – Choosing an underlying data structure; – Determining additional information to be maintained in the underlying data structure; – Verifying that the additional information can be maintained by the basic modifying operations on the underlying data structure; and – Developing new operations • Note: this isn’t a “formula”, but a good starting point
  • 18. Augmenting Red-Black Trees for Order Statistics • Step 1: – We chose red-black trees as the underlying data structure due to efficient support of other dynamic-set operations • Step 2: – Augmented nodes with the size field, to allow the desired operations to be more efficient • Step 3: – We ensured that insert and delete can maintain the new field and still operate in O(lgn) time • Step 4: – We developed ostree::Select and ostree::Rank
  • 19. Why Augment Red-Black Trees? • From Theorem 15.1: – If the new field can computed and maintained using only the information in nodes x, x->left, and x->right, then we can maintain the values of the new field in all nodes during insertion and deletion without asymptotically affecting the O(lgn) performance of these operations
  • 20. §15.3 Interval Trees • An interval is a pair of real numbers used to specify a range of values – A closed interval [t1, t2] specifies a range that includes the endpoints – An open interval (t1, t2) specifies a range that excludes the endpoints – A half-open interval [t1, t2) or (t1, t2] excludes one of the endpoints • E.g., consider a log that stores events sorted by time – We may want to query the log to find out what happened during a given time interval
  • 21. Intervals • Assume intervals are represented as structs with two fields: lo and hi • Consider two intervals x and y – Any two intervals must satisfy the interval trichotomy: •x and y overlap (x.lo <= y.hi && y.lo <= x.hi) •x lies completely to the left of y (x.hi < y.lo) •x lies completely to the right of y (y.hi < x.lo)
  • 22. Interval Trees • Interval Tree is a red-black tree that maintains a dynamic set of nodes – Each node contains an interval • Support these operations: – Insertion - adds an element to the tree – Deletion - removes an element from the tree – Search - searches for an interval that overlaps the requested interval
  • 24. Interval Trees • The interval tree stores intervals, and is sorted by the low endpoint • Each node contains an additional field, max, which is the maximum value of any interval endpoint stored in its subtree – Maintained through insertion/deletion with this O(1) statement: x->max = max(x->interval->hi, x->left->max, x->right->max) – What about through rotations? The same operation applies, to the new parent node
  • 25. Interval Trees: New Operations • The only new operation is the Search operation: intervalTree::Search(interval i) { node *x = root; while ( x != NULL && !Overlap(x->interval, i) ) { if ( x->left != NULL && x->left->max >= i->lo ) x = x->left; else x = x->right; } }
  • 26. Interval Tree Search [16,21] 30 [8,9] 23 [5,8] 10 [15,23] 23 [0,3] 3 [6,10] 10 [25,30] 30 [17,19] 20 [26,26] 26 [19,20] 20 • Search this tree for the interval [22,25] • Now search for [11,14] • What is the asymptotic growth of Search? • Why does it work?
  • 27. Interval Tree Search • Interval tree search algorithm finds the first overlapping interval • How could we find all overlapping intervals?
  • 28. Why Interval Tree Search Works • Recall this part of the interval tree search algorithm: if ( x->left != NULL && x->left->max >= i->lo ) x = x->left; else x = x->right; • If the else is executed, then the left branch is NULL, or the lo end of the interval we’re searching for is to the right of highest hi endpoint in the left subtree, so if an interval exists it must be in the right subtree • If the first branch is executed, then the max value in the left subtree is greater than lo value of the interval we’re searching for - so there can be an overlapping interval in the left subtree
  • 29. Why Interval Tree Search Works • Why aren’t there any in the right subtree? – Assume there are no overlapping intervals – The tree is sorted by the lo endpoint – All nodes in the right subtree have lo endpoints > all nodes in the right subtree – Since i->lo < x->left->max, then for some node in the left subtree, there’s a node with hi endpoint = x->left->max, and lo endpoint < all lo endpoints in right subtree – If there are no overlapping intervals, it follows that i->lo < that node’s lo interval (otherwise it would overlap), so therefore if an overlapping interval existed it would have to be in the left subtree
  • 30. Assignment • Page 286: 15.1-3, 15.1-5 • Page 295: 15.3-2, 15.3-3, 15.3-5, 15.3-6