摘自mit bbs的pdf
177. Given a list of intervals, 比如 (10,20),(30,50)...,and a target interval,比如(18,35) 找有多少 overlap.
每个节点记录信息:
•Eachnode
v hasa point
v.ptandtwo lists
v.left and
v.right.
•
u.pt < v.pt fornodes
u inleft subtree of
v.
•
u.pt > v.pt fornodes
u inright subtree of
v.
•So,it is a binary search tree on
pt.
•Intervalswith
r
i
< v.pt arestored in the left subtree of
v.
•Intervalswith
l
i
>v.ptare stored in the right subtree of
v.
•Intervalswith
l
i
<=v.pt
<=
r
i are stored in
v.
§
v.lefthas these intervals sorted by
l
i.
§
v.righthas these intervals sorted by
r
i.
大致思路就是这样,懒得解释了==
每个节点包括当前分割点的值val,左侧树的interval都是小于val,右侧都是大于val。
同时还维持了2个链表,左侧链表按照start排序,右侧按照end排序。这些Intervals放在这个节点因为它和val有重叠。
当来一个查询时:
分情况判断是否和当前节点的val重叠,然后左右递归。
我真懒,懒得详细解释,拷贝从某个ppt。
网上搜Interval Tree会有挺多资料的,但是貌似的英文的讲的比较好,因为中文描述就我这种打酱油的解释==