I have a collection of 2-D points which represent a 1-variable function.
Given a random input value, I have to select the closest value.
Example:
Curve:
(1,5)
(2,8)
(5,9)
Input: 3 Output: 8
My main concern is speed, space doesn’t matter as much.
Which data structure would be best?
EDIT: The table is static, it won’t change during runtime
It depends upon whether the table is static or dynamic.
If it’s static data, simple sorted array and binary search will get the job done: search for the key, if it isn’t found, check the index above and below to see which is closer to the search key, and return its associated value.
If the data is dynamic, I’d go with a B+Tree variant (though any balanced tree structure should work). Essentially the same algorithm, but you’d be checking sibling nodes, instead of just checking adjacent array cells.