Introduction Radboud University Nijmegen (Balanced) Binary Search Trees Alexandra Silva [email protected]http://www.cs.ru.nl/ ~ alexandra Institute for Computing and Information Sciences Radboud University Nijmegen 14th October 2014 Alexandra 14th October 2014 Lesson 6 1 / 21
79
Embed
(Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• This week: data structures, importance of a good DS.
Alexandra 14th October 2014 Lesson 6 2 / 21
Introduction Radboud University Nijmegen
Motivation
• Airport runaway reservation system
• Simplified scenario: only one runaway available.
• Safety issues associated with landing planes, and planes takingoff.
• Build these constraints in– and the checks for theseconstraints– into your data structure.
Alexandra 14th October 2014 Lesson 6 3 / 21
Introduction Radboud University Nijmegen
Parameters
• Reserve request: specifies landing time t.
• Add t to the set R of landing times if no other landings arescheduled within k minutes.
• k can vary: let’s assume it is statically set (e.g. 3 min).
• After landing, remove request from R.
What operations do we need in the data structure?
• Adding requests. If they satisfy constraint!
• Removing requests.
• Notion of time, checks every m seconds to update thestructure.
• Nutshell: we need a data structure that allows for insertionand removal of elements.
• Additional requirement: operations in O(lg n)
Alexandra 14th October 2014 Lesson 6 4 / 21
Introduction Radboud University Nijmegen
Parameters
• Reserve request: specifies landing time t.
• Add t to the set R of landing times if no other landings arescheduled within k minutes.
• k can vary: let’s assume it is statically set (e.g. 3 min).
• After landing, remove request from R.
What operations do we need in the data structure?
• Adding requests.
If they satisfy constraint!
• Removing requests.
• Notion of time, checks every m seconds to update thestructure.
• Nutshell: we need a data structure that allows for insertionand removal of elements.
• Additional requirement: operations in O(lg n)
Alexandra 14th October 2014 Lesson 6 4 / 21
Introduction Radboud University Nijmegen
Parameters
• Reserve request: specifies landing time t.
• Add t to the set R of landing times if no other landings arescheduled within k minutes.
• k can vary: let’s assume it is statically set (e.g. 3 min).
• After landing, remove request from R.
What operations do we need in the data structure?
• Adding requests. If they satisfy constraint!
• Removing requests.
• Notion of time, checks every m seconds to update thestructure.
• Nutshell: we need a data structure that allows for insertionand removal of elements.
• Additional requirement: operations in O(lg n)
Alexandra 14th October 2014 Lesson 6 4 / 21
Introduction Radboud University Nijmegen
Parameters
• Reserve request: specifies landing time t.
• Add t to the set R of landing times if no other landings arescheduled within k minutes.
• k can vary: let’s assume it is statically set (e.g. 3 min).
• After landing, remove request from R.
What operations do we need in the data structure?
• Adding requests. If they satisfy constraint!
• Removing requests.
• Notion of time, checks every m seconds to update thestructure.
• Nutshell: we need a data structure that allows for insertionand removal of elements.
• Additional requirement: operations in O(lg n)
Alexandra 14th October 2014 Lesson 6 4 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44
not allowed (because 46 is in R)
• 53 X
• 20 not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44 not allowed (because 46 is in R)
• 53
X
• 20 not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44 not allowed (because 46 is in R)
• 53 X
• 20
not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44 not allowed (because 46 is in R)
• 53 X
• 20 not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44 not allowed (because 46 is in R)
• 53 X
• 20 not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Example
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
Lecture 5: Scheduling and Binary Search
Trees
Lecture Overview
• Runway reservation system
– Definition
– How to solve with lists
• Binary Search Trees
– Operations
Readings
CLRS Chapter 10, 12.1-3
Runway Reservation System
• Airport with single (very busy) runway (Boston 6 ! 1)
• “Reservations” for future landings
• When plane lands, it is removed from set of pending events
• Reserve req specify “requested landing time” t
• Add t to the set if no other landings are scheduled within k minutes either way.
Assume that k can vary.
– else error, don’t schedule
Example
37 41 46 49 56time (mins)
now x x x x
Figure 1: Runway Reservation System Example
Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3
1
What happens for
• 44 not allowed (because 46 is in R)
• 53 X
• 20 not allowed (time has passed)
• How do we do it efficiently in O(lg n)?
Alexandra 14th October 2014 Lesson 6 5 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good?
most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time.
Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good?
Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Options for data structures
• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)
• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.
• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).
• We almost had it in the sorted list/array.
• Key point: We need fast insertion into a sorted list.
• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?
• A large array indexed by time!
Alexandra 14th October 2014 Lesson 6 6 / 21
Introduction Radboud University Nijmegen
Binary Search trees
• Each node x in the binary tree has a key key(x).
• Nodes other than the root have a parent p(x).
• Nodes may have a left child left(x) and/or a right childright(x).
• The invariant of BSTs: for any node x , for all nodes y in theleft subtree of x , key(y) ≤ key(x). For all nodes y in theright subtree of x key(y) ≥ key(x).
Alexandra 14th October 2014 Lesson 6 7 / 21
Introduction Radboud University Nijmegen
Operations – Insert
Insert(val)
• Follow left and right children till you find the position (or seethe value). [Example on the board]
• How about the within k = 3 check?
• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 8 / 21
Introduction Radboud University Nijmegen
Operations – Insert
Insert(val)
• Follow left and right children till you find the position (or seethe value). [Example on the board]
• How about the within k = 3 check?
• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 8 / 21
Introduction Radboud University Nijmegen
Operations – Insert
Insert(val)
• Follow left and right children till you find the position (or seethe value). [Example on the board]
• How about the within k = 3 check?
• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 8 / 21
Introduction Radboud University Nijmegen
Operations – Insert
Insert(val)
• Follow left and right children till you find the position (or seethe value). [Example on the board]
• How about the within k = 3 check?
• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]
Complexity:
O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 8 / 21
Introduction Radboud University Nijmegen
Operations – Insert
Insert(val)
• Follow left and right children till you find the position (or seethe value). [Example on the board]
• How about the within k = 3 check?
• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 8 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity:
O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
Operations
Finding a value in the BST if it exists: find(val)
• Follow left and right pointers until you find it or hit NIL.
Finding the minimum element in a BST: findmin()
• Key is to just go left till you cannot go left anymore.
• findmax?
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 9 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers.
How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How?
Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity:
O(h), h is the height of the tree.
Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
New requirement
• Problem specification does not stay the same. . .
• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?
• How to do it?
• Augmented BST.
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
• Insert and deletion modify numbers. How? Add-in on smallernodes.
Complexity: O(h), h is the height of the tree.Alexandra 14th October 2014 Lesson 6 10 / 21
Introduction Radboud University Nijmegen
Rank(t)
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
One option:
• Walk down tree to find the desired time.
• Add in the nodes that are smaller.
• Add in the subtree sizes to the left.
• Example: Rank(79) = 1 + 2 + 1 + 1 = 5
Alexandra 14th October 2014 Lesson 6 11 / 21
Introduction Radboud University Nijmegen
Rank(t)
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
One option:
• Walk down tree to find the desired time.
• Add in the nodes that are smaller.
• Add in the subtree sizes to the left.
• Example: Rank(79) =
1 + 2 + 1 + 1 = 5
Alexandra 14th October 2014 Lesson 6 11 / 21
Introduction Radboud University Nijmegen
Rank(t)
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
One option:
• Walk down tree to find the desired time.
• Add in the nodes that are smaller.
• Add in the subtree sizes to the left.
• Example: Rank(79) = 1 + 2 + 1 + 1 = 5
Alexandra 14th October 2014 Lesson 6 11 / 21
Introduction Radboud University Nijmegen
Have we solved the original problem?
• We have a data structure with operation in O(h).
• The original problem required O(lg n).
• Are we there?
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!
Balanced BSTs to the rescue in the next lecture!
7
�
• Bad news: BSTs can look like lists.
• Good news: we have a solution. Balanced BSTs.
Alexandra 14th October 2014 Lesson 6 12 / 21
Introduction Radboud University Nijmegen
Have we solved the original problem?
• We have a data structure with operation in O(h).
• The original problem required O(lg n).
• Are we there?
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!
Balanced BSTs to the rescue in the next lecture!
7
�
• Bad news: BSTs can look like lists.
• Good news: we have a solution. Balanced BSTs.
Alexandra 14th October 2014 Lesson 6 12 / 21
Introduction Radboud University Nijmegen
Have we solved the original problem?
• We have a data structure with operation in O(h).
• The original problem required O(lg n).
• Are we there?
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!
Balanced BSTs to the rescue in the next lecture!
7
�
• Bad news: BSTs can look like lists.
• Good news: we have a solution. Balanced BSTs.
Alexandra 14th October 2014 Lesson 6 12 / 21
Introduction Radboud University Nijmegen
Have we solved the original problem?
• We have a data structure with operation in O(h).
• The original problem required O(lg n).
• Are we there?
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!
Balanced BSTs to the rescue in the next lecture!
7
�
• Bad news: BSTs can look like lists.
• Good news: we have a solution. Balanced BSTs.
Alexandra 14th October 2014 Lesson 6 12 / 21
Introduction Radboud University Nijmegen
Have we solved the original problem?
• We have a data structure with operation in O(h).
• The original problem required O(lg n).
• Are we there?
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!
Balanced BSTs to the rescue in the next lecture!
7
�
• Bad news: BSTs can look like lists.
• Good news: we have a solution. Balanced BSTs.
Alexandra 14th October 2014 Lesson 6 12 / 21
Introduction Radboud University Nijmegen
Balanced BSTs
• Operations of BSTs were in O(h). [What is h?]
• h can vary between lg n and n:
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
New Requirement
Rank(t): How many planes are scheduled to land at times t? The new requirement
necessitates a design amendment.
Cannot solve it e�ciently with what we have but can augment the BST structure.
79
49
46
43 64 83
6
2
1
3
1 1
what lands before 79?
keep track of size of subtrees, during insert and delete
Figure 5: Augmenting the BST Structure
Summarizing from Fig. 5, the algorithm for augmentation is as follows:
1. Walk down tree to find desired time
2. Add in nodes that are smaller
3. Add in subtree sizes to the left
In total, this takes O(h) time.
6
vs
Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011
49 461 + 2 + 1 + 1 = 5
79 64subtree
subtree
Figure 6: Augmentation Algorithm Example
All the Python code for the Binary Search Trees discussed here are available at this
link
Have we accomplished anything?
Height h of the tree should be O(lg n).
46
43
49
55
Figure 7: Insert into BST in sorted order
The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!