btree applications
TRANSCRIPT
CSE 2331
Horizontal-Vertical Line Segment Intersection
Given:
• set LH of horizontal line segments;
• set LV of vertical line segments.
Does any line segment in LV intersect a line segment in LH?
7.3
CSE 2331
Horizontal-Vertical Line Segment Intersection
Brute force algorithm:
1 foreach vertical line segment σV ∈ LV do
2 foreach horizontal line segment σH ∈ LH do
3 if σH ∩ σV 6= ∅ then
4 q ← σH ∩ σV ;
5 return (q);
6 end
7 end
8 end
9 return (“No intersection”);
7.5
CSE 2331
Sort Horizontal Lines
1 LV ← vertical line segments;
2 LH ← horizontal line segments sorted by increasing y;
3 foreach vertical line segment σV ∈ LV do
4 σH ← first line segment in LH with σH .y ≥ σV .miny;
5 while (σH 6= NIL) and (σH .y ≤ σv.maxy) do
6 if σH ∩ σV 6= ∅ then
7 q ← σH ∩ σV ;
8 return (q);
9 end
/* Get next horizontal segment in LH */
10 σH ← σH .Next;
11 end
12 end
13 return (“No intersection”);
7.7
CSE 2331
Line Sweep Algorithm
1 LV ← vertical line segments sorted by increasing x;
2 LH ← horizontal line segments sorted by increasing y;
3 foreach vertical line segment σV ∈ LV do
4 σH ← first line segment in LH with σH .y ≥ σV .miny;
5 while (σH 6= NIL) and (σH .y ≤ σv.maxy) do
6 if σH ∩ σV 6= ∅ then
7 q ← σH ∩ σV ;
8 return (q);
9 end
/* Get next horizontal segment in LH */
10 σH ← σH .Next;
11 end
12 end
13 return (“No intersection”);
7.9
CSE 2331
Tree Minimum Greater Than or Equal to
Find minimum key greater than or equal to 12.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll ==
48nnnnn
2KK
KK 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99
17nnnnn BB
23PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
7.11
CSE 2331
Tree Minimum Greater Than or Equal to
function TreeMinGE(T , K)
/* Return node with min key greater than or equal to K */
1 u← NIL;
2 v ← T.root;
3 while (v 6= NIL) do4 if (K ≤ v.key) then5 u← v;
6 v ← v.left;
7 else8 v ← v.right;
9 end
10 end
11 return (u);
7.12
CSE 2331
Horizontal-Vertical Line Segment Intersection
1 T.Init();
2 L← endpoints of all line segments sorted by x-coordinate;
3 foreach point p ∈ L do4 if (p is the left endpoint of a horizontal segment σH) then5 Create new tree node u with
6 u.key = σH .y and u.data.lineseg = σH ;
7 TreeInsert (T , u);
8 else if (p is the lower endpoint of a vertical segment σV ) then9 u← TreeMinGE (T , p.y);
10 if (u 6= NIL) and (u.y ≤ σV .maxy) then
11 return (u.data.lineseg ∩ σV );
12 else if (p is the right endpoint of a horizontal segment σH) then13 u← tree node representing σH ;
14 TreeDelete (T ,u);
15 end
16 end
17 return (“No intersection”);
7.13
CSE 2331
Report All HV Line Segment Intersections
Given:
• set LH of horizontal line segments;
• set LV of vertical line segments.
Report all the intersections of line segments in LH and LV .
7.15
CSE 2331
Report All HV Line Segment Intersections
Brute force algorithm:
1 foreach horizontal line segment σH ∈ LH do
2 foreach vertical line segment σV ∈ LV do
3 if σH ∩ σV 6= ∅ then
4 q ← σH ∩ σV ;
5 Report intersection point q;
6 end
7 end
8 end
7.17
CSE 2331
Running Times of Reporting
Let I = number of intersections.
Desired running time is Θ(f(n) + I) where f(n) is small
(e.g., f(n) ∈ Θ(n log2(n)).)
7.18
CSE 2331
Report All HV Line Segment Intersections
1 T.Init();
2 L← endpoints of all line segments sorted by x-coordinate;
3 foreach point p ∈ L do4 if (p is the left endpoint of a horizontal segment σH) then5 Create new tree node u with
6 u.key = σH .y and u.data.lineseg = σH ;
7 u.data.lineseg← σH ;
8 TreeInsert (T , u);
9 else if (p is the lower endpoint of a vertical segment σV ) then10 Report line segments in T intersecting σV ;
11 else if (p is the right endpoint of a horizontal segment σH) then12 u← tree node representing σH ;
13 TreeDelete (T ,u);
14 end
15 end
7.20
CSE 2331
Report in Range
Report all nodes of the following tree with keys in range [8, 25]:(Range [8, 25] includes 8 and 25.)
15
fffffffffffff
ZZZZZZZZZZZZZZZZZZZ
6ppp
pp EEE 29
hhhhhhhhhhJJ
J
3�� ==
8OOOOO 22
zz DD31
2 4 13}}
19 25DD
9 26
7.21
CSE 2331
Report in Range: Running Time
procedure TreeRangeReport(x, kmin, kmax)
1 if (x 6= NIL) then
2 if (kmin ≤ x.key) then
3 TreeRangeReport(x.left, kmin, kmax);
4 end
5 if (kmin ≤ x.key ≤ kmax) then print x.key;
6 if (x.key ≤ kmax) then
7 TreeRangeReport(x.right, kmin, kmax);
8 end
9 end
h = tree heightI = number of nodes reported.
Running time:
7.22
CSE 2331
Report All HV Line Segment Intersections
1 T.Init();
2 L← endpoints of all line segments sorted by x-coordinate;
3 foreach point p ∈ L do4 if (p is the left endpoint of a horizontal segment σH) then5 Create new tree node u with
6 u.key = σH .y and u.data.lineseg = σH ;
7 u.data.lineseg← σH ;
8 TreeInsert (T , u);
9 else if (p is the lower endpoint of a vertical segment σV ) then10 TreeRangeReport (T.root, σV .miny, σV .maxy);
11 else if (p is the right endpoint of a horizontal segment σH) then12 u← tree node representing σH ;
13 TreeDelete (T ,u);
14 end
15 end
7.23
CSE 2331
Count HV Line Segment Intersections
Given:
• set LH of horizontal line segments;
• set LV of vertical line segments.
For each σV ∈ LV , count number of intersections with lines in LH .
7.25
CSE 2331
Count HV Line Segment Intersections
1 count← 0;
2 T.Init();
3 L← endpoints of all line segments sorted by x-coordinate;
4 foreach point p ∈ L do5 if (p is the left endpoint of a horizontal segment σH) then6 Create new tree node u;
7 u.key← σH .y;
8 u.data.lineseg← σH ;
9 TreeInsert (T , u);
10 else if (p is the lower endpoint of a vertical segment σV ) then11 count← count+ CountNumIntersections (T , σV );
12 else if (p is the right endpoint of a horizontal segment σH) then13 u← tree node representing σH ;
14 TreeDelete (T ,u);
15 end
16 end
7.27
CSE 2331
Binary Search Tree: Counting
For each node u in the binary search tree, add a field u.size which
stores the number of nodes in the subtree rooted at u.
20
eeeeeeeeeeeeeeee
ZZZZZZZZZZZZZZZZZZZZZ
8lllllll ==
48nnnnn
2KK
KK 11UUUUUUUUU 37
iiiiiiiii BB
5�� 99
17nnnnn BB
23PPPPP 42
4 7 13BB
18 29}} BB
14 26 31
7.28
CSE 2331
Count Nodes Greater Than or Equal to
function TreeCountGE(x, K)
/* Return number of nodes with keys greater than or equalto K in subtree rooted at x */
1 count← 0;
2 v ← x;
3 while (v 6= NIL) do4 if (v.key ≥ K) then5 count← count + 1;
6 if (v.right 6= NIL) then count← count + v.right.size;
7 v ← v.left;
8 else9 v ← v.right;
10 end
11 end
12 return (count);
7.29
CSE 2331
Count Number of Nodes in Range
function TreeRangeCount(x, kmin, kmax)
/* Return number of nodes with keys in range [kmin, kmax] in
subtree rooted at x */
1 v ← x;
2 while (v 6= NIL) and (v.key 6∈ [kmin, kmax]) do
3 if (v.key ≤ kmin) then v ← v.right;
4 else if (v.key ≥ kmax) then v ← v.left;
5 end
6 if (v 6= NIL) then
7 countL ← TreeCountGE(v.left, kmin);
8 countR ← TreeCountLE(v.right, kmax);
9 return (1 + countL + countR);
10 else return (0);
7.30
CSE 2331
Count HV Line Segment Intersections
1 count← 0;
2 T.Init();
3 L← endpoints of all line segments sorted by x-coordinate;
4 foreach point p ∈ L do5 if (p is the left endpoint of a horizontal segment σH) then6 Create new tree node u;
7 u.key← σH .y;
8 u.data.lineseg← σH ;
9 TreeInsert (T , u);
10 else if (p is the lower endpoint of a vertical segment σV ) then11 count← count+ TreeRangeCount (T , σV .miny, σV .maxy);
12 else if (p is the right endpoint of a horizontal segment σH) then13 u← tree node representing σH ;
14 TreeDelete (T ,u);
15 end
16 end
7.31
CSE 2331
Point Location
Given:
• set Srect of non-intersecting, axis-parallel rectangles;
• set Spoint of points.
Report the rectangle containing each point.
7.33
CSE 2331
Data Structures
• Stack;
• Heap (HeapSort, Prim’s MST algorithm, Dijkstra’s shortest
path algorithm);
• Red-black tree (Line segment intersection);
• Hash table (Duplicate elements, counting number of elements);
• Union-find structure (Connected components).
7.35
CSE 2331
Exercise: Deep Thought - Highest Priority
You are in charge of the supercomputer Deep Thought.
• Deep Thought has an “Input List” of programs which it needs
to process;
• Every minute new programs are being added to the Input List
by scientists from across the world;
• Each program has a priority number from 1 to 2128 − 1;
• Each time Deep Thought completes a program, it selects the
next program with highest priority from the Input List and
works on that program.
Give a data structure for quickly adding new programs to the list
and selecting the next program for Deep Thought to process.
7.36
CSE 2331
Exercise: Deep Thought - Count
Competitive scientists always want to know how many other programs in
the Input List have priority less than their program, and how many have
priority greater than their program.
• Every minute new programs are being added to the Input List by
scientists from across the world;
• Each program has a priority number from 1 to 2128 − 1;
• Each time Deep Thought completes a program, it selects the next
program with highest priority from the Input List and works on that
program.
Give a data structure which allows a scientist to quickly query how many
programs in the Input List have higher priority and how many programs
have lower priority.
7.37
CSE 2331
Exercise: Deep Thought - Wait
Because Deep Thought is such a valuable resource, some scientists
want to hog Deep Thought for their research. For this reason, you
instituted a rule that after a scientist has submitted a program, she
must wait 24 hours until she can submit another program.
• Each scientist has a 128 bit password which she must submit
with her program;
• Each program has a submission time which is the time the
program was added to the Input List.
Give a data structure for quickly determining if the scientist who
submitted a program has waited 24 hours from the last time she
submitted a program.
7.38
CSE 2331
Exercise: Deep Thought - Median Priority
Some selfish scientists have been assigning high priority to their programs
to get them processed first. To “punish” such selfishness, Deep Thought
no longer selects the program with highest priority from the Input List.
Instead, Deep Thought selects the program with median priority.
• Every minute new programs are being added to the Input List by
scientists from across the world;
• Each time Deep Thought completes a program, it selects the next
program with median priority from the Input List and works on that
program.
• Let n be the number of programs in the Input List. The median
priority is an integer I such that ⌊n/2⌋ elements have priority less
than or equal to I and ⌈n/2⌉ elements have priority greater than or
equal to I.
Give a data structure for quickly adding new programs to the list and
selecting the next program for Deep Thought to process.
7.39