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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

442 Chap. 13 Advanced Tree <strong>Structures</strong>This section presents two sp<strong>at</strong>ial d<strong>at</strong>a structures for storing point d<strong>at</strong>a in two ormore dimensions. They are the k-d tree <strong>and</strong> the PR quadtree. The k-d tree is an<strong>at</strong>ural extension of the BST to multiple dimensions. It is a binary tree whose splittingdecisions altern<strong>at</strong>e among the key dimensions. Like the BST, the k-d tree usesobject space decomposition. The PR quadtree uses key space decomposition <strong>and</strong> sois a form of trie. It is a binary tree only for one-dimensional keys (in which case itis a trie with a binary alphabet). For d dimensions it has 2 d branches. Thus, in twodimensions, the PR quadtree has four branches (hence the name “quadtree”), splittingspace into four equal-sized quadrants <strong>at</strong> each branch. Section 13.3.3 brieflymentions two other vari<strong>at</strong>ions on these d<strong>at</strong>a structures, the bintree <strong>and</strong> the pointquadtree. These four structures cover all four combin<strong>at</strong>ions of object versus keyspace decomposition on the one h<strong>and</strong>, <strong>and</strong> multi-level binary versus 2 d -way branchingon the other. Section 13.3.4 briefly discusses sp<strong>at</strong>ial d<strong>at</strong>a structures for storingother types of sp<strong>at</strong>ial d<strong>at</strong>a.13.3.1 The K-D TreeThe k-d tree is a modific<strong>at</strong>ion to the BST th<strong>at</strong> allows for efficient processing ofmultidimensional keys. The k-d tree differs from the BST in th<strong>at</strong> each level ofthe k-d tree makes branching decisions based on a particular search key associ<strong>at</strong>edwith th<strong>at</strong> level, called the discrimin<strong>at</strong>or. In principle, the k-d tree could be used tounify key searching across any arbitrary set of keys such as name <strong>and</strong> zipcode. Butin practice, it is nearly always used to support search on multidimensional coordin<strong>at</strong>es,such as loc<strong>at</strong>ions in 2D or 3D space. We define the discrimin<strong>at</strong>or <strong>at</strong> level ito be i mod k for k dimensions. For example, assume th<strong>at</strong> we store d<strong>at</strong>a organizedby xy-coordin<strong>at</strong>es. In this case, k is 2 (there are two coordin<strong>at</strong>es), with the x-coordin<strong>at</strong>e field arbitrarily design<strong>at</strong>ed key 0, <strong>and</strong> the y-coordin<strong>at</strong>e field design<strong>at</strong>edkey 1. At each level, the discrimin<strong>at</strong>or altern<strong>at</strong>es between x <strong>and</strong> y. Thus, a node N<strong>at</strong> level 0 (the root) would have in its left subtree only nodes whose x values are lessthan N x (because x is search key 0, <strong>and</strong> 0 mod 2 = 0). The right subtree wouldcontain nodes whose x values are gre<strong>at</strong>er than N x . A node M <strong>at</strong> level 1 wouldhave in its left subtree only nodes whose y values are less than M y . There is no restrictionon the rel<strong>at</strong>ive values of M x <strong>and</strong> the x values of M’s descendants, becausebranching decisions made <strong>at</strong> M are based solely on the y coordin<strong>at</strong>e. Figure 13.11shows an example of how a collection of two-dimensional points would be storedin a k-d tree.In Figure 13.11 the region containing the points is (arbitrarily) restricted to a128 × 128 square, <strong>and</strong> each internal node splits the search space. Each split isshown by a line, vertical for nodes with x discrimin<strong>at</strong>ors <strong>and</strong> horizontal for nodeswith y discrimin<strong>at</strong>ors. The root node splits the space into two parts; its childrenfurther subdivide the space into smaller parts. The children’s split lines do notcross the root’s split line. Thus, each node in the k-d tree helps to decompose the

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

Saved successfully!

Ooh no, something went wrong!