• Like
二叉查找树范围查询算法
Upcoming SlideShare
Loading in...5
×

二叉查找树范围查询算法

  • 294 views
Uploaded on

 

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
294
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Range Queries 17-1 Range Queries on Binary Search TreesAlgorithm Ê Ò ÉÙ ÖÝË ÑÔÐ (Ì , ½ , ¾ ): 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 QueriesAlgorithm Ê Ò ÉÙ ÖÝ ÓÐÓÙÖ( Ì , ½ , ¾ , 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 Ì .root10: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)11: else below the splitting point12: if key(Ì .root) < splitValue then right child is black13: RangeQueryColour(Ì .left, ½ , ¾ , splitValue)14: report Ì .root15: report all nodes in Ì .right16: else left child is black17: report all nodes in Ì .left18: report Ì .root19: RangeQueryColour(Ì .right, ½ , ¾ , splitValue)
  • 4. Range Queries 17-4 Quad-tree example * * * * * * * * * * * * * * * * * * * *
  • 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-treesAlgorithm Ë ÑÔÐ Ù Ð ÌÖ ( 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 Ý -coordinate10: ̽ = SimpleBuildkdTree(È , level ·½ )11: ̾ = SimpleBuildkdTree(È , level Ö ·½ )12: return tree with subtrees ̽ and ̾
  • 8. Range Queries 17-8 Building kd-trees with pre-sortingAlgorithm ÈÖ ×ÓÖØ Ù Ð ÌÖ ( 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: else13: [...] // Similarly with Ý -coordinate14: ·½ ̽ = buildKdTree(ÈÜ , ÈÝ , level )15: Ö ·½ Ö Ì¾ = buildKdTree(ÈÜ , ÈÝ , level )16: return tree with subtrees ̽ and ̾
  • 9. Range Queries 17-9 Range treesBST 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)