int main() { CompleteTree testTree = {NULL, 12}; /* 0 1 2 3 4 5 6 7 8 9 10 11 */ CompleteTree *tree = &testTree; for (unsigned int i = 0; i < 12; i++) { if (i < 6) assert(!isLeaf(tree, i)); else assert(isLeaf(tree, i)); } assert(getLeftChild(tree, 0) == 1); assert(getLeftChild(tree, 2) == 5); assert(getLeftChild(tree, 4) == 9); assert(getLeftChild(tree, 5) == 11); for (unsigned int i = 4; i < 7; i++) assert(getLeftSibling(tree, i) == (i - 1)); for (unsigned int i = 3; i < 6; i++) assert(getRightSibling(tree, i) == (i + 1)); for (unsigned int i = 1; i < 3; i++) assert(getParent(tree, i) == 0); for (unsigned int i = 3; i < 5; i++) assert(getParent(tree, i) == 1); for (unsigned int i = 5; i < 7; i++) assert(getParent(tree, i) == 2); for (unsigned int i = 7; i < 9; i++) assert(getParent(tree, i) == 3); for (unsigned int i = 9; i < 11; i++) assert(getParent(tree, i) == 4); assert(getParent(tree, 11) == 5); assert(!getDepth(tree, 0)); for (unsigned int i = 1; i < 3; i++) assert(getDepth(tree, i) == 1); for (unsigned int i = 3; i < 7; i++) assert(getDepth(tree, i) == 2); for (unsigned int i = 7; i < testTree.length; i++) assert(getDepth(tree, i) == 3); assert(getHeight(tree, 0) == 4); for (unsigned int i = 1; i < 3; i++) assert(getHeight(tree, i) == 3); for (unsigned int i = 3; i < 6; i++) assert(getHeight(tree, i) == 2); for (unsigned int i = 6; i < testTree.length; i++) assert(getHeight(tree, i) == 1); }
void UpperNode::self_collide() { if (isLeaf()) { s_trees[getID()]->self_collide(); return; } getLeftChild()->self_collide(); if (getRightChild()) { getRightChild()->self_collide(); getLeftChild()->collide(getRightChild()); } }
bool MaxHeap::isLeaf(size_t i) { if (getLeftChild(i) > m_nextIndex) return true; else return false; }
void UpperNode::visulization(int level) { if (isLeaf()) _box.visulization(); else if ((level > 0)) { if (level == 1) _box.visulization(); else if (getLeftChild()) getLeftChild()->visulization(level-1); if (getRightChild()) getRightChild()->visulization(level-1); } }
void UpperNode::Construct(unsigned int *lst, unsigned int lst_num) { if (lst_num == 1){ _child = lst[0]; _box = s_trees[getID()]->box(); return; } _box.empty(); //try to split them for (unsigned int t=0; t<lst_num; t++) { int i=lst[t]; _box += s_trees[i]->box(); } if (lst_num == 2) {// must split it! getLeftChild()->Construct(lst[0]); getRightChild()->Construct(lst[1]); return; } aap pln(_box); unsigned int left_idx=0, right_idx=lst_num-1; for (unsigned int t=0; t<lst_num; t++) { int i=lst[left_idx]; if (pln.inside(s_trees[i]->box().center())) left_idx++; else {// swap it unsigned int tmp = i; lst[left_idx] = lst[right_idx]; lst[right_idx--] = tmp; } } int hal = lst_num/2; if (left_idx == 0 || left_idx == lst_num) { getLeftChild()->Construct(lst, hal); getRightChild()->Construct(lst+hal, lst_num-hal); } else { getLeftChild()->Construct(lst, left_idx); getRightChild()->Construct(lst+left_idx, lst_num-left_idx); } }
void MaxHeap::balanceEntireTreeRec(size_t i) { if (isLeaf(i)) return; balanceEntireTreeRec(getLeftChild(i)); balanceEntireTreeRec(getRightChild(i)); balanceTreeRec(i); }
void UpperNode::refit() { if (isLeaf()) { _box = s_trees[getID()]->box(); } else { getLeftChild()->refit(); if (getRightChild()) getRightChild()->refit(); if (getRightChild()) _box = getLeftChild()->_box + getRightChild()->_box; else _box = getLeftChild()->_box; } }
void DeformBVHNode::Construct(unsigned int *lst, unsigned int lst_num) { _count = lst_num; for (unsigned int t=0; t<lst_num; t++) _box += s_mdl->_tri_boxes[lst[t]]; if (lst_num == 1) { _child = ID(lst[0]); _box = s_mdl->_tri_boxes[lst[0]]; } else { // try to split _child = this-s_current_node; s_current_node += 2; if (lst_num == 2) {// must split getLeftChild()->Construct(ID(lst[0])); getRightChild()->Construct(ID(lst[1])); } else { aap pln(_box); unsigned int left_idx = 0, right_idx = lst_num-1; for (unsigned int t=0; t<lst_num; t++) { int i=lst[left_idx]; if (pln.inside(s_mdl->_tri_centers[i])) left_idx++; else {// swap it unsigned int tmp = i; lst[left_idx] = lst[right_idx]; lst[right_idx--] = tmp; } } int half = lst_num/2; if (left_idx == 0 || left_idx == lst_num) { getLeftChild()->Construct(lst, half); getRightChild()->Construct(lst+half, lst_num-half); } else { getLeftChild()->Construct(lst, left_idx); getRightChild()->Construct(lst+left_idx, lst_num-left_idx); } } } }
void test2() { struct node rootStruct; struct node * root = &rootStruct; setNodeRed(root); struct node lStruct; struct node lStruct2; struct node lStruct3; root->left = &lStruct; root->right = &lStruct2; setNodeRed(root->left); getLeftChild(root)->left = &lStruct3; setNodeRed(root->right); assert(!is_red_black_tree(root)); }
bool SynopsisMD2::isLogicalLeaf(int lvl,int idx,int & left_child,bool assign) { if(isLeaf(lvl,idx) /*|| isLastLevel(lvl)*/) return false; if(assign) left_child = getLeftChild(lvl,idx); //otherwise use the already calculated value int right_child = left_child + 1; #ifdef SYNDEBUG assert(shape.size() > lvl+1); // we are not at the last level assert(shape[lvl+1].size() > right_child); // the vector of the children is ok #endif return (isLeaf(lvl+1,left_child) && isLeaf(lvl+1,right_child)); }
void heap (int arrayList[],int size,int currentLocation) { int temp; int parentIndex,leftChildIndex,rightChildIndex; parentIndex = getParent(currentLocation); leftChildIndex = getLeftChild(currentLocation); rightChildIndex = getRightChild(currentLocation); if(currentLocation<size) { if(arrayList[currentLocation]<arrayList[rightChildIndex]&&rightChildIndex<size) { temp=arrayList[currentLocation]; arrayList[currentLocation]=arrayList[rightChildIndex]; arrayList[rightChildIndex]=temp; } heap(arrayList,size,rightChildIndex); if(arrayList[currentLocation]<arrayList[leftChildIndex]&&leftChildIndex<size) { temp=arrayList[currentLocation]; arrayList[currentLocation]=arrayList[leftChildIndex]; arrayList[leftChildIndex]=temp; } heap(arrayList,size,leftChildIndex); if(currentLocation!=0) { if(arrayList[currentLocation]>arrayList[parentIndex]&&parentIndex<size) { temp=arrayList[currentLocation]; arrayList[currentLocation]=arrayList[parentIndex]; arrayList[parentIndex]=temp; } } } }
void MaxHeap::balanceTreeRec(size_t i) { size_t l = getLeftChild(i); size_t r = getRightChild(i); size_t greatest; if (l < m_nextIndex && (*m_array)[l].key > (*m_array)[i].key) greatest = l; else greatest = i; if (r < m_nextIndex && (*m_array)[r].key > (*m_array)[greatest].key) greatest = r; if (greatest != i) { swapNode(i, greatest); balanceTreeRec(greatest); } }
void UpperNode::collide(UpperNode *other) { s_mdl->_counts[0]._num_box_tests++; if (!_box.overlaps(other->_box)) return; if (isLeaf() && other->isLeaf()) { s_trees[getID()]->collide(s_trees[other->getID()]); return; } if (isLeaf()) { collide(other->getLeftChild()); collide(other->getRightChild()); } else { getLeftChild()->collide(other); getRightChild()->collide(other); } }
void DecisionTree::Node::printTree() const { // cerr << "["; if(isLeaf()) { // cerr << "LEAF: "<< m_probability<<" "; if(m_probability>0 && m_probability<1) cerr << m_probability << " "; } else { // cerr << m_tree->getFeatureName(m_feature_index); } if(!isLeaf()) { // cerr << "-> L: "; getLeftChild()->printTree(); // cerr << " "; } if(!isLeaf()) { // cerr << "-> R: "; getRightChild()->printTree(); // cerr << " "; } // cerr << "]"; }
void printTree(struct node * n) { if (n == NULL) return; printTree(getLeftChild(n)); printf("%d(%s) ", n->key, isNodeRed(n) ? "red" : "black"); printTree(n->right); }
bool SynopsisMD2::evict_victims(int lvl, int idx,int plvl, int pidx,bool * visited_prev,int target_size) { bool done; if(isLeaf(lvl,idx)) { if(lvl == plvl && idx == pidx) *visited_prev = true; return false; } //int left_child = levels->at(lvl).prefix + getLeftChild(lvl,levels->at(lvl).idx,idx); int left_child = getLeftChild(lvl,idx); /* levels->at(lvl).prefix = left_child; levels->at(lvl).idx = idx;*/ if(*visited_prev && isLogicalLeaf(lvl,idx,left_child,false)) /* if candidate for truncating */ { bool left,right; int leaf_idx; left = getLeaf(lvl+1,left_child,leaf_idx,true); right = getLeafValue(lvl+1, leaf_idx + 1); //getLeaf(lvl+1, left_child + 1,leaf_idx+1); return evict_lleaf(lvl,idx,left_child,leaf_idx,left,right,target_size); } /* else, truncate the descendants (bottom-up) and check again ^^ */ { if(!(*visited_prev) && lvl == plvl && idx == pidx) { *visited_prev = true; } /* look left */ { //cout<<"looking for victims at left child "<<lvl+1<<","<<left_child<<endl; done = evict_victims(lvl+1,left_child,plvl,pidx,visited_prev,target_size); if(done) return done; } /* look right */ int right_child = left_child + 1; { //cout<<"looking for victims at right child "<<lvl+1<<","<<right_child<<endl; done = evict_victims(lvl+1,right_child,plvl,pidx,visited_prev,target_size); if(done) return done; } } return false; }