Skip to content

Instantly share code, notes, and snippets.

@buyoh
Created April 20, 2016 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buyoh/47a8ff21330ec5de86fff77f51830113 to your computer and use it in GitHub Desktop.
Save buyoh/47a8ff21330ec5de86fff77f51830113 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <queue>
#include <list>
#include <map>
using namespace std;
//template<typename T>
class CompareTree{
private:
double border;
int dim;
int leaf=-1;
template<typename T>
bool compare(T e){
return border <= e[dim];
}
public:
CompareTree *left = nullptr;
CompareTree *right = nullptr;
CompareTree(int d):leaf(d){}
CompareTree(int d,double b):border(b),dim(d){
}
~CompareTree(){
delete left;
delete right;
}
template<typename T>
int classify(T e){
if (leaf<0){
if (compare(e)){
return right->classify(e);
}else{
return left->classify(e);
}
}else{
return leaf;
}
}
};
int main(){
int i,j,k,l;
int c=0;
CompareTree *pct;
CompareTree rct(0,2.0);
rct.left = pct = new CompareTree(1,3.0);
rct.right = new CompareTree(1);
pct->left = new CompareTree(0);
pct->right = new CompareTree(1);
// u[0] <= 2.0 ? class1 : u[1] <= 3.0 ? class0 : class1
vector<vector<double>> test=vector<vector<double>>{
vector<double>{0.0,0.0},
vector<double>{5.0,0.0},
vector<double>{0.0,5.0},
vector<double>{5.0,5.0}};
for (vector<double> &v:test)
cout << rct.classify(v) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment