node* queue::pop(){ node* mynumber = data[0]; data[0] = data[used-1]; used--; int i = 0; node* temp; while ((2*i + 1) < used && (data[i]->freq > data[leftchild(i)]->freq || data[i]->freq > data[rightchild(i)]->freq)){ if (data[leftchild(i)]->freq <= data[rightchild(i)]->freq){ temp = data[i]; data[i] = data[leftchild(i)]; data[leftchild(i)] = temp; i = leftchild(i); } else if (data[leftchild(i)]->freq > data[rightchild(i)]->freq){ temp = data[i]; data[i] = data[rightchild(i)]; data[rightchild(i)] = temp; i = rightchild(i); } } return mynumber; }
void heap :: siftdown(int pos) { while (!isLeaf(pos)) { // Stop if pos is a leaf int j = leftchild(pos); int rc = rightchild(pos); if ((rc < n) && Comp::prior(Heap[rc], Heap[j])) j = rc; // Set j to greater child’s value if (Comp::prior(Heap[pos], Heap[j])) return; // Done swap(Heap, pos, j); pos = j; // Move down } }
node * node_heap::largerChild(node * n) { int index_n = n->get_index(); int index_l = leftchild(index_n); int index_r = rightchild(index_n); if (index_l == -1) return 0; if (index_r == -1) return heap[index_l]; node *l = heap[index_l]; node *r = heap[index_r]; node *w = ((*l) < (*r)) ? l : r; return w; }
void RandomForest::Node<T>::CreateTree() { tree->SetActureTreeNode(tree->GetActureTreeNode()+1); int bestFeatureIndex; T bestFeatureValue; if (depth == tree->GetDepthUpperBound()) { bestFeatureIndex = -1; bestFeatureValue=meanLeaf(); featureIndex=bestFeatureIndex; splitValue=bestFeatureValue; return; } chooseBestSplit(&bestFeatureIndex, &bestFeatureValue, &decreasedImpurity); featureIndex=bestFeatureIndex; splitValue=bestFeatureValue; if (bestFeatureIndex==-1) { return; } std::shared_ptr<std::vector<int> > indexList0(new std::vector<int>); std::shared_ptr<std::vector<int> > indexList1(new std::vector<int>); binSplitDataSet(sampleIndexList, bestFeatureIndex, bestFeatureValue, indexList0,indexList1); std::shared_ptr<Node<T> > leftchild(new Node<T>(GetTree())); leftchild->SetDepth(depth+1); if (leftchild->GetDepth() > tree->GetActureTreeDepth()) { tree->SetActureTreeDepth(leftchild->GetDepth()); } leftchild->SetSampleIndexList(indexList0); leftchild->CreateTree(); left=leftchild; std::shared_ptr<Node<T> > rightchild(new Node<T>(GetTree())); rightchild->SetDepth(depth+1); if (rightchild->GetDepth() > tree->GetActureTreeDepth()) { tree->SetActureTreeDepth(rightchild->GetDepth()); } rightchild->SetSampleIndexList(indexList1); rightchild->CreateTree(); right=rightchild; }
void percolate_down(heap *h,int pos) { int max,temp,left,right; left=leftchild(h,pos); right=rightchild(h,pos); if(left!=-1 && h->array[left]>h->array[pos]) max=left; else max=pos; if(right!=-1 && h->array[right]>h->array[max]) max=right; if(max!=pos) { temp=h->array[max]; h->array[max]=h->array[pos]; h->array[pos]=temp; } else return; percolate_down(h,max); }
int queue::pop(){ int mynumber = data[0]; data[0] = data[used-1]; used--; int i = 0; int temp; while ((2*i + 1) < used && (data[i] < data[leftchild(i)] || data[i] < data[rightchild(i)])){ if (data[leftchild(i)] > data[rightchild(i)]){ temp = data[i]; data[i] = data[leftchild(i)]; data[leftchild(i)] = temp; temp = order[i]; order[i] = order[leftchild(i)]; order[leftchild(i)] = temp; i = leftchild(i); } else if (data[leftchild(i)] < data[rightchild(i)]){ temp = data[i]; data[i] = data[rightchild(i)]; data[rightchild(i)] = temp; temp = order[i]; order[i] = order[rightchild(i)]; order[rightchild(i)] = temp; i = rightchild(i); } else if (data[leftchild(i)] == data[rightchild(i)]){ if (order[leftchild(i)] > order[rightchild(i)]){ temp = data[i]; data[i] = data[leftchild(i)]; data[leftchild(i)] = temp; temp = order[i]; order[i] = order[leftchild(i)]; order[leftchild(i)] = temp; i = leftchild(i); } else { temp = data[i]; data[i] = data[rightchild(i)]; data[rightchild(i)] = temp; temp = order[i]; order[i] = order[rightchild(i)]; order[rightchild(i)] = temp; i = rightchild(i); } } } return mynumber; }