int treesearch(Treeptr p, char *w) /* Check whether word w is in tree p*/ { int cond; if (p == NULL) /* If tree is empty*/ return 0; /* We did't find word*/ if ((cond = strcmp(w, p->word)) == 0) /* Word w is the same with word of current node*/ return 1; else if (cond < 0) /* If w precedes word of current node*/ return treesearch(p->left, w); /* Search left suftree*/ else /* Otherwise*/ return treesearch(p->right, w); /* search right subtree*/ }
node* treesearch (node* root, int index) { if (root != NULL) { if (root->record->index == index || root == NULL) { return root; } if (index > root->record->index) { return treesearch (root->right, index); } if (index < root->record->index) { return treesearch (root->left, index); } } /* if (root != NULL) */ return NULL; }
/* searches the abstract syntax tree for the supplied expression/string */ static searchASTExpr *treesearch(searchASTExpr *expr, exprunion *loc) { searchASTExpr *ret = compareloc(expr, loc); if(ret) return ret; if(expr->type == AST_NODE_CHILD) { int i; for(i=0;i<expr->u.child.argc;i++) { searchASTExpr *d = treesearch(&expr->u.child.argv[i], loc); if(d) return d; } } return NULL; }
/* * searches the AST cache, if this fails it goes and searches the tree. * the cache is hit most of the time and I guess makes it nearly O(1) amortised... */ searchASTExpr *cachesearch(searchASTCache *cache, exprunion *loc) { searchASTExpr *ret = compareloc(cache->tree, loc); int i; if(ret) return ret; for(i=0;i<AST_RECENT;i++) { if(!cache->cache[i]) continue; ret = compareloc(cache->cache[i], loc); if(ret) return ret; } return treesearch(cache->tree, loc); }