07.search Tree Applications
07.search Tree Applications
A
Interval Tree
on the x-axis:
median
A A B B
C C D D E E
F F G G H H
X X
xmid(v)
L R
Smid
Sleft Sright
A A B B
C C D D E E
F F G G H H
X X
median
A A B B
C C D D E E
F F G G H H
X X
[D G B> <G D B]
G
[A F> <F A] [H X E> <E H X]
F E
[C> <C]
C
A A B B
C C D D E E
F F G G H H
X X
[D G B> <G D B]
G
[A F> <F A] [H X E> <E H X]
F E
[C> <C]
C
A A B B
C C D D E E
F F G G H H
X X
[D G B> <G D B]
G
[A F> <F A] [H X E> <E H X]
F E
[C> <C]
C
A A B B
C C D D E E
F F G G H H
X X
[D G B> <G D B]
G
[A F> <F A] [H X E> <E H X]
F E
[C> <C]
C
xmid(v) qx
if ( ! v ) return; //base
if ( qx < xmid(v) ) E E
C C
all intervals of Smid(v) containing qx
B B
queryIntervalTree( lc(v), qx )
A A
else if ( xmid(v) < qx )
use R-list to report L R
all intervals of Smid(v) containing qx
Smid
queryIntervalTree( rc(v), qx )
else //with a probability ≈ 0 Sleft Sright
report all segments of Smid( v ) //both rc(v) & lc(v) can be ignored
A A B B
C C D D E E
F F G G H H
X X
[D G B> <G D B]
G
[A F> <F A] [H X E> <E H X]
F E
[C> <C]
C
B
Segment Tree
把一条线分割成不相等的两段,再把这两段按照同样的比例再分成两个
部分。假设第一次分出来的两段中,一段代表可见世界,另一段代表理
智世界。然后再看第二次分成的两段,他们分别代表清楚与不清楚的程
度,你便会发现,可见世界那一段的第一部分是它的影像 [email protected]
Elementary Intervals
p1 p2 p3 p4 p5 p6 p7
p1 p2 p3 p4 p5 p6 p7
Every interval spans (n) EI's and a total space of (n2) is required
For each s of I
InsertSegment( T.root, s )
At each level,
4 nodes are visited
(2 stores + 2 recursions)
O(logn) time
if ( v is a leaf )
return
if ( qx R( lc(v) ) )
Query( lc(v), qx )
Query( rc(v), qx )
At each node v
- in time
C1
Range Query: 1D
你这个人太敏感了。这个社会什么都需要,唯独不需要敏感 [email protected]
1D Range Query
x
x1 x2
x
x1 x2
the interval contains up to O(n) points, which need O(n) time to enumerate
However, how if we
ignore the time for enumerating and count only the searching time?
Sort all points into a sorted vector and add an extra sentinel p[0] = -
x
x1 x2
- Traverse the vector BACKWARD from p[t] and report each point //O(r)
x
x1 x2
C2
Range Query: 2D
[email protected]
Planar Range Query
salary
Let be a planar set
Given
- COUNTING:
- REPORTING:
Binary search
u x and v y
D1
Multi-Level Search Tree: 1D
只在此山中,云深不知处 [email protected]
Structure: A Complete (Balanced) BST
66
39 85
16 52 74 93
8 26 45 61 70 78 90 99
5 12 22 33 42 45 52 61 66 70 74 78 85 90 93 99
1 5 8 12 16 22 26 33 39 42
39 85
16 52 74 93
8 26 45 61 70 78 90 99
5 12 22 33 42 45 52 61 66 70 74 78 85 90 93 99
78~84
1 5 8 12 16 22 26 33 39 42
16~21
Starting from the LCA, traverse path(16) and path(78) once more resp.
17 79
66
39 85
16 52 74 93
8 26 45 61 70 78 90 99
5 12 22 33 42 45 52 61 66 70 74 78 85 90 93 99
78~84
1 5 8 12 16 22 26 33 39 42
16~21
17 79
66
39 85
16 52 74 93
8 26 45 61 70 78 90 99
5 12 22 33 42 45 52 61 66 70 74 78 85 90 93 99
78~84
1 5 8 12 16 22 26 33 39 42
16~21
Region(u) and Region(v) are disjoint iff neither is the ancestor of the other
rejected
D2
Multi-Level Search Tree: 2D & kD
几株不知名的树,已脱下了黄叶
只有那两三片,多么可怜在枝上抖怯
它们感到秋来到,要与世间离别 [email protected]
2D Range Query = x-Query + y-Query
(almost) all
Tree of trees
Query Time = ~
can be built
in time
needs space
in time
in time
be answered in time
E
Range Tree
顺藤摸瓜 [email protected]
Coherence
DIFFERENT y-lists,
So it not necessary
7 7
to store each canonical subset 6 6
5 5
as a BBST 4 4
3 3
2 2
Instead, a sorted y-list simply works 1 1
0 0
do an O(logn) search
in AL and AR resp. 7 7
6 6
5 5
Hence for any y-query with qy,
4 4
3 3
once we know its entry in Av, we can 2 2
1 1
determine its entry in either AL or AR 0 0
F1
kd-Tree: 2D
韦小宝跟著她走到桌边,只见桌上大白布上钉满了几千枚绣花针,几千块碎片已拼成一幅
完整无缺的大地图,难得的是几千片碎皮拼在一起,既没多出一片,也没少了一片
夫仁政,必自經界始...經界既正,分田制祿可坐而定也 [email protected]
思路
为利用BBST来支持二维的区域查询,可以
递归地划分平面,并将分出来的矩形区域组织为一棵二叉树
首先,根节点对应于整个平面
然后,交替地按x、y坐标划分,直至...
为避免歧义,可约定每个矩形区域都是左闭右开、下闭上开
Root = createKdNode()
return( Root )
G G G G
B B B B
F F F F
C E C E C E C E
A D A D A D A D
| | |
{A,B,C,D,F,G,E} {A,B,C,D,F,G,E} {A,B,C,D,F,G,E} {A,B,C,D,F,G,E}
A | | |
{A} {B,C} {D,E} {F,G} {B,C} {D,E} {F,G}
B C D E F G
ABCDEFG
DEG AB CF
EG
树高
任何同层的节点 和 ,都有:
同层的所有节点所对应的区域,无缝地覆盖了整个平面:
, 且
任何一次矩形查询的解,都可以表示为若干个节点所对应 的并 //多少个?
F2
kd-Tree: Query
[email protected]
查询:算法kdSearch(v,R):热刀来切logn层巧克力
// Pre-condition: region( v ) R
- if ( isLeaf( v ) )
if ( inside( v, R ) ) report(v)
return
- if ( region( v->lc ) R )
reportSubtree( v->lc )
else if ( region( v->lc ) R )
kdSearch( v->lc, R ) //recurse
- if ( region( v->rc ) R )
reportSubtree( v->rc )
else if ( region( v->rc ) R )
kdSearch( v->rc, R ) //recurse
compare >>
HBAFGDCJEIQPKLSMRNTO
CABEDFIGJH MNKLOPRQTS
DE GJ NO ST
<< compare
HBAFGDCJEIQPKLSMRNTO
CABEDFIGJH MNKLOPRQTS
DE GJ NO ST
F3
kd-Tree: Complexity
肉眼看不清细节,但他们都知道那是木星所在的位置,
这颗太阳系最大的行星已经坠落到二维平面上了
有人嘲笑这种体系说:为了能发现这个比例中项并组成政府
共同体,按照我的办法,只消求出人口数字的平方根就行了 [email protected]
Preprocessing
T(n)
= 2*T(n/2) + O(n)
= O(nlogn)
of O(logn)
1
+ 2
+ 4
+ ...
+ O(2logn)
= O(n)
No more than 2 of
the 4 grandchildren of each node
will recurse
-
-
Solve to
Can 2d-tree be extended to kd-tree and help HIGHER dimensional range query?