void DeapHeap::removeMinKey() { Node *minRoot=getMin(); if (minRoot) if (minRoot->isLeaf()) { root->left=NULL; delete minRoot; } else { Node *last=lastNode; swap(minRoot, last); //korekta wskaznika na ostatni element if (lastNode->parent->right==lastNode) { lastNode=lastNode->parent->getLeft(); } else { bool lastOne=false; Node *beg = lastNode; while (beg->parent->right!=beg) { beg=beg->parent; if (beg==root) { lastOne=true; break; } } if (!lastOne) beg=beg->parent->getLeft(); else beg=(Node*)root; while (!beg->isLeaf()) beg=beg->getRight(); lastNode = beg; } if (last->parent->left==last) last->parent->left=NULL; else last->parent->right=NULL; delete last; Node *beg=minRoot; while (!beg->isLeaf() ) { Node* tmp=beg; bool A=beg->getLeft() && beg->getLeft()->key <= beg->key; bool B=beg->getRight() && beg->getRight()->key <= beg->key; if (A && B) { beg=(beg->getLeft()->key < beg->getRight()->key)?beg->getLeft():beg->getRight(); swap(tmp, beg); } else if (A) { beg=beg->getLeft(); swap(tmp, beg); } else if (B) { beg=beg->getRight(); swap(tmp, beg); } else break; } SIDE side; Node* cousin = searchCousin(beg, side); if (cousin && beg->key > cousin->key) swap(beg, cousin); } }
string Tree::procurafinal(string codificacao){ unsigned char buffer; string aux_s; aux_s.clear(); ofstream write; const char* aux_ch = filename.c_str(); write.open(aux_ch, ios::out | ios::binary | ios::app); if(write.is_open()){ QStack<Node*>* StackdeCodigo = new QStack<Node*>(); StackdeCodigo->push(root); for(int i = 0; i < codificacao.length();i++){ Node* temp = new Node(); if(codificacao[i] == '0'){ temp = StackdeCodigo->top()->getLeftChild(); if(temp->isLeaf() == false){ StackdeCodigo->push(temp); } else{ buffer = temp->getContent(); StackdeCodigo->clear(); StackdeCodigo->push(root); write << buffer; } } else if(codificacao[i] == '1'){ temp = StackdeCodigo->top()->getRightChild(); if(temp->isLeaf() == false){ StackdeCodigo->push(temp); } else{ buffer = temp->getContent(); StackdeCodigo->clear(); StackdeCodigo->push(root); write << buffer; } } } } else { cout << "TRASH!" << endl; } write.close(); return aux_s; }
/** * @brief helper methodfor remove() * @details recursively search for the parent of the removal target.\n * Once found, remove it accordingly in 1 of 4 scenarios. * * @param node the node potential to be the removal target's parent * @param rmToken the removal target string * * @return removal success status */ bool TBST::remove(Node* node, string rmToken) { string nodeToken = node->getData()->getToken(); bool targetIsOnLeft = (rmToken < nodeToken); Node* rmTarget; // looking for the removal node if (targetIsOnLeft) { if (node->hasLeftChild()) { rmTarget = node->getLeft(); } else { return false; } } else { if (node->hasRightChild()) { rmTarget = node->getRight(); } else { return false; } } // if found, then remove with 1 of 4 cases if (rmToken == rmTarget->getData()->getToken()) { if (rmTarget->isLeaf()) { return rmLeaf(node, targetIsOnLeft); } else if (rmTarget->hasLeftChild() && !rmTarget->hasRightChild()) { return rmNodeWithLeft(node, targetIsOnLeft); } else if (rmTarget->hasRightChild() && !rmTarget->hasLeftChild()) { return rmNodeWithRight(node, targetIsOnLeft); } else { return rmNodeWithBoth(node, targetIsOnLeft); } // otherwise, recurse } else { return remove(rmTarget, rmToken); } }
/* this function is useful for search and modify, and range-query */ Node* //return the first key's position that >= *_key Tree::find(const Bstr* _key, int* _store, bool ifmodify) const { //to assign value for this->bstr, function shouldn't be const! if(this->root == NULL) return NULL; //Tree Is Empty Node* p = root; int i, j; Bstr bstr = *_key; //local Bstr: multiple delete while(!p->isLeaf()) { if(ifmodify) p->setDirty(); j = p->getNum(); for(i = 0; i < j; ++i) //BETTER(Binary-Search) if(bstr < *(p->getKey(i))) break; p = p->getChild(i); this->prepare(p); } j = p->getNum(); for(i = 0; i < j; ++i) if(bstr <= *(p->getKey(i))) break; if(i == j) *_store = -1; //Not Found else *_store = i; bstr.clear(); return p; }
void DrawTreeCairo::DrawTimeEdges() { cairo_set_line_width (cr, linewidth); const double midnode = leafWidth; cairo_set_font_size (cr, fontsize); cairo_set_source_rgba (cr,parameters->allFontColor.red,parameters->allFontColor.green,parameters->allFontColor.blue, 1); cairo_move_to(cr, 0, pageheight); cairo_line_to(cr, pagewidth, pageheight); cairo_line_to(cr, pagewidth, 0); cairo_set_line_width(cr, 1); cairo_set_dash(cr, dashed1, len1, 0); for(unsigned u = 0; u < species->getNumberOfNodes(); u++) { Node* n = species->getNode(u); if (!n->isLeaf()) { cairo_move_to(cr, n->getX(), pageheight); cairo_line_to(cr, n->getX(), n->getY() + midnode); } } cairo_stroke(cr); cairo_set_dash(cr, dashed3, 0, 0); }
// Returns true if rect fully overlaps n bool buildQuery(const Node& n, const Rect rect, const int32_t count, Point* out_points, bool x) { if (n.isLeaf()) { return true; } else { bool leftFullyOverlaps = false, rightFullyOverlaps = false; if (x) { if (rect.lx < n.split) leftFullyOverlaps = buildQuery(*n.left, rect, count, out_points, false); if (rect.hx >= n.split) rightFullyOverlaps = buildQuery(*n.right, rect, count, out_points, false); } else { if (rect.ly < n.split) leftFullyOverlaps = buildQuery(*n.left, rect, count, out_points, true); if (rect.hy >= n.split) rightFullyOverlaps = buildQuery(*n.right, rect, count, out_points, true); } if (leftFullyOverlaps && rightFullyOverlaps) { if (n.points.empty()) { s_nodes.emplace_back(coverage(n.left->rect, rect), n.left.get()); s_nodes.emplace_back(coverage(n.right->rect, rect), n.right.get()); return false; } return true; } else if (leftFullyOverlaps) { s_nodes.emplace_back(coverage(n.left->rect, rect), n.left.get()); } else if (rightFullyOverlaps) { s_nodes.emplace_back(coverage(n.right->rect, rect), n.right.get()); } return false; } }
void Node::selectAction() { srand(time(0)); vector<Node*> visited; Node *cur = this; visited.push_back(this); backupTable->restorePosition(table); bool whiteToMove = table->data.whiteToMove; while (!cur->isLeaf()) { cur = cur->select(visited.size()); table->moveCheck(cur->currMove, true); visited.push_back(cur); } Node *newNode; int value = MAX_LONG * table->isEnd(); if (value == 0) { cur->expand(); newNode = cur->select(visited.size()); visited.push_back(newNode); value = evaluate(newNode); if (! whiteToMove) { value = -value; } } else { if (! whiteToMove) { value = -value; } newNode = cur; } for (Node *node : visited) { node->updateStats(value); } }
void DrawTreeCairo::TimeLabelsOnEdges() { cairo_set_source_rgba (cr, 0, 0, 0, 1); for(unsigned u = 0; u < species->getNumberOfNodes(); u++) { Node* n = species->getNode(u); const string timelabel = double2charp(n->getTime()); double xpos = n->getX(); double ypos = n->getY(); if (!n->isLeaf()) { cairo_text_extents (cr, timelabel.c_str(), &extents); xpos = xpos - (extents.width + extents.x_advance); ypos = ypos - leafWidth; cairo_move_to(cr,xpos,ypos); cairo_save(cr); if(parameters->horiz) { cairo_rotate(cr,-(pi/4)); } cairo_show_text(cr,timelabel.c_str()); cairo_restore(cr); } } }
Node* DeapHeap::searchCousin(Node* node, SIDE& side) { Node *tmp = node; stack<DIRECTION> wayPoints; while (tmp->parent!=root) { if (tmp->parent->left == tmp) wayPoints.push(L); else wayPoints.push(R); tmp=tmp->parent; } if (tmp==getMin()) { tmp=getMax(); side=MIN; } else { tmp=getMin(); side=MAX; } while (!wayPoints.empty() && !tmp->isLeaf()) { if (wayPoints.top() == L) tmp=tmp->getLeft(); else tmp=tmp->getRight(); wayPoints.pop(); } return(tmp); }
// Sets the divergence time of node v void Tree::setTime(const Node& v, double time) const { (*times)[v] = time; assert(v.isLeaf() || (*times)[v] >= (*times)[v.getLeftChild()]); assert(v.isLeaf() || (*times)[v] >= (*times)[v.getRightChild()]); assert(v.isRoot() || (*times)[v.getParent()] >= (*times)[v]); }
inline iterator begin() { Node* start = this; while (!start->isLeaf()) { start = start->m_children[0]; } assert(start == this || start->m_next != NULL); return iterator(start); }
void update(const Node &no, int old_v, int new_v){ if (!overlap(no)) return ; //printf("update(%d: %d %d, %d %d\n", no.o, no.L, no.R, old_v, new_v); Treap::remove(treap[no.o], old_v); //printf("update(%d: %d %d, %d %d\n", no.o, no.L, no.R, old_v, new_v); Treap::insert(treap[no.o], new_v); if (no.isLeaf()) return; update(no.lch(), old_v, new_v); update(no.rch(), old_v, new_v); }
void init(const Node& no){ memset(cnt[no.o], 0, sizeof(cnt[no.o])); cover[no.o] = 0; cnt[no.o][0] = no.value(); if (no.isLeaf()){ return; }else{ init(no.lch()); init(no.rch()); } }
// Check that all nodes has sane identity numbers // This is used when reading user-defined trees bool Tree::IDnumbersAreSane(Node& n) const { bool ret = n.getNumber() < getNumberOfNodes(); if(n.isLeaf() == false) { ret = ret && IDnumbersAreSane(*n.getLeftChild()) && IDnumbersAreSane(*n.getRightChild()); } return ret; }
void DrawTreeCairo::DrawSpeciesEdgesWithContour() { Color& cfill = config->species_edge_color; Color& cline = config->species_edge_contour_color; cairo_set_line_width(cr, s_contour_width); cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND); cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND); const double midnode = leafWidth; Node *root = species->getRootNode(); cairo_move_to(cr, 0, root->getY()+midnode); cairo_rel_line_to(cr, root->getX(), 0); cairo_rel_line_to(cr,0, -(midnode * 2) ); cairo_rel_line_to(cr, -(root->getX()), 0); cairo_close_path(cr); cairo_set_source_rgba(cr, cline.red, cline.green, cline.blue, 1); cairo_stroke_preserve(cr); cairo_set_source_rgba(cr, cfill.red, cfill.green, cfill.blue, 1); cairo_fill(cr); for ( Node *n = species->preorder_begin(); n != 0; n = species->preorder_next(n) ) { const double x = n->getX(); const double y = n->getY(); if (!n->isLeaf()) { double pmidx; double pmidy; intersection(x, y - midnode, n->getLeftChild()->getX(), n->getLeftChild()->getY()-midnode, x, y + midnode, n->getRightChild()->getX(), n->getRightChild()->getY()+midnode, pmidx, pmidy); cairo_move_to(cr, x, y + midnode); cairo_rel_line_to(cr,n->getLeftChild()->getX()-x,n->getLeftChild()->getY()-y); cairo_rel_line_to(cr, 0, -(midnode * 2) ); cairo_line_to(cr, pmidx, pmidy); cairo_line_to(cr,n->getRightChild()->getX(),n->getRightChild()->getY()+midnode); cairo_rel_line_to(cr, 0, - (midnode * 2) ); cairo_line_to(cr, x, y - midnode); cairo_close_path(cr); cairo_set_source_rgba(cr, cline.red, cline.green, cline.blue, 1); cairo_stroke_preserve(cr); cairo_set_source_rgba(cr, cfill.red, cfill.green, cfill.blue, 1); cairo_fill(cr); } } }
int prefixTrieMatching(string text, trie &t) { char currentSymbol = text[0]; Node currentNode = t[0]; unsigned int letterId = 0; int currentSymToInd = letterToIndex(currentSymbol); while(true) { if(currentNode.isLeaf()) { return 1; } else if(currentNode.next[currentSymToInd] != NA) { currentNode = t[currentNode.next[currentSymToInd]]; if (letterId < text.size() - 1) { currentSymbol = text[++letterId]; currentSymToInd = letterToIndex(currentSymbol); } else { if(currentNode.isLeaf()) { return 1; } else { return 0; } } } else { return 0; } } }
size_t TreeTemplateTools::getNumberOfLeaves(const Node& node) { size_t nbLeaves = 0; if (node.isLeaf()) { nbLeaves++; } for (int i = 0; i < static_cast<int>(node.getNumberOfSons()); i++) { nbLeaves += getNumberOfLeaves(*node[i]); } return nbLeaves; }
// Accessing leaves from a name Node* Tree::findLeaf(const string& name) const { Node* ret = findNode(name); if(ret->isLeaf()) { return ret; } else { throw AnError("Found interior node when looking for a leaf name ", name, 1); } }
void Tree::PortalBuilder::build( Vec2f const& max , Vec2f const& min ) { int numNodes = mTree->mNodes.size(); for( int i = 0 ; i < numNodes ; ++i ) { Node* node = mTree->mNodes[i]; if ( node->isLeaf() ) continue; SuperPlane* splane = createSuperPlane( node , max , min ); generatePortal_R( node , splane ); } }
void init(const Node& no){ treap[no.o] = Treap::nil; if (no.isLeaf()){ Treap::insert(treap[no.o], a[no.L]); //printf("\n"); }else{ init(no.lch()); init(no.rch()); for (int i = no.L; i <= no.R; i++){ Treap::insert(treap[no.o], a[i]); } //printf("\n"); } }
void recursiveSplit(Node &n, int depth) { const bool not_a_leaf{ false }; const bool is_a_leaf { true }; if (n.numVoxels() <= svt::minVoxels) { n.isLeaf(is_a_leaf); std::cout << depth << ": Returning... numVoxels.\n"; return; } if (n.percentEmpty() <= svt::minEmptyPercent){ n.isLeaf(is_a_leaf); std::cout << depth << ": Returning... minEmptyPercent.\n"; return; } Plane p{ findPlane(n.shortestAxis(), n.bv()) }; Node left{ n.leftChild(), not_a_leaf, num(n.min(), p.max()), n.min(), p.max() - n.min() }; recursiveSplit(left, depth + 1); Node right{ n.rightChild(), not_a_leaf, num(p.min(), n.max()), p.min(), n.max() - p.min() }; recursiveSplit(right, depth + 1); svt::tree[left.index()] = left; svt::tree[right.index()] = right; }
int alphaBetaPrune(Node& n, bool isMax, int& myMoveCol/*, int alpha, int beta*/) { if (n.isLeaf()) { myMoveCol = n.newMoveCol; return n.heuristicValue(); } if (isMax) { int max_val = INT_MIN; for (const auto child : n.children) { int col; int val = alphaBetaPrune(*child, false, col/*, alpha, beta*/); // alpha = max(val, alpha); if (max_val <= val) { max_val = val; myMoveCol = child->newMoveCol; } // if(beta <= alpha) { // break; // } } return max_val; } else { int min_val = INT_MAX; for (const auto& child : n.children) { int col; int val = alphaBetaPrune(*child, true, col/*, alpha, beta*/); // beta = min(beta, val); if (min_val >= val) { min_val = val; myMoveCol = child->newMoveCol; } // if(beta <= alpha) { // break; // } } return min_val; } }
Group ClusterTools::getGroups(const Node& subtree, vector<Group>& groups) { Group group; if (subtree.isLeaf()) { group.add(subtree.getName()); } else { size_t subcount = 0; vector<size_t> thisgroup; for (size_t i = 0; i < subtree.getNumberOfSons(); i++) { const Node * son = subtree.getSon(i); // Get group for each son node: Group sonGroup = getGroups(*son, groups); vector<size_t> subgroup(sonGroup.size()); size_t index = subcount; for (size_t j = 0; j < sonGroup.size(); j++) { group.add(sonGroup[j]); subgroup[j] = subcount; thisgroup.push_back(subcount); subcount++; } if(subgroup.size() > 1) { //group.addSubgroup(subgroup, sonGroup.getHeight()); // Recursively add subgroups: for(size_t j = 0; j < sonGroup.getSubgroups().size(); j++) { group.addSubgroup(sonGroup.getSubgroup(j) + index, sonGroup.getSubgroupHeight(j)); } } group.setHeight(sonGroup.getHeight() + son->getDistanceToFather()); } vector<string> propNames = subtree.getNodePropertyNames(); for(size_t i = 0; i < propNames.size(); i++) { group.setProperty(propNames[i], subtree.getNodeProperty(propNames[i])); } group.addSubgroup(thisgroup, group.getHeight()); // Add this group: groups.push_back(group); } return group; }
void DrawTreeCairo::DrawSpeciesNodeLabels() { cairo_select_font_face (cr, parameters->species_font.c_str(), CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT_BOLD); cairo_set_source_rgba (cr, parameters->speciesFontColor.red, parameters->speciesFontColor.green, parameters->speciesFontColor.blue, 1); cairo_set_font_size (cr, speciesfontsize); for(unsigned u = 0; u < species->getNumberOfNodes(); u++) { Node* n = species->getNode(u); ostringstream st; if(parameters->do_not_draw_species_tree == false) { if (parameters->ids_on_inner_nodes) { st << n->getNumber(); } st << " " + n->getName(); } const string ns = st.str(); const double xpos = n->getX(); const double ypos = n->getY(); cairo_text_extents (cr, ns.c_str(), &extents); if(n->isLeaf()) { cairo_move_to(cr, xpos + (extents.height / 2) , ypos + leafWidth); } else { cairo_move_to(cr, xpos - (0.25 * extents.width) , ypos + leafWidth); } cairo_save(cr); if(parameters->horiz) { cairo_rotate(cr,-(pi/4)); } cairo_show_text(cr,ns.c_str()); cairo_restore(cr); } cairo_stroke(cr); }
void Tree::getTerminalNodes( Node* pNode, vector<Node*>& res ) const { for( int i = 0; i < Node::getAlphabetSize(); ++i ) { Node* next = pNode->getNode( i ); if( next != NULL ) { if( next->isLeaf() ) { res.push_back( next ); } else { getTerminalNodes( next, res ); } } } }
void DrawTreeCairo::DrawSpeciesEdges() { cairo_set_source_rgba(cr,config->species_edge_color.red,config->species_edge_color.green,config->species_edge_color.blue,1); cairo_set_line_width(cr, 1); const double midnode = leafWidth; Node *root = species->getRootNode(); cairo_move_to(cr, 0, (root->getY() + midnode) ); cairo_rel_line_to(cr,root->getX(),0); cairo_rel_line_to(cr, 0, -(midnode * 2) ); cairo_rel_line_to(cr, -(root->getX()) , 0); cairo_close_path(cr); cairo_stroke_preserve(cr); cairo_fill(cr); for ( Node *n = species->preorder_begin(); n != 0; n = species->preorder_next(n) ) { const double x = n->getX(); const double y = n->getY(); cairo_set_source_rgba(cr,config->species_edge_color.red,config->species_edge_color.green,config->species_edge_color.blue,1); if (!n->isLeaf()) { cairo_move_to(cr,x,y + midnode); cairo_rel_line_to(cr,n->getLeftChild()->getX()-x,n->getLeftChild()->getY()-y); cairo_rel_line_to(cr,0,-midnode*2); cairo_rel_line_to(cr,x-n->getLeftChild()->getX(),(y) - (n->getLeftChild()->getY())); cairo_close_path(cr); cairo_stroke_preserve(cr); cairo_fill(cr); cairo_move_to(cr,x,y - midnode); cairo_rel_line_to(cr,n->getRightChild()->getX()-x,n->getRightChild()->getY()-y); cairo_rel_line_to(cr,0,midnode*2); cairo_rel_line_to(cr,x-n->getRightChild()->getX(),y - n->getRightChild()->getY()); cairo_close_path(cr); cairo_stroke_preserve(cr); cairo_fill(cr); } cairo_stroke(cr); } }
void RHomogeneousClockTreeLikelihood::computeBranchLengthsFromHeights(Node* node, double height) throw (Exception) { for (unsigned int i = 0; i < node->getNumberOfSons(); i++) { Node* son = node->getSon(i); if (son->isLeaf()) { son->setDistanceToFather(std::max(minimumBrLen_, height)); } else { Parameter* p = &brLenParameters_.getParameter(string("HeightP") + TextTools::toString(son->getId())); double sonHeightP = p->getValue(); double sonHeight = sonHeightP * height; son->setDistanceToFather(std::max(minimumBrLen_, height - sonHeight)); computeBranchLengthsFromHeights(son, sonHeight); } } }
void BTree<K, Comp>::exportAsDot(std::ostream& out) { out << "digraph bTree {\n"; std::deque<uint64_t> pidQueue; pidQueue.push_back(rootPID); while(!pidQueue.empty()) { uint64_t pid = pidQueue.back(); pidQueue.pop_back(); //fix page BufferFrame& bf = bufferManager.fixPage(pid, false); Node<K, Comp> *currNode = reinterpret_cast<Node<K, Comp>*>(bf.getData()); if(currNode->isLeaf()) { out << "node" << pid << " [shape=record, label=\"<count> " << (currNode->count) << " | "; for(uint64_t i = 0; i < currNode->count; i++) { out << "{ <key" << i << "> " << currNode->keyValuePairs[i].first << " | " << "<tid" << i << "> " << currNode->keyValuePairs[i].second << " } | "; } out << "next\"];" << std::endl; if(currNode->next != std::numeric_limits<uint64_t>::max()) { out << "node" << pid << ":next -> node" << currNode->next << ";" <<std::endl; } } else { out << "node" << pid << " [shape=record, label=\"<count> " << (currNode->count) << " | "; for(uint64_t i = 0; i < currNode->count; i++) { out << "<ptr" << i << "> * | " << "<key" << i << "> " << currNode->keyValuePairs[i].first << " | "; } out << "<ptr" << currNode->count << "> *\"];" << std::endl; for(uint64_t i = 0; i < currNode->count; i++) { out << "node" << pid << ":ptr" << i << " -> node" << currNode->keyValuePairs[i].second << ";" <<std::endl; pidQueue.push_back(currNode->keyValuePairs[i].second); } out << "node" << pid << ":ptr" << currNode->count << " -> node" << currNode->next << ";" <<std::endl; pidQueue.push_back(currNode->next); } //unfix page bufferManager.unfixPage(bf, false); } out << "}"; }
QByteArray HandleFile::rebuildFile(QByteArray codeFile, int sizeTrash, Node *root) { Node *curr = root; ByteArray code; code.toByteArray(codeFile); QByteArray codeFileOut; long long int sizeCode = codeFile.size(); for(long long int i = 0; i < sizeCode*8 - sizeTrash; ++i) { if(code.getBit(i)) { curr = curr->getRightChild(); } else { curr = curr->getLeftChild(); } if(curr->isLeaf()) { codeFileOut += curr->getContent(); curr = root; } } return codeFileOut; }
vector<string> exODT_model::NNIs(int e) { vector<string> NNIs; int left_e,right_e,f; Node * root = id_nodes[e]; if (root->isLeaf()) return NNIs; vector <Node *> roots_sons=root->getSons(); right_e=node_ids[roots_sons[0]]; left_e=node_ids[roots_sons[1]]; if (roots_sons[0]->isLeaf()) ; else { vector <Node *> right_sons=roots_sons[0]->getSons(); f=node_ids[right_sons[0]]; NNIs.push_back(feSPR(left_e,f)); f=node_ids[right_sons[1]]; NNIs.push_back(feSPR(left_e,f)); } if (roots_sons[1]->isLeaf()) ; else { vector <Node *> left_sons=roots_sons[1]->getSons(); f=node_ids[left_sons[0]]; NNIs.push_back(feSPR(right_e,f)); f=node_ids[left_sons[1]]; NNIs.push_back(feSPR(right_e,f)); } return NNIs; }