Very simple notes of KD tree (to be updated later)

Today (18.5.4) roommate A suddenly asked me how to get started with algorithms, and excitedly gave him Amway Deng's "Data Structure", but then he asked me if I could get started quickly within two weeks, after all, I plan to engage in Machine Learning, Then he took out his mobile phone and looked at the arrangement given by his teacher: python learning and implementing K-nearest neighbor algorithm/decision tree/naive XXX/support vector machine (???), within two weeks, choose one of the four

It turned out that I thought that the algorithm I learned was similar to this, and I came to ask for advice. I know π.

However, I still touched a KD tree 10 months ago. I should still remember the K nearest neighbors. I will recall this article in a close-up and urge myself to update the questions.

//The following text is not implemented by code
//It will be updated later
//

The data structure that K-nearest neighbors rely on: KD tree

Simply put, it is an extended K-dimensional binary tree. Each node in the tree represents both an actual point and a hyperplane that cuts the space. Then we need to know how it expresses the space.

Specific implementation points

In the KD tree, the comparison of each layer of nodes is to compare a certain dimension as a key, the small one goes to the left subtree, and the large one goes to the right subtree. In ACM, in order to save trouble, each dimension is selected as the key. One layer is directly updated to the next dimension comparison, reciprocating cycle, such as \(K=3\) is the traversal method of \(x→y→z→x\)

Don't forget that the nodes in the KD tree are to record the current actual point. In order to balance, the point we choose is the median value of the points that exist in the undivided space of the current subtree (compared by the current dimension)

Common operations

①Insert: Like ordinary BST, but the comparison process becomes \(K\) dimensions that are constantly changing, and only one dimension is compared at a time

②Query: If it is to query the nearest point of \(P\) , start from the root, set \(ans\) to \(INF\) , compare the distance between the left and right subtrees, go to the small \(dfs\) first , After recursion, search for the larger side (provided that \(dis\) is better than the current \(ans\) ), that is to say, if there is no intersection with the circle made, the pruning will no longer recurse (the Manhattan distance is the smallest side of the rectangle). Distance). If \(K\) is the nearest neighbor, you can maintain a large root heap with \(size\) as \(K\) .

③Delete: does not exist

maintain

I haven't come across any question type yet. If you click too many points, it will easily make the tree crooked, and occasionally refactor it violently.

At present it seems to be a very simple \(DSA\)

Do the rest as you go

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293868&siteId=291194637