11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 13.3 Sp<strong>at</strong>ial <strong>D<strong>at</strong>a</strong> <strong>Structures</strong> 443CxA (40, 45)BADyxB (15, 70)C (70, 10)D (69, 50)EFyE (66, 85) F (85, 90)(a)(b)Figure 13.11 Example of a k-d tree. (a) The k-d tree decomposition for a 128 ×128-unit region containing seven d<strong>at</strong>a points. (b) The k-d tree for the region of (a).space into rectangles th<strong>at</strong> show the extent of where nodes can fall in the varioussubtrees.Searching a k-d tree for the record with a specified xy-coordin<strong>at</strong>e is like searchinga BST, except th<strong>at</strong> each level of the k-d tree is associ<strong>at</strong>ed with a particular discrimin<strong>at</strong>or.Example 13.5 Consider searching the k-d tree for a record loc<strong>at</strong>ed <strong>at</strong> P =(69, 50). First compare P with the point stored <strong>at</strong> the root (record A inFigure 13.11). If P m<strong>at</strong>ches the loc<strong>at</strong>ion of A, then the search is successful.In this example the positions do not m<strong>at</strong>ch (A’s loc<strong>at</strong>ion (40, 45) is notthe same as (69, 50)), so the search must continue. The x value of A iscompared with th<strong>at</strong> of P to determine in which direction to branch. BecauseA x ’s value of 40 is less than P’s x value of 69, we branch to the right subtree(all cities with x value gre<strong>at</strong>er than or equal to 40 are in the right subtree).A y does not affect the decision on which way to branch <strong>at</strong> this level. At thesecond level, P does not m<strong>at</strong>ch record C’s position, so another branch mustbe taken. However, <strong>at</strong> this level we branch based on the rel<strong>at</strong>ive y valuesof point P <strong>and</strong> record C (because 1 mod 2 = 1, which corresponds to they-coordin<strong>at</strong>e). Because C y ’s value of 10 is less than P y ’s value of 50, webranch to the right. At this point, P is compared against the position of D.A m<strong>at</strong>ch is made <strong>and</strong> the search is successful.If the search process reaches a null pointer, then th<strong>at</strong> point is not containedin the tree. Here is a k-d tree search implement<strong>at</strong>ion, equivalent to the findhelpfunction of the BST class. KD class priv<strong>at</strong>e member D stores the key’s dimension.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!