Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

# 二叉查找树范围查询算法

329

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

No Downloads
Views
Total Views
329
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Range Queries 17-1 Range Queries on Binary Search TreesAlgorithm &#xCA; &#xD2; &#xC9;&#xD9; &#xD6;&#xDD;&#xCB; &#xD1;&#xD4;&#xD0; (&#xCC; , &#xBD; , &#xBE; ): 1: If &#xCC; is empty, return 2: if key(&#xCC; .root) &lt; &#xBD; then 3: RangeQuerySimple(&#xCC; .right, &#xBD; , &#xBE; ) 4: else if key(&#xCC; .root) &gt; &#xBE; then 5: RangeQuerySimple(&#xCC; .left, &#xBD; , &#xBE; ) 6: else 7: RangeQuerySimple(&#xCC; .left, &#xBD; , &#xBE; ) 8: report &#xCC; .root 9: RangeQuerySimple(&#xCC; .right, &#xBD; , &#xBE; )
• 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 &#xAF; query range &#xBE;&#x2104;
• 3. Range Queries 17-3 Coloring for Range QueriesAlgorithm &#xCA; &#xD2; &#xC9;&#xD9; &#xD6;&#xDD; &#xD3;&#xD0;&#xD3;&#xD9;&#xD6;( &#xCC; , &#xBD; , &#xBE; , splitValue = undef): 1: if &#xCC; is non-empty then &#xCC; .root is gray 2: if key(&#xCC; .root) &lt; &#xBD; then left child is white 3: RangeQueryColour(&#xCC; .right, &#xBD; , &#xBE; , splitValue) 4: else if key(&#xCC; .root &gt; &#xBE; then right child is white 5: RangeQueryColour(&#xCC; .left, &#xBD; , &#xBE; , splitValue) 6: else if splitValue undef then root is splitting point 7: splitValue = key(&#xCC; .root) 8: RangeQueryColour(&#xCC; .left, &#xBD; , &#xBE; , splitValue) 9: report &#xCC; .root10: RangeQueryColour(&#xCC; .right, &#xBD; , &#xBE; , splitValue)11: else below the splitting point12: if key(&#xCC; .root) &lt; splitValue then right child is black13: RangeQueryColour(&#xCC; .left, &#xBD; , &#xBE; , splitValue)14: report &#xCC; .root15: report all nodes in &#xCC; .right16: else left child is black17: report all nodes in &#xCC; .left18: report &#xCC; .root19: RangeQueryColour(&#xCC; .right, &#xBD; , &#xBE; , 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 &#xDC; &#xBC; &#xDD; &#xBD;&#xBE;&#xBC; &#xDD; &#xBF;&#xBC; &#xDC; &#xBF;&#xBC; &#xDC; &#xBD;&#xBC; &#xDC; &#xBD;&#xBD;&#xBC; &#xDC; &#xBC; &#xDD; &#xBD;&#xBD;&#xBC; * * * * * * * * *
• 6. Range Queries 17-6 kd-tree range queries &#xDC; &#xBC; &#xDD; &#xBD;&#xBE;&#xBC; &#xDD; &#xBF;&#xBC; &#xDC; &#xBF;&#xBC; &#xDC; &#xBD;&#xBC; &#xDC; &#xBD;&#xBD;&#xBC; &#xDC; &#xBC; &#xDD; &#xBD;&#xBD;&#xBC; * * * * * &#xDD; &#xBC; * * * &#xDC; &#xBC; * * * &#xAF; query range &#xBC; &#xBC;&#x2104; &#xA2; &#xBE;&#xBC; &#xBC;&#x2104;
• 7. Range Queries 17-7 Building kd-treesAlgorithm &#xCB; &#xD1;&#xD4;&#xD0; &#xD9; &#xD0; &#xCC;&#xD6; ( points &#xC8; , int level ): 1: if &#xC8; &#xBD; then 2: Build one-node tree and return 3: else 4: if level is even then 5: Let &#xDC;&#xD1; = median &#xDC;-coordinate among &#xC8; 6: set &#xC8; points in &#xC8; with &#xDC;-coord. &#xDC;&#xD1; 7: set &#xC8; &#xD6; points in &#xC8; with &#xDC;-coord. &#xDC;&#xD1; 8: else 9: [...] // Similarly with &#xDD; -coordinate10: &#xCC;&#xBD; = SimpleBuildkdTree(&#xC8; , level &#xB7;&#xBD; )11: &#xCC;&#xBE; = SimpleBuildkdTree(&#xC8; , level &#xD6; &#xB7;&#xBD; )12: return tree with subtrees &#xCC;&#xBD; and &#xCC;&#xBE;
• 8. Range Queries 17-8 Building kd-trees with pre-sortingAlgorithm &#xC8;&#xD6; &#xD7;&#xD3;&#xD6;&#xD8; &#xD9; &#xD0; &#xCC;&#xD6; ( points &#xC8; ): 1: Let &#xC8;&#xDC; be a copy of &#xC8; , sort by &#xDC;-coordinate 2: Let &#xC8;&#xDD; be a copy of &#xC8; , sort by &#xDD; -coordinate 3: buildKdTree(&#xC8;&#xDC; , &#xC8;&#xDD; , 0)Algorithm &#xD9; &#xD0; &#xC3; &#xCC;&#xD6; ( points &#xC8;&#xDC; , points &#xC8;&#xDD; , level ): 1: // &#xC8;&#xDC; and &#xC8;&#xDD; are same points, sorted by &#xDC; and &#xDD; respectively 2: if &#xC8;&#xDC; &#xBD; then 3: Build one-node tree and return 4: else 5: if level is even then 6: Let &#xDC;&#xD1; = median &#xDC;-coordinate among &#xC8; 7: set &#xC8;&#xDC; points in &#xC8;&#xDC; with &#xDC;-coord. &#xDC;&#xD1; &#xD6; 8: set &#xC8;&#xDC; points in &#xC8;&#xDC; with &#xDC;-coord. &#xDC;&#xD1; &#xD6; 9: set &#xC8;&#xDD; points in &#xC8;&#xDD; with &#xDC;-coord. &#xDC;&#xD1;10: set &#xC8;&#xDD; points in &#xC8;&#xDD; with &#xDC;-coord. &#xDC;&#xD1;11: // All sub-lists maintain their relative order.12: else13: [...] // Similarly with &#xDD; -coordinate14: &#xB7;&#xBD; &#xCC;&#xBD; = buildKdTree(&#xC8;&#xDC; , &#xC8;&#xDD; , level )15: &#xD6; &#xB7;&#xBD; &#xD6; &#xCC;&#xBE; = buildKdTree(&#xC8;&#xDC; , &#xC8;&#xDD; , level )16: return tree with subtrees &#xCC;&#xBD; and &#xCC;&#xBE;
• 9. Range Queries 17-9 Range treesBST by &#xDC;-coordinate: (60,70) (20,50) (80,60) (10,80) (40,10) (70,90) (90,30) (30,40) (50,20) BST of subtree by &#xDD; -coordinate: (30,40) (40,10) (20,50) (50,20) (10,80)