Exercise C: k-d Trees

Objectives

Exercises

For the list of points $(12.0, 8.0), (18.0, 11.0), (15.0, 15.0), (8.0, 12.0), (3.0, 18.0), (10.0, 1.0)\ , (17.0, 5.0)$

Solution for 1st List of Points

After adding points one by one (rotate 90 degrees clockwise) for top-down
view.
2               (15.0, 15.0)
1        (18.0, 11.0)
2               (17.0,  5.0)
0 (12.0,  8.0)
2               ( 3.0, 18.0)
1        ( 8.0, 12.0)
2               (10.0,  1.0)

Regions for each node
2 (15.0, 15.0) [(12.0, 11.0)-(inf, inf)]
1 (18.0, 11.0) [(12.0, -inf)-(inf, inf)]
2 (17.0, 5.0) [(12.0, -inf)-(inf, 11.0)]
0 (12.0, 8.0) [(-inf, -inf)-(inf, inf)]
2 (3.0, 18.0) [(-inf, 12.0)-(12.0, inf)]
1 (8.0, 12.0) [(-inf, -inf)-(12.0, inf)]
2 (10.0, 1.0) [(-inf, -inf)-(12.0, 12.0)]

Building the balanced tree
X: [(3.0, 18.0), (8.0, 12.0), (10.0, 1.0), (12.0, 8.0), (15.0, 15.0), (17.0, 5.0), (18.0, 11.0)]
Y: [(10.0, 1.0), (17.0, 5.0), (12.0, 8.0), (18.0, 11.0), (8.0, 12.0), (15.0, 15.0), (3.0, 18.0)]
selecting (12.0, 8.0) as the root

left subtree
Y: [(10.0, 1.0), (8.0, 12.0), (3.0, 18.0)]
X: [(3.0, 18.0), (8.0, 12.0), (10.0, 1.0)]
selecting (8.0, 12.0) as the root

left left subtree
X: [(10.0, 1.0)]
Y: [(10.0, 1.0)]
selecting (10.0, 1.0) as the root

left right subtree
X: [(3.0, 18.0)]
Y: [(3.0, 18.0)]
selecting (3.0, 18.0) as the root

right subtree
Y: [(17.0, 5.0), (18.0, 11.0), (15.0, 15.0)]
X: [(15.0, 15.0), (17.0, 5.0), (18.0, 11.0)]
selecting (18.0, 11.0) as the root

right left subtree
X: [(17.0, 5.0)]
Y: [(17.0, 5.0)]
selecting (17.0, 5.0) as the root

right right subtree
X: [(15.0, 15.0)]
Y: [(15.0, 15.0)]
selecting (15.0, 15.0) as the root

Built Tree
2               (15.0, 15.0)
1        (18.0, 11.0)
2               (17.0,  5.0)
0 (12.0,  8.0)
2               ( 3.0, 18.0)
1        ( 8.0, 12.0)
2               (10.0,  1.0)

Regions for each node
2 (15.0, 15.0) [(12.0, 11.0)-(inf, inf)]
1 (18.0, 11.0) [(12.0, -inf)-(inf, inf)]
2 (17.0, 5.0) [(12.0, -inf)-(inf, 11.0)]
0 (12.0, 8.0) [(-inf, -inf)-(inf, inf)]
2 (3.0, 18.0) [(-inf, 12.0)-(12.0, inf)]
1 (8.0, 12.0) [(-inf, -inf)-(12.0, inf)]
2 (10.0, 1.0) [(-inf, -inf)-(12.0, 12.0)]



not exploring subtree (18.0, 11.0) with region (12.0, -inf)-(inf, inf)
        distance from (4.0, 5.0) to best so far (10.0, 1.0) is 7.211103
        minimum distance to this subtree's region is 8.000000

7.211103
(10.0, 1.0)

Solution for 2nd List of Points

Run the executable /c/cs223/hw5/KDTreeExamples with first command-line argument equal to the integer sum of the x- and y-coordinates of the left-left grandchild of the root when building a balanced k-d tree from the 2nd list of points and making the left subtree contain one less point than the right subtree in subtrees that contain an even number of points. The second command-line argument is 2 and the third is either --add, --build, or --nearest where --nearest is followed by the point you want to find the nearest neighbor of.
/c/cs223/hw5/KDTreeExamples password 2 --add
/c/cs223/hw5/KDTreeExamples password 2 --build
/c/cs223/hw5/KDTreeExamples password 2 --nearest 4 5

Run with no arguments to see other options.