12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

464 Chap. 13 Advanced Tree <strong>Structures</strong>private KDNodefindmin(KDNode rt, int descrim, int level) {KDNode temp1, temp2;int[] key1 = null;int[] key2 = null;if (rt == null) return null;temp1 = findmin(rt.left(), descrim, (level+1)%D);if (temp1 != null) key1 = temp1.key();if (descrim != level) {temp2 = findmin(rt.right(), descrim, (level+1)%D);if (temp2 != null) key2 = temp2.key();if ((temp1 == null) || ((temp2 != null) &&(key1[descrim] > key2[descrim])))temp1 = temp2;key1 = key2;} // Now, temp1 has the smaller valueint[] rtkey = rt.key();if ((temp1 == null) || (key1[descrim] > rtkey[descrim]))return rt;elsereturn temp1;}Figure 13.12 The k-d tree findmin method. On levels using the minimumvalue’s discrimina<strong>to</strong>r, branching is <strong>to</strong> the left. On other levels, both children’ssubtrees must be visited. Helper function min takes two nodes <strong>and</strong> a discrimina<strong>to</strong>ras input, <strong>and</strong> returns the node with the smaller value in that discrimina<strong>to</strong>r.Note that we can replace the node <strong>to</strong> be deleted with the least-valued nodefrom the right subtree only if the right subtree exists. If it does not, then a suitablereplacement must be found in the left subtree. Unfortunately, it is not satisfac<strong>to</strong>ry<strong>to</strong> replace N’s record with the record having the greatest value for the discrimina<strong>to</strong>rin the left subtree, because this new value might be duplicated. If so, then wewould have equal values for the discrimina<strong>to</strong>r in N’s left subtree, which violatesthe ordering rules for the k-d tree. Fortunately, there is a simple solution <strong>to</strong> theproblem. We first move the left subtree of node N <strong>to</strong> become the right subtree (i.e.,we simply swap the values of N’s left <strong>and</strong> right child pointers). At this point, weproceed with the normal deletion process, replacing the record of N <strong>to</strong> be deletedwith the record containing the least value of the discrimina<strong>to</strong>r from what is nowN’s right subtree.Assume that we want <strong>to</strong> print out a list of all records that are within a certaindistance d of a given point P. We will use Euclidean distance, that is, point P isdefined <strong>to</strong> be within distance d of point N if 1√(P x − N x ) 2 + (P y − N y ) 2 ≤ d.1 A more efficient computation is (P x − N x) 2 + (P y − N y) 2 ≤ d 2 . This avoids performing asquare root function.

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

Saved successfully!

Ooh no, something went wrong!