示例#1
0
文件: tree.c 项目: imej/c
/*searchtree: search for a word in the tree */
struct tnode * searchtree(struct tnode *tree, const char *wd)
{
    struct tnode *p = tree;

    if (p == NULL || wd == NULL) {
        return NULL;
    }

    if (strcmp(p->word, wd) < 0) {
        p = searchtree(p->right, wd);
    } else if (strcmp(p->word, wd) > 0){
        p = searchtree(tree->left, wd);
    } 

    return p;
}
示例#2
0
int main ( void ) {
	printf("100%%\n");
	Tree *found = NULL ;
	Tree *Tree1 = createtree() ;
	searchtree( Tree1 ) ;
	return 0 ; 
}
示例#3
0
void process() {
    int i;
    if (c != 0) printf("\n");
    c++;
    printf("CASE %d\n", c);
    for (i = 0; i < n; i++) {
        scanf("%d", input + i);
        input[i]--;
        parent[i] = -1;
    }
    init();
    scanf("%d", &m);
    int u, v;
    for (i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        u--;
        v--;
        searchtree(0, n - 1, u, v, u, v);
        unionset(u, v);
    }
    scanf("%d", &k);
    for (i = 0; i < k; i++) {
        scanf("%d%d", &u, &v);
        u--;
        v--;
        if (find(u) == find(v)) printf("YES\n");
        else printf("NO\n");
    }
}
示例#4
0
void searchtree(int s, int e, int & maximal, int & minimal, int u, int v) {
    if (e - s + 1 <= 0) {
        maximal = minimal = -1;
        return;
    }
    if (s == u && v == e) {
        if (e - s + 1 <= 0) {
            maximal = minimal = -1;
            return;
        }
        if (e - s + 1 == 1) {
            maximal = minimal = input[e];
            return;
        }
        if (e - s + 1 == 2) {
            maximal = input[s] > input[e] ? input[s] : input[e];
            minimal = input[s] > input[e] ? input[e] : input[s];
            return;
        }
        maximal = arr[(s + e) / 2].max;
        minimal = arr[(s + e) / 2].min;
        return;
    }
    int temp = (s + e) / 2;
    int i, j;
    if (u <= temp && temp <= v) {
        maximal = input[temp];
        minimal = input[temp];
        if (u <= temp - 1) {
            searchtree(s, temp - 1, i, j, u, temp - 1);
            if (i > maximal) maximal = i;
            if (j >= 0 && j < minimal) minimal = j;
        }
        if (v >= temp + 1) {
            searchtree(temp + 1, e, i, j, temp + 1, v);
            if (i > maximal) maximal = i;
            if (j >= 0 && j < minimal) minimal = j;
        }
        return;
    }
    if (u > temp) {
        searchtree(temp + 1, e, maximal, minimal, u, v);
        return;
    }
    searchtree(s, temp - 1, maximal, minimal, u, v);
}
示例#5
0
文件: tree.c 项目: imej/c
/*intree: if a string is in a tree */
int intree(struct tnode *tree, const char *wd)
{
    struct tnode *p = searchtree(tree, wd);
    if (p == NULL) {
        return 0;
    } else {
        return 1;
    }
}
示例#6
0
int comp (ppint *pp, int key) {
  int x, y;
  int listcnt;
  node *root, *temp;;
  int bl[MAXGENE];
  int cpt;

  listcnt = 0;
  root = NULL;

  for (x = 0; x <= MAXGENE; x++) {
    bl[x] = 0;
  }

  for (x = 1; x <= pp->seed[key].cnt; x++) {
    listcnt++;
    bl[listcnt] = pp->seed[key].part[x];
    addtree(pp->seed[key].part[x], &root);
  }
 
  for (x = 1; x <= MAXGENE; x++) {
    if (bl[x] != 0) {
      for (y = 1; y <= pp->seed[bl[x]].cnt; y++) {
	temp = searchtree(root, pp->seed[bl[x]].part[y]);
	if (temp == NULL) {
	  listcnt++;
	  bl[listcnt] = pp->seed[bl[x]].part[y];
	  addtree(pp->seed[bl[x]].part[y], &root);
	}
      }
    }
  }
  
  cpt = cnttree(root);
  destroytree(root);
  return cpt;
}