int main() { int n; check(scanf("%d", &n) == 1, "can't read value"); struct Node *nodesP = allocateNodes(n); for(size_t i = 0; i < n; i++) { check(scanf("%d", &nodesP[i].value) == 1, "can't read value"); nodesP[i].key = i; } stackT *stackP = allocateStack(n); for(size_t i = n; i > 0; i--) { while(!stackIsEmpty(stackP) && stackTop(stackP).value < nodesP[i-1].value) { stackPop(stackP); } if(stackIsEmpty(stackP)) { nodesP[i-1].nearMax = n; } else { nodesP[i-1].nearMax = stackTop(stackP).key; } stackPush(stackP, nodesP[i-1]); } freeStack(stackP); traverseTree(nodesP, 0, n-1, TRAVERSE_POSTORDER); printf("\n"); traverseTree(nodesP, 0, n-1, TRAVERSE_INORDER); printf("\n"); free(nodesP); return 0; }
//遍历树,左路径添0,右路径添1 void traverseTree(htNode *treeNode, hlTable **table, int k, char code[256]) { if(treeNode->left == NULL && treeNode->right == NULL) { code[k] = '\0'; hlNode *aux = (hlNode*)malloc(sizeof(hlNode)); aux->code = (char*)malloc( sizeof(char) * (strlen(code)+1) ); strcpy(aux->code, code); aux->symbol = treeNode->symbol; aux->next = NULL; if( (*table)->first == NULL ) { (*table)->first = aux; (*table)->last = aux; } else { (*table)->last->next = aux; (*table)->last = aux; } } if(treeNode->left != NULL) { code[k] = '0'; traverseTree(treeNode->left, table, k+1, code); } if(treeNode->right != NULL) { code[k] = '1'; traverseTree(treeNode->right, table, k+1, code); } }
int traverseTree(node_4_h* n1, node_4_h* n2) { if(n1 == NULL) return 0; if(n1->value == n2->value && treeMatch(n1, n2)) return 1; return(traverseTree(n1->left, n2) || traverseTree(n1->right, n2)); }
void traverseTree(tnode h, void (*visit)(tnode)) { if (h->l != NULL) traverseTree(h->l, visit); // In-order Traversal (*visit)(h); if (h->r != NULL) traverseTree(h->r, visit); }
void traverseTree(ExprTree *tree, MathExpr **expr) // Converts binary tree to linked list (postfix format) { MathExpr *node; if ( tree == NULL) return; traverseTree(tree->left, expr); traverseTree(tree->right, expr); node = (MathExpr *) malloc(sizeof(MathExpr)); node->fvalue = tree->fvalue; node->opcode = tree->opcode; node->ivar = tree->ivar; node->next = NULL; node->prev = (*expr); if (*expr) (*expr)->next = node; (*expr) = node; }
int main(void) { tnode head = createTree(1, N); traverseTree(head, visit); return 0; }
MathExpr * mathexpr_create(char *formula, int (*getVar) (char *)) { ExprTree *tree; MathExpr *expr = NULL; MathExpr *result = NULL; getVariableIndex = getVar; Err = 0; PrevLex = 0; CurLex = 0; S = formula; Len = strlen(S); Pos = 0; Bc = 0; tree = getTree(); if (Bc == 0 && Err == 0) { traverseTree(tree, &expr); while (expr) { result = expr; expr = expr->prev; } } deleteTree(tree); return result; }
int main() { struct node *tree = makeTree(); traverseTree(tree, 0); decodeMessage(tree); return 0; }
TNode *TOutlineViewer::iterate( Boolean(*action)(TOutlineViewer *, TNode *, int, int, long, ushort), Boolean checkResult) { int position = -1; return traverseTree(this, action, position, checkResult, getRoot(), 0, 0, True); }
bool traverseTree(Node* rootNode, WillBeHeapVector<RawPtrWillBeMember<Node>, initialNodeVectorSize>* partiallyDependentNodes) { // To make each minor GC time bounded, we might need to give up // traversing at some point for a large DOM tree. That being said, // I could not observe the need even in pathological test cases. for (Node& node : NodeTraversal::startsAt(rootNode)) { if (node.containsWrapper()) { if (!node.isV8CollectableDuringMinorGC()) { // This node is not in the new space of V8. This indicates that // the minor GC cannot anyway judge reachability of this DOM tree. // Thus we give up traversing the DOM tree. return false; } node.clearV8CollectableDuringMinorGC(); partiallyDependentNodes->append(&node); } if (ShadowRoot* shadowRoot = node.youngestShadowRoot()) { if (!traverseTree(shadowRoot, partiallyDependentNodes)) return false; } else if (node.isShadowRoot()) { if (ShadowRoot* shadowRoot = toShadowRoot(node).olderShadowRoot()) { if (!traverseTree(shadowRoot, partiallyDependentNodes)) return false; } } // <template> has a |content| property holding a DOM fragment which we must traverse, // just like we do for the shadow trees above. if (isHTMLTemplateElement(node)) { if (!traverseTree(toHTMLTemplateElement(node).content(), partiallyDependentNodes)) return false; } // Document maintains the list of imported documents through HTMLImportsController. if (node.isDocumentNode()) { Document& document = toDocument(node); HTMLImportsController* controller = document.importsController(); if (controller && document == controller->master()) { for (unsigned i = 0; i < controller->loaderCount(); ++i) { if (!traverseTree(controller->loaderDocumentAt(i), partiallyDependentNodes)) return false; } } } } return true; }
void traverseTree( Node* node ,long index) { if ( !node ) return; if ( !node -> bottomleft && !node -> bottomright ) { buffer[index] = '\0'; printf ("'%c'",node->data.dataletter.letter); characters[node -> data.dataletter.letter].bitstring = (unsigned char*)malloc( strlen(buffer)+1 ); strcpy( characters[node -> data.dataletter.letter].bitstring , buffer ); return; } if ( index == currsize-1 ) buffer=realloc(buffer,currsize+BUFSIZ); buffer[index] = '0'; traverseTree( node -> bottomleft ,index+1); buffer[index] = '1'; traverseTree( node -> bottomright, index+1); if ( node -> data.datatype.type == LETTER ) printf("'%c'", node -> data.dataletter.letter); else printf("%lu",node -> data.datanum.num); }
void traverseTree(struct Node *nodes, size_t left, size_t right, int traverseType) { if (left > right) { return; } if (left == right) { printf("%d ", nodes[left].value); return; } traverseTree(nodes, left+1, nodes[left].nearMax-1, traverseType); if(TRAVERSE_INORDER == traverseType) { printf("%d ", nodes[left].value); } traverseTree(nodes, nodes[left].nearMax, right, traverseType); if(TRAVERSE_POSTORDER == traverseType) { printf("%d ", nodes[left].value); } }
TreeNode *traverseTree(TreeNode *root, char *wordToTest) { int comparison = strcmp(wordToTest, root->word); if(comparison < 0){ if(root->left != NULL){ return traverseTree(root->left, wordToTest); } else { return root; } } else if(comparison > 0){ if(root->right != NULL){ return traverseTree(root->right, wordToTest); } else { return root; } } else { return root; } }
//***************************************************************************************************** static bool traverseTree(item *current) { // Receives – A pointer to a node // Task - Considering the recieved node is root and print every node below it // Returns - Whether or not the received node is valid if (current != NULL) { traverseTree(current->left); output << " " << setw(11) << left << current->ID << setw(25) << left << current->description << setw(6) << right << current->onHand << setw(11) << right << current->onOrder << endl; traverseTree(current->right); } else return false; return true; }
//创建huffman表 hlTable *buildTable(htTree *huffmanTree) { hlTable *table = (hlTable*)malloc(sizeof(hlTable)); table->first = NULL; table->last = NULL; char code[256]; int k=0; traverseTree(huffmanTree->root, &table, k, code); return table; }
//***************************************************************************************************** static void print(char type, tree *theTree) { // Receives – A letter description what to print and the pointer of a tree // Task - Prints the list of all entries that is in the tree // Returns - Nothing if (type == 'E') { //print the whole tree as a list, print error if there is no tree if (!traverseTree(theTree->getRoot())) output << "Error -- No Entry Found!"<< endl; } return; }
void traverseTree(htNode *treeNode, hlTable **table, int k, char code[256]) { //If we reach the end we introduce the code in the table if(treeNode->left == NULL && treeNode->right == NULL) { code[k] = '\0'; hlNode *aux = (hlNode *)malloc(sizeof(hlNode)); aux->code = (char *)malloc(sizeof(char)*(strlen(code)+1)); strcpy(aux->code, code); aux->symbol = treeNode->symbol; aux->next = NULL; if((*table)->first == NULL) { (*table)->first = aux; (*table)->last = aux; } else { (*table)->last->next = aux; (*table)->last = aux; } } //We concatenate a 0 for each step to the left if(treeNode->left!=NULL) { code[k]='0'; traverseTree(treeNode->left,table,k+1,code); } //We concatenate a 1 for each step to the right if(treeNode->right!=NULL) { code[k]='1'; traverseTree(treeNode->right,table,k+1,code); } }
void QuadTree::traverseTree(QuadNode * node, BoudingBox viewport, std::list<NodeObject*>& result) { if (node == nullptr) { return; } if (node->_listNodeObject.size() != 0 && viewport.Intersect(node->_bouding)) { std::copy(node->_listNodeObject.begin(), node->_listNodeObject.end(), std::back_inserter(result)); } else if (!viewport.Intersect(node->_bouding)) { return; } //traverse child node traverseTree(node->_topLeft, viewport, result); traverseTree(node->_topRight, viewport, result); traverseTree(node->_bottomLeft, viewport, result); traverseTree(node->_bottomRight, viewport, result); }
HFSBTreeNode HFSBTree::traverseTree(int nodeIndex, const Key* indexKey, KeyComparator comp, bool wildcard) { //std::cout << "Examining node " << nodeIndex << std::endl; HFSBTreeNode node(m_reader, nodeIndex, be(m_header.nodeSize)); switch (node.kind()) { case NodeKind::kBTIndexNode: { int position; uint32_t* childIndex; if (wildcard) { auto it = std::lower_bound(node.begin<Key>(), node.end<Key>(), indexKey, [=](const Key* keyA, const Key* keyB) { return comp(keyA, keyB) < 0; }); position = it.index() - 1; } else { auto it = std::upper_bound(node.begin<Key>(), node.end<Key>(), indexKey, [=](const Key* keyA, const Key* keyB) { return comp(keyA, keyB) < 0; }); position = it.index() - 1; } if (position < 0) position = 0; // recurse down childIndex = node.getRecordData<uint32_t>(position); return traverseTree(be(*childIndex), indexKey, comp, wildcard); } case NodeKind::kBTLeafNode: { return node; } case NodeKind::kBTHeaderNode: case NodeKind::kBTMapNode: break; default: std::cerr << "Invalid node kind! Kind: " << int(node.kind()) << std::endl; } return HFSBTreeNode(); }
static void mutex_lock_checked(MutexInfo* mrl, MutexInfo* object) { pid_t tid = gettid(); if (object->owner == tid) { object->lockCount++; return; } object->owner = tid; object->lockCount = 0; if (sPthreadDebugLevel >= CAPTURE_CALLSTACK) { // always record the call stack when acquiring a lock. // it's not efficient, but is useful during diagnostics object->stackDepth = get_backtrace(object->stackTrace, STACK_TRACE_DEPTH); } // no other locks held in this thread -- no deadlock possible! if (mrl == NULL) return; // check if the lock we're trying to acquire is a direct descendant of // the most recently locked mutex in this thread, in which case we're // in a good situation -- no deadlock possible if (historyListHas(&mrl->children, object) >= 0) return; pthread_mutex_lock_unchecked(&sDbgLock); linkParentToChild(mrl, object); if (!traverseTree(object, mrl)) { backtrace_shutdown(); LOGW("%s\n", kEndBanner); unlinkParentFromChild(mrl, object); // reenable pthread debugging for this thread sPthreadDebugDisabledThread = -1; } else { // record the call stack for this link // NOTE: the call stack is added at the same index // as mrl in object->parents[] // ie: object->parents.count == object->stacks.count, which is // also the index. if (sPthreadDebugLevel >= CAPTURE_CALLSTACK) { callstackListAdd(&object->stacks, object->stackDepth, object->stackTrace); } } pthread_mutex_unlock_unchecked(&sDbgLock); }
TNode *traverseTree(TOutlineViewer *outLine, Boolean(*action)(TOutlineViewer *, TNode *, int, int, long, ushort), int &position, Boolean &checkResult, TNode *cur, int level, long lines, Boolean lastChild) { Boolean result; int j, childCount; TNode *ret; ushort flags; Boolean children; if (cur == 0) return 0; children = outLine->hasChildren(cur); flags = 0; if (lastChild) flags |= ovLast; if (children && outLine->isExpanded(cur)) flags |= ovChildren; if (! children || outLine->isExpanded(cur)) flags |= ovExpanded; position++; result = (*action)(outLine, cur, level, position, lines, flags); if (checkResult && result) return cur; if (children && outLine->isExpanded(cur)) { childCount = outLine->getNumChildren(cur); if (! lastChild) lines |= 1 << level; for (j = 0; j < childCount; j++) { ret = traverseTree(outLine, action, position, checkResult, outLine->getChild(cur, j), level + 1, lines, Boolean(j == (childCount - 1))); if (ret) return ret; } } return 0; }
hlTable * buildTable(htTree * huffmanTree) { //We initialize the table hlTable *table = (hlTable *)malloc(sizeof(hlTable)); table->first = NULL; table->last = NULL; //Auxiliary variables char code[256]; //k will memories the level on which the traversal is int k=0; //We traverse the tree and calculate the codes traverseTree(huffmanTree->root,&table,k,code); return table; }
//------------------------------------------------------------------------------ void QuadTree::traverseTree(QuadTreeNode* n) { if (n != NULL) { PRINT_DEBUG("---------------------------\n"); PRINT_DEBUG("min x: %d, max x: %d\n", (int)n->min[0], (int)n->max[0]); PRINT_DEBUG("min z: %d max z: %d\n", (int)n->min[1], (int)n->max[1]); PRINT_DEBUG("---------------------------\n"); for (int i = 0; i < 4; i++) { traverseTree(n->child[i]); } } else { PRINT_DEBUG("------------------\n"); PRINT_DEBUG("leaf node\n"); PRINT_DEBUG("------------------\n"); } }
void Expression::compileExpression( const QString & expression ) { // Make a tree to put the expression in. BTreeBase *tree = new BTreeBase(); BTreeNode *root = new BTreeNode(); // parse the expression into the tree buildTree(expression,tree,root,0); // compile the tree into assembly code tree->setRoot(root); tree->pruneTree(tree->root()); traverseTree(tree->root()); // Note deleting the tree deletes all nodes, so the root // doesn't need deleting separately. delete tree; return; }
void buildTree(void) { buildQueue(); printQueue(); int lvalue,rvalue; if ( !head ) exit(EXIT_FAILURE); while ( head -> right ) { Node* temp = createNode(); temp -> bottomleft = head; temp -> bottomright = head -> right; head = head -> right -> right; temp -> bottomleft -> right = NULL; temp -> bottomright -> right = NULL; if ( temp -> bottomleft -> data.datatype.type == LETTER ) lvalue = characters[ temp -> bottomleft -> data.dataletter.letter ].count; else lvalue = temp -> bottomleft -> data.datanum.num; if ( temp -> bottomright -> data.datatype.type == LETTER ) rvalue = characters[ temp -> bottomright -> data.dataletter.letter ].count; else rvalue = temp -> bottomright -> data.datanum.num; temp -> data.datatype.type = NUMBER; temp -> data.datanum.num= lvalue + rvalue; if ( !head ) head = temp; else addToQueue( temp ); } buffer = (unsigned char*)malloc ( currsize ); traverseTree(head,0UL); //creation of code is done. // tester started.. for ( int var = 0; var <128 ;++ var ) { if ( characters[var].count ){ printf("%c\t%s\n",var,characters[var].bitstring); // writeBits( characters[var].bitstring ); } } }
std::list<NodeObject*> QuadTree::getListObjectInViewPort(BoudingBox viewport) { std::list<NodeObject*> result; traverseTree(_root, viewport, result); result.sort([](NodeObject* node1,NodeObject* node2) { return node1->_objectID < node2->_objectID; }); //resolve duplicates object; auto pos = std::unique(result.begin(), result.end(),[](NodeObject* id1,NodeObject* id2) { return id1->_objectID == id2->_objectID; }); result.erase(pos, result.end()); return result; }
BOMBTreeNode BOMBTree::traverseTree(uint32_t nodeIndex, const void* indexKey, KeyComparator comp, bool wildcard) { BOMBTreeNode node(m_store, nodeIndex); switch (node.kind()) { case kBOMIndexNode: { int position; if (wildcard) { auto it = std::lower_bound(node.begin<void>(), node.end<void>(), indexKey, [=](const void* keyA, const void* keyB) { return comp(keyA, keyB) < 0; }); position = it.index() - 1; } else { auto it = std::upper_bound(node.begin<void>(), node.end<void>(), indexKey, [=](const void* keyA, const void* keyB) { return comp(keyA, keyB) < 0; }); position = it.index() - 1; } if (position < 0) position = 0; // recurse down return traverseTree(node.getRecordDataBlockId(position), indexKey, comp, wildcard); } case kBOMLeafNode: return node; default: std::cerr << "Invalid node kind: " << int(node.kind()) << std::endl; } return BOMBTreeNode(); }
void gcTree(v8::Isolate* isolate, Node* startNode) { WillBeHeapVector<RawPtrWillBeMember<Node>, initialNodeVectorSize> partiallyDependentNodes; Node* node = startNode; while (Node* parent = node->parentOrShadowHostOrTemplateHostNode()) node = parent; if (!traverseTree(node, &partiallyDependentNodes)) return; // We completed the DOM tree traversal. All wrappers in the DOM tree are // stored in partiallyDependentNodes and are expected to exist in the new space of V8. // We report those wrappers to V8 as an object group. if (!partiallyDependentNodes.size()) return; Node* groupRoot = partiallyDependentNodes[0]; for (size_t i = 0; i < partiallyDependentNodes.size(); i++) { partiallyDependentNodes[i]->markAsDependentGroup(groupRoot, isolate); } }
int isPrefix(TreeNode *root, char *wordToTest) { TreeNode *node = traverseTree(root, wordToTest); while(1){ int comparison = strcmp(wordToTest, node->word); if(comparison<0){ if(strncmp(wordToTest, node->word, strlen(wordToTest))==0){ return 1; } return 0; } else if(comparison == 0){ return 1; } else { if(node->parent == NULL){ return 0; } else { node = node->parent; } } } }
HFSBTreeNode HFSBTree::findLeafNode(const Key* indexKey, KeyComparator comp, bool wildcard) { return traverseTree(be(m_header.rootNode), indexKey, comp, wildcard); }