Efficient solutions: Binary search trees (and extensions)
overview
This lecture is only the tip of the iceberg!
See COS 350 and COS 424!
Geometric Applications of BSTs
1D range search
1d range search
Extension of ordered symbol table
Insert key-value pair
Search for key k
Delete key k
Range search: find all keys between k1 and k2
Range count: find number of keys between k1 and k2
Application: database queries
1d range search
insert B B
insert D B D
insert A A B D
insert I A B D I
insert H A B D H I
insert F A B D F H I
insert P A B D F H I P
search G to K H I
count G to K 2
Suppose that the keys are stored in a sorted array. What is the order of growth of the running time to perform range count as a function of \(N\) and \(R\), where \(N\) is number of keys and \(R\) is number of matching keys?
\(\log R\)
\(\log N\)
\(R + \log N\)
\(R + N\)
1d range search: elementary impl
Ordered array: slow insert; fast range search
Unordered list: fast insert; slow range search
data structure
insert
range count
range search
ordered array
\(N\)
\(\log N\)
\(R + \log N\)
unordered list
\(1\)
\(N\)
\(N\)
goal
\(\log N\)
\(\log N\)
\(R + \log N\)
\(N\) is number of keys
\(R\) is number of keys that match
assuming distinct keys
1d range count: BST implementation
1D range count: how many keys between lo and hi, inclusively?
public int size(Key lo, Key hi) {
int rhi = rank(hi);
int rlo = rank(lo);
if(contains(hi))
return rhi-rlo+1;
else
return rhi-rlo;
// num of keys < hi
}
Proposition: running time proportional to \(\log N\), assuming BST is balanced.
Pf: Nodes examined = search path to lo + search path to hi
1d range search: BST implementation
1D range search: find all keys between lo and hi
Recursively find all keys in left subtree if any could fall in range
Check key in current node
Recursively find all keys in right subtree if any could fall in range
1d range search: BST implementation
keys(E,Q) => {E, G, H, L, M, P}
1d range search: BST implementation
Proposition: running time proportional to \(R + \log N\)
Proof: Nodes examined = search path to lo + search path to hi + matches
1d range search: summary of performance
Ordered array: slow insert; fast range search
Unordered list: fast insert, slow range search
BST: fast insert, fast range search
data structure
insert
range count
range search
ordered array
\(N\)
\(\log N\)
\(R + \log N\)
unordered list
\(1\)
\(N\)
\(N\)
goal
\(\log N\)
\(\log N\)
\(R + \log N\)
\(N\) is number of keys
\(R\) is number of keys that match
Exercise: interval stabbing query
Goal: Insert intervals ((left,right)) and support queries of the form "how many intervals contain x?"
Geometric applications of bsts
line segment intersection
orthogonal line segment intersection
Given \(N\) horizontal and vertical line segments, find all intersections
Quadratic algorithm: Check all pairs of line segments for intersections
microprocessors and geometry
Early 1970s: microprocessor design became a geometric problem
Very Large Scale Integration (VLSI)
Computer-Aided Design (CAD)
Design-rule checking
Certain wires cannot intersect
Certain spacing needed between different types of wires
Debugging = line segment (or rectangle) intersection
algorithms and moore's law
Moore's law (1965): Transistor count doubles every 2 years
Gordon Moore
Sustaining Moore's law
Sustaining Moore's law: How much money/time do I need to get the job done with a quadratic algorithm?