Docsity

CMSC 420 Homework 4: kd-Trees - Prof. Sharat Chandran, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Spring 2002 Sharat
CMSC 420: Homework 4: kd-Trees
•Handed out: 4/30 Due: 5/9 before class starts. Late homeworks will not be accepted.
•Please answer in the space provided
•(Optional) Please write (if true) in your handwriting and sign: ”I pledge on my honor that
I have not given or received any unauthorized assistance on this assignment or any previous
homework.” Note that you are permitted and encouraged to discuss programming assignments,
not homework.
1. (Building kd-trees statically). Assume that the points (35,90), (70,80), (10,75) (80,40), (50,90),
(70,30), (90,60), (50,25), (25,10), (20,50), and (60,10) are given in advance, so you can prepro-
cess them in a sorted manner. The following conventions apply: Sorting is in descending order;
while sorting on a coordinate, break ties by considering the other coordinate; the median of a
sorted list Ai,i= 0 . . . 2k−1, k > 0 is k; splitting on xcorresponds to cd == 0;
(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 oi 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 = m edi an ( S ) ; / / S now do es NOT c on t ai 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 o n x now
f o r ( i = 0 ; i <n/ 2; i ++) l e f t [ 0 ] [ i ] = a [ i ] ;
}else {
(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.
pf2

Partial preview of the text

Download CMSC 420 Homework 4: kd-Trees - Prof. Sharat Chandran and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Spring 2002 Sharat

CMSC 420: Homework 4: kd-Trees

  • Handed out: 4/30 Due: 5/9 before class starts. Late homeworks will not be accepted.
  • Please answer in the space provided
  • (Optional) Please write (if true) in your handwriting and sign: ”I pledge on my honor that I have not given or received any unauthorized assistance on this assignment or any previous homework.” Note that you are permitted and encouraged to discuss programming assignments, not homework.
  1. (Building kd-trees statically). Assume that the points (35, 90), (70, 80), (10, 75) (80, 40), (50, 90), (70, 30), (90, 60), (50, 25), (25, 10), (20, 50), and (60, 10) are given in advance, so you can prepro- cess them in a sorted manner. The following conventions apply: Sorting is in descending order; while sorting on a coordinate, break ties by considering the other coordinate; the median of a sorted list Ai, i = 0... 2 k − 1, k > 0 is k; splitting on x corresponds to cd == 0;

(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

  1. (Nearest neighbor, no Priority Q version) In the example shown, the distance between the query point (40, 50) and two nodes is not computed. Move the query point (40, 50) to an alternate location so that all 11 distances must be necessarily computed by process(). State the order in which the nodes are processed. What is the alternate location? (If no such location is possible, state that this is the case).
  2. (Optional) As in question 2, but using a priority queue.