Example #1
0
void* randomForestClassifyThread(void* index) {
    int number = *(int*)index;
    vector<int> result;
    set<int> attributes = getRandomAttributes();
    vector<int> samples = getRandomSampleIndexs();
    TreeNode* tree = buildDecisionTree(samples, attributes, samples);
    for (int j = 0; j < allTests.size(); j++) {
        result.push_back(findPath(tree, allTests[j]));
    }
    cout << number << endl;
    results[number] = result;
    tree->clear();
    delete tree;
}
Example #2
0
void randomForestClassify() {
    for (int i = 0; i < TREE_NUM; i++) {
        vector<int> result;
        set<int> attributes = getRandomAttributes();
        vector<int> samples = getRandomSampleIndexs();
        TreeNode* tree = buildDecisionTree(samples, attributes, samples);
        for (int j = 0; j < allTests.size(); j++) {
            result.push_back(findPath(tree, allTests[j]));
        }
        cout << "Run tree " << i << " finish" << endl;
        results[i] = result;
        tree->clear();
        delete tree;
    }
}
Example #3
0
 iterator insert(StoredObject&& toStore)
 {
     TreeNode* node = getNode(toStore.location);
     if (node->level() > 0)
     {
         // We store one element at time so there will be a moment before node overflow when its
         // count will be equal capacity. Then we'll relocate all its elements to the new child
         // nodes. At worst scenario, all elements will be relocated to the same node, so its
         // count() will be again equal to capacity. The loop ends when at least one element is
         // relocated to the another child node.
         while (node->count() == nodeCapacity && node->level() > 0)
         {
             typename TreeNode::iterator it;
             typename TreeNode::iterator itEnd = node->end();
             for (it = node->begin(); it != itEnd; ++it)
             {
                 node->child(it->location).insert(std::move(*it));
             }
             node->clear();
             node = &(node->child(toStore.location));
         }
     }
     return iterator(node, node->insert(std::move(toStore)));
 }
 vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
     
     vector<vector<int>> answer;
     
     vector<int> level;
     
     if(root==NULL) {
         return answer;
     }
     
     
     queue<TreeNode *> q;
     
     q.push(root);
     
     int num = 1;
     
     int row = 0;
     
     TreeNode *temp;
     
     while(!q.empty()){
         
         temp = q.front();
         
         q.pop();
        
         level.push_back(temp->val);
         
         num--;
         
             
         if(temp->left!=NULL){
             q.push(temp->left);
         }
         
         if(temp->right!=NULL){
             q.push(temp->right);
         }
             
        
         
         if(num==0){
             
             
             num = q.size();
             
             if(row%2==0){
                 
                 answer.push_back(level);
             }else{
                 
                 vector<int> temp;
                 for(int i = level.size() - 1 ; i >= 0; --i) {
                     temp.push_back(level[i]);
                 }
                 answer.push_back(temp);
                 temp.clear();
             }
             
             level.clear();
             
             row++;
             
         }
         
     }
     
     return answer;
 }