1. Range Queries 17-1
Range Queries on Binary Search Trees
Algorithm Ê Ò ÉÙ ÖÝË ÑÔÐ (Ì , ½ , ¾ ):
1: If Ì is empty, return
2: if key(Ì .root) < ½ then
3: RangeQuerySimple(Ì .right, ½ , ¾ )
4: else if key(Ì .root) > ¾ then
5: RangeQuerySimple(Ì .left, ½ , ¾ )
6: else
7: RangeQuerySimple(Ì .left, ½ , ¾ )
8: report Ì .root
9: RangeQuerySimple(Ì .right, ½ , ¾ )
2. Range Queries 17-2
Range Queries on Binary Search Trees
35
18 45
10 25 42
7 14 22 31
4 8 12 16 30
9
¯ query range ¾℄
3. Range Queries 17-3
Coloring for Range Queries
Algorithm Ê Ò ÉÙ ÖÝ ÓÐÓÙÖ( Ì , ½ , ¾ , splitValue = undef):
1: if Ì is non-empty then Ì .root is gray
2: if key(Ì .root) < ½ then left child is white
3: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
4: else if key(Ì .root > ¾ then right child is white
5: RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
6: else if splitValue undef then root is splitting point
7: splitValue = key(Ì .root)
8: RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
9: report Ì .root
10: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
11: else below the splitting point
12: if key(Ì .root) < splitValue then right child is black
13: RangeQueryColour(Ì .left, ½ , ¾ , splitValue)
14: report Ì .root
15: report all nodes in Ì .right
16: else left child is black
17: report all nodes in Ì .left
18: report Ì .root
19: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
5. Range Queries 17-5
kd-tree example
140 *
130 *
120 *
110 *
100 *
80 *
40 *
30 *
10 *
10 20 30 40 50 70 80 110 140
Ü ¼
Ý ½¾¼ Ý ¿¼
Ü ¿¼ Ü ½¼ Ü ½½¼ Ü ¼
Ý ½½¼ * * * * * * *
* *
6. Range Queries 17-6
kd-tree range queries
Ü ¼
Ý ½¾¼ Ý ¿¼
Ü ¿¼ Ü ½¼ Ü ½½¼ Ü ¼
Ý ½½¼ * * * * * Ý ¼ *
* * Ü ¼ *
* *
¯ query range ¼ ¼℄ ¢ ¾¼ ¼℄
7. Range Queries 17-7
Building kd-trees
Algorithm Ë ÑÔÐ Ù Ð ÌÖ ( points È , int level ):
1: if È ½ then
2: Build one-node tree and return
3: else
4: if level is even then
5: Let ÜÑ = median Ü-coordinate among È
6: set È points in È with Ü-coord. ÜÑ
7: set È Ö points in È with Ü-coord. ÜÑ
8: else
9: [...] // Similarly with Ý -coordinate
10: ̽ = SimpleBuildkdTree(È , level ·½ )
11: ̾ = SimpleBuildkdTree(È , level
Ö
·½ )
12: return tree with subtrees ̽ and ̾
8. Range Queries 17-8
Building kd-trees with pre-sorting
Algorithm ÈÖ ×ÓÖØ Ù Ð ÌÖ ( points È ):
1: Let ÈÜ be a copy of È , sort by Ü-coordinate
2: Let ÈÝ be a copy of È , sort by Ý -coordinate
3: buildKdTree(ÈÜ , ÈÝ , 0)
Algorithm Ù Ð Ã ÌÖ ( points ÈÜ , points ÈÝ , level ):
1: // ÈÜ and ÈÝ are same points, sorted by Ü and Ý respectively
2: if ÈÜ ½ then
3: Build one-node tree and return
4: else
5: if level is even then
6: Let ÜÑ = median Ü-coordinate among È
7: set ÈÜ points in ÈÜ with Ü-coord. ÜÑ
Ö
8: set ÈÜ points in ÈÜ with Ü-coord. ÜÑ
Ö
9: set ÈÝ points in ÈÝ with Ü-coord. ÜÑ
10: set ÈÝ points in ÈÝ with Ü-coord. ÜÑ
11: // All sub-lists maintain their relative order.
12: else
13: [...] // Similarly with Ý -coordinate
14: ·½
̽ = buildKdTree(ÈÜ , ÈÝ , level )
15:
Ö
·½
Ö
̾ = buildKdTree(ÈÜ , ÈÝ , level )
16: return tree with subtrees ̽ and ̾
9. Range Queries 17-9
Range trees
BST by Ü-coordinate:
(60,70)
(20,50) (80,60)
(10,80) (40,10) (70,90) (90,30)
(30,40) (50,20)
BST of subtree by Ý -coordinate:
(30,40)
(40,10) (20,50)
(50,20) (10,80)