KD-Trees and Range search

imagine you have a database containing thousands maybe millions of medical patients each dataset with us reduce suggest the patient’s height weight whether or not the patient has been diagnosed with a particular disease etc the new patient comes in and to get the hint of our particular treatment who will come out we may want to compare two previous cases with similar patients let’s say we want to search on database for people who trade in a 1780 kilo between 170 180 centimeters and who have been diagnosed with let’s say some kind of heart problem how can we in an efficient way search for all patients within this multi-dimensional range we cannot possibly go through all elements in the database every time we want to make ourselves we therefore need to have a data structured in ordered way speed exam but how do we order mouths dimensional data the answer is a data structure called KD tree or K dimensional tree so how do we build such a data structure imagine that we have a space with a number of points we start with finding the median of the points x values and split the set into two different branches in each branch we will now find the median along the y axis and divide into branches once again we will continue like this alternating between splitting along the x and y axis until each cell only contains one point we now have our finished tree Sparky now how do we use this structure to find the points in a given range let’s say we want to find all points between this and this x-value and between this and this y value we start by looking at a tree root since both branches intersects with the range we need to continue searching along both branches we now look at the second level and see that only the upper or right branch intersects with the range we can thus discard the left one while continuing on the right door upper branch we see that both branches intersect the range Madonn has a look at the actual points in the excel and see it lays within range for our main right brows we do the same thing we look at the upper and lower branches and see if they intersect with the range continuing along those that do intersect and discarding those who don’t when we reach a leaf we look if the point lies within the range or not and that is a final range search for multi-dimensional data you