Beginner KD Tree

Source: Internet
Author: User

In the beginning will not KD tree, feel kd tree must be very God, learned kd tree found KD tree is very good to write.

Each node of the KD tree has a split hyper-plane, and I am dividing the current dimension with a depth% dimension, compared to the current node.

Attach template code, find plane K close-up (kd tree bare title)

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <queue>6#include <cmath>7 using namespacestd;8 #defineMAXN 1000209 #defineINF 0X3F3F3F3F3F3F3FLLTen  One intNow ; AtypedefLong Longll; - structpoint{ -     intx[2]; the     BOOL operator< (point a)Const{ -         returnX[now] <A.x[now]; -     } - }A[MAXN]; +Priority_queue <ll>Heap; - intN,k,id; + ll Curd; A  at voidBuildintLintRintDEP) { -     if(L > R)return; -now = dep%2; -     intMid = (L + r) >>1; -Nth_element (A + l,a + mid,a + R +1); -Build (L,mid-1, DEP +1); inBuild (Mid +1, R,DEP +1); - } toinline LL Sqr (intx) {return(LL) x *x;} +inline ll Dis (intX1,intX2,intY1,inty2) { -     returnSqr (X1-X2) + sqr (y1-y2); the } * voidQuery (point P,intLintRintDEP) { $     if(L > R)return;Panax Notoginsengnow = dep%2; -     intMid = (L + r) >>1; thell tmp = DIS (p.x[0],a[mid].x[0],p.x[1],a[mid].x[1]); +     if(Mid >ID) { A         if(Heap.size () <k) Heap.push (TMP); the         Else if(Heap.top () >tmp) {Heap.pop (); Heap.push (TMP);} +     } -     if(L = = r)return; $     if(P.x[now] >A[mid].x[now]) { $Query (P,mid +1, R,DEP +1); -now = dep%2; -         if(Heap.size () < K | | heap.top () >= Sqr (P.x[now]-a[mid].x[now]) query (P,l,mid-1, DEP +1); the     } -     Else{WuyiQuery (P,l,mid-1, DEP +1); thenow = dep%2; -         if(Heap.size () < K | | heap.top () >= Sqr (P.x[now]-a[mid].x[now]) query (P,mid +1, R,DEP +1); Wu     } - } About intMain () { $Freopen ("star.in","R", stdin); -Freopen ("Star.out","W", stdout); -scanf"%d%d",&n,&k); -      for(inti =1; I <= N; i++){ Ascanf"%d%d", &a[i].x[0],&a[i].x[1]); +     } theBuild1N1); -      for(inti =1; I <= N; i++){ $ID =i; theQuery (A[i],1N1); the     } theprintf"%lld\n", Heap.top ()); the     return 0; -}

After learning more about the application of KD tree, the type of problem can be applied in KD tree model.

Beginner KD Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.