

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Spring 2002 Sharat
CMSC 420: Homework 4: kd-Trees
(a) Complete the following pseudocode used in buildTree(). S[0] is an array containing points sorted on x. S[1] is an array containing points sorted on y. v o i d l e f t P o i n t s ( S , cd ) { i n t n = S. l e n g t h ; m = median ( S ) ; / / S now does NOT c o n t a i n m i f ( cd ) { f o r ( i = 0 ; i < n / 2 ; i ++) l e f t [ 1 ] [ i ] = S [ 1 ] [ i ] ; a = q s o r t ( l e f t [ 1 ] , x ) ; / / a i s a s o r t e d on x now f o r ( i = 0 ; i < n / 2 ; i ++) l e f t [ 0 ] [ i ] = a [ i ] ; } e l s e {
(b) Draw the tree that would be produced when buildTree() is called on the given points. Circle an internal node and write the coordinates of the relevant point inside the circle. Also label the node on its left by stacking two arrays one on top of each other.
(c) What is the big-Oh time complexity of leftPoints() using the procedure in Step (a)? Explain briefly.
(d) Update leftPoints() so that buildTree() runs in O(n log n) time