コード例 #1
0
ファイル: wordcount.c プロジェクト: bnjzer/c
struct tnode *addtree(struct tnode *p, char *w, int nline){
  int cond;

  if(p == NULL){ // it's a new word
    p = talloc(); //get addres for new node
    p->word = strdup2(w);
    p->count = 1;
    p->left = p->right = NULL;
    p->lines[0] = nline;
    p->indLastoccurence = 0;
  } else if((cond = strcmp(w, p->word)) < 0)
    p->left = addtree(p->left, w, nline);
  else if(cond > 0)
    p->right = addtree(p->right, w,nline);
  else {
    p->count++;
    if(p->indLastoccurence == MAXOCCURENCE-1)
      printf("Max occurences reached for word %s\n", p->word);
    else
      if(p->lines[p->indLastoccurence] != nline)
        p->lines[++p->indLastoccurence] = nline;
  }

  return p;
}
コード例 #2
0
/*
   添加树

   每一步中,新单词与节点中存储的单词进行比较,随后,通过递归调用addtree 
   而转向左子树或右子树。该单词最终将与树中的某节点匹配(这种情况下计数
   值加 1) ,或遇到一个空指针(表明必须创建一个节点并加入到树中) 。若生成
   了新节点,则addtree 返回一个指向新节点的指针,该指针保存在父节点中
 */
struct tnode *addtree(struct tnode *p,char *w,int *nlines) {
	struct link *addlink(struct link *p,int *nlines);

	struct tnode *alloc(void);
	char *strup(char *w);
	int flag;
	/*
	   1.树还未建立,建树
	   2.比较新单词时,建树
	 */
	if(p == NULL ) {
		p = alloc();
		p->linkpoint = addlink(p->linkpoint,nlines);
		p->word = strup(w);
		p->left = p->right = NULL;
	}
	else if( (flag = strcmp(w,p->word)) == 0 ) {
		p->linkpoint= addlink(p->linkpoint,nlines);
	}
	//当小于当前节点时添加左子树
	else if(flag < 0 ) {
		p->left = addtree(p->left,w,nlines);
	}
	//当大于当前节点时添加右子树
	else {
		p->right = addtree(p->right,w,nlines);
	}

	return p;
}
コード例 #3
0
ファイル: 6-6.c プロジェクト: devilTian/Fragment
struct tnode *addtree(struct tnode *p, char *word) {

	int cmp;
	struct tnode *newnode;

	if (p == NULL) {
		p = talloc();
		p->word = strdupx(word);
		p->count = 1;
		p->left  = NULL;
		p->right = NULL;
	} else {
		if ((cmp = strcmp(p->word, word)) == 0) {
			p->count++;
		} else if (cmp > 0) {
			p->left = addtree(p->left, word);
		} else if (cmp < 0) {
			p->right = addtree(p->right, word);
		} else {
			printf("error");
			exit(-1);
		}
	}
	return p;
}
コード例 #4
0
ファイル: test_2.cpp プロジェクト: Ramadanov/DATECS
struct tnode *addtree(struct tnode*p, char *w)
{
	int cond;
	if (p == NULL)
	{
		//p = talloc();
		//p->word = strdup(w);
		//p->count = 1;
		//p->left = p->right = NULL;
	}
	else if ((cond = strcmp(w, p->word)) == 0)
	{
		p->count++;
	}
	else if (cond < 0)
	{
		p->left = addtree(p->right, w);
	}
	else
	{
		p->right = addtree(p->right, w);
	}
	return p;

}
コード例 #5
0
ファイル: ex6_2.c プロジェクト: simsimzone/ch6
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
	int cond;
	if (p == NULL) {
		/* a new word has arrived */
		p = talloc(); /* make a new node */
		p->word = str_dup(w);
		p->count = 1;
		p->group = currgroup;
		p->left = p->right = NULL;
		return p;
	}
	if (str_n_cmp(w, p->word, n) == 0)	/* group matching */
	{
		if (!p->group)	/* if it doesn't has group set a new group for it */
			p->group = ++lg;
		currgroup = p->group;
	}
	if ((cond = str_cmp(w, p->word)) == 0)
		p->count++; /* repeated word */
	else if (cond < 0) /* less than into left subtree */
		p->left = addtree(p->left, w);
	else /* greater than into right subtree */
		p->right = addtree(p->right, w);
	return p;
}
コード例 #6
0
ファイル: 6_2.c プロジェクト: aaabhilash97/kr
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w,int set,int len)
{
	int cond;
	if (p == NULL) {
		p = talloc();
		p->word = strdup1(w);
		p->count = 1;
		p->set=set;
		p->left = p->right = NULL;
	} 	
	else if ((cond = strcmp1(w, p->word,len)) == 0){
		p->set=0;
		p->count++;
	}
	else if (cond ==-1)
		p->left = addtree(p->left, w,0,len);
	else if(cond==1)
		p->right = addtree(p->right, w,0,len);
	else if(cond==-2){
		p->left=addtree(p->left,w,1,len);
		p->set=1;
	}
	else if(cond==2){
		p->right=addtree(p->right,w,1,len);
		p->set=1;
	}
	return p;
}
コード例 #7
0
ファイル: indexer.c プロジェクト: andrewsuzuki/kr-solutions
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
	int cond;
	struct tlinen *nln;

	if (p == NULL) {
		p = talloc();
		p->word = _strdup(w);
		p->left = p->right = NULL;

		p->linen = lalloc();
		p->linen->line = cline;
		p->linen->next = NULL;
	} else if ((cond = strcmp(w, p->word)) == 0) {
		for (nln = p->linen; nln != NULL && nln->line != cline; nln = nln->next);
		if (nln == NULL) {
			nln = lalloc();
			nln->line = cline;
			nln->next = p->linen;
			p->linen = nln;
		}
	} else if (cond < 0)
		p->left = addtree(p->left, w);
	else
		p->right = addtree(p->right, w);
	return p;
}
コード例 #8
0
ファイル: varcount.c プロジェクト: andrewsuzuki/kr-solutions
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w)
{
	int cond;
	struct tgroup *newgroup;

	if (p == NULL) {
		p = talloc();
		p->stub = _strdup(w, firstn);
		p->left = p->right = NULL;

		p->group = galloc();
		p->group->word = _strdup(w, -1);
		p->group->next = NULL;
	} else if ((cond = strncmp(w, p->stub, firstn)) == 0) {
		for (newgroup = p->group; newgroup != NULL && strcmp(newgroup->word, w); newgroup = newgroup->next);
		if (newgroup == NULL) {
			newgroup = galloc();
			newgroup->word = _strdup(w, -1);
			newgroup->next = p->group;
			p->group = newgroup;
		}
	} else if (cond < 0)
		p->left = addtree(p->left, w);
	else
		p->right = addtree(p->right, w);
	return p;
}
コード例 #9
0
ファイル: tree.c プロジェクト: jediwjr/rose_backup
int main () {
  struct tnode *root;
  root = addtree(NULL, 5);
  root = addtree(root, 3);
  root = addtree(root, 7);
	root = addtree(root, 1);
	root = addtree(root, 4);
	printf("Height = %d\n", height(root));
  printinorder(root);
}
コード例 #10
0
ファイル: fastmarch.c プロジェクト: 1014511134/src
void fastmarch(void) 
/*< fast marching time-to-depth conversion >*/
{
  int i,j,ii,jj,ind,m,mm;
  char ch,dir;

  for( i=0;i<nx;i++ ) {
      ind=i+nx;
      ch=solve(ind,'z',1);
      if( ch=='s') {
	  addtree(ind);
	  *(ms+ind)='c';
      }
  }
  
  while( count > 0 ) {
    ind=(*(r+1));
    j=ind/nx;
    i=ind%nx;
    *(ms+ind)='a';
    deltree();
    /*(      printf("accept %i, %i\t",i,j);*/
    /* update the nearest neighbors */
    for( m=0; m<4; m++ ) {
      ch='n';
      dir=(m<2) ? 'x' : 'z';
      mm=2*(m%2)-1;
      if( dir=='x' ) {
	ii=i+mm;
	jj=j;
	if( ii>=0 && ii<nx ) ch='y';
      }
      else if( dir=='z' ) {
	ii=i;
	jj=j+mm;
	if( jj>=0 && jj<nz ) ch='y';
      }
      if( ch=='y' ) {
	ind=ii+jj*nx;
	if( *(ms+ind)=='a' ) ch='n';
	else {
	  ch=solve(ind,dir,mm);
	  if( ch=='s' ) {
	    if( *(ms+ind)=='u' ) {
	      addtree(ind);
	      *(ms+ind)='c';
	    }
	    else updtree(ind);
	    /* printf("(%i,%i) is updated; t0=%.4e\tx0=%.4e\tv=%.4e\t q=%.4e\n",ii,jj,*(t0+ind),*(x0+ind),*(v+ind),*(q+ind));*/
	  }
	}
      }
    }
  }
}
コード例 #11
0
ファイル: tree.c プロジェクト: jediwjr/rose_backup
struct tnode *addtree(struct tnode *p, int value)
{  
  if (p == NULL) {
    p = (struct tnode *) malloc(sizeof(struct tnode));
    p->value = value;
    p->left = p->right = NULL;
  } else if (value < p->value) {
    p->left = addtree(p->left, value);
  } else {
    p->right = addtree(p->right, value);
  }
  return p;
}
コード例 #12
0
ファイル: c.c プロジェクト: jefferyyuan/wypractice
struct tnode *addtree(struct tnode *p, char *w, int num, int *found) {
  int cond;
  if(p == NULL) {
    p = talloc();
    p->word = wystrdup(w);
    p->match = *found;
    p->left = p->right = NULL;
  } else if((cond = wystrcmp(w, p, num, found)) < 0) {
    p->left = addtree(p->left, w, num, found);
  } else if(cond > 0)
    p->right = addtree(p->right, w, num, found);
  return p;
}
コード例 #13
0
ファイル: bt.c プロジェクト: Ironsongming/homework
struct tnode *addtree(struct tnode *p, int v) {
    if (p == NULL) {
        p = (struct tnode *) malloc (sizeof (struct tnode));
        p->val = v;
        p->count = 1;
        p->left = p->right = NULL;
    } else if (p->val == v)
        p->count++;
    else if (p->val > v)
        p->left = addtree(p->left, v);
    else
        p->right = addtree(p->right, v);
    return p;
}
コード例 #14
0
ファイル: ex6-2.c プロジェクト: haimoz/HelloC
/* addtree:  add a value to a tree, return a pointer to the root.  
 * if the value is already in the tree, do nothing.  
 */
struct tnode *addtree(char *v, struct tnode *n)
{
	int cond; /* condition of string comparison */
	
	if (n == NULL) {
		n = (struct tnode *)malloc(sizeof(struct tnode));
		n->value = strdup(v);
		n->left = n->right = NULL;
	} else if ((cond = strcmp(v, n->value)) < 0) {
		n->left = addtree(v, n->left);
	} else if (cond > 0) {
		n->right = addtree(v, n->right);
	}
	return n;
}
コード例 #15
0
ファイル: 6-03.c プロジェクト: RuohengLiu/learn-to-code
struct tnode *addtree(struct tnode *p, char *w) {
    int cond;
    if (p == NULL) {
        p = talloc();
        p->word = Strdup(w);
        p->firstline = NULL;
        p->firstline = addline(p->firstline, line);
        p->left = p->right = NULL;
    } else if ((cond = strcmp(w, p->word)) == 0)
        addline(p->firstline, line);
    else if (cond < 0)
        p->left = addtree(p->left, w);
    else p->right = addtree(p->right, w);
    return p;
}
コード例 #16
0
ファイル: i.c プロジェクト: jefferyyuan/wypractice
struct tnode *addtree(struct tnode *p, char *w) {
  int cond;
  if(p == NULL) {
    p = talloc();
    p->word = wystrdup(w);
    p->num = 1;
    p->left = p->right = NULL;
  } else if((cond = strcmp(w, p->word)) == 0) {
    p->num++;
  } else if(cond < 0)
    p->left = addtree(p->left, w);
  else if(cond > 0)
    p->right = addtree(p->right, w);
  return p;
}
コード例 #17
0
ファイル: 6.4.c プロジェクト: mScotty/the_labyrinth
int main(int argc, char *argv[])
{
    struct tnode *root;
    char word[MAXWORD];
    char c;
    int nlines = 1;
    struct tnode *sorted = NULL;

    root = NULL;
    while ((c=getword(word, MAXWORD)) != EOF) {
        /* convert word to lower case */
        int i;
        for(i = 0; word[i] != '\0'; i++)
            word[i] = tolower(word[i]);

        /* add word to the tree */
        if (c == '\n')
            nlines++;
        else //if (!noiseword(word))
            root = addtree(root,word,nlines); 
    }
   
    /* prints the tree by order of decreasing frequency */
    sorted = sort(sorted, root);
    freqprint(sorted);
    return 0;
}
コード例 #18
0
ファイル: ex6_2.c プロジェクト: simsimzone/ch6
main(int argc, char *argv [])
{
	struct tnode *root;
	char word[MAXWORD];
	if (argc > 1)
	{
		n = atoi(*++argv);
		n = n <= 0 ? COMP_LEN : n;
	}
	else
		n = COMP_LEN;
	root = NULL;
	while (getword(word, MAXWORD) != EOF)
	{
		if (isalpha(word[0]) && (int) strlen(word) >= n)
		{
			currgroup = 0;
			root = addtree(root, word);
		}
	}
	lg = 0;
	treeprint(root);
	getchar();
	return 0;
}
コード例 #19
0
ファイル: exercise6-4.c プロジェクト: bonpiedlaroute/bplr
int main(int argc, char *argv[])
{
    struct tword *root, *tmp;
    char word[MAXWORD];
    struct tnode *tree = NULL;

    root = NULL;

    while( getword(word, MAXWORD) != EOF )
    {
        if( isalpha(word[0]) || word[0] == '_' || word[0] == '#' )
            root = add(root, word);
    }

    tmp = root;

    while( tmp != NULL )
    {
        tree = addtree(tree, tmp->word, tmp->count);
        tmp = tmp->next;
    }
    
    treeprint(tree);
    return 0;
}
コード例 #20
0
ファイル: combined_sources.c プロジェクト: imscs21/myuniv
/* word frequency count */
int main() {
	struct tnode *root, *freqroot;
	char word[MAXWORD];
	char *key[MAXKEYLIST]={NULL,};
	root = NULL;
	freqroot = NULL; // 새로운 트리를 위한 뿌리 노드를 만들어야 한다.
	while (getword(word, MAXWORD) != EOF)
		if (isalpha(word[0]))
			root = addtree(root, word);
	freqroot = createtreefromtree(root, freqroot);
	 treeprint(root);
     printf("==END::raw tree==\n");
	treeprint2(freqroot);
    printf("==END::freq tree==\n");
	inorder_traversal(root,key);
	int ti=0;
	while(key[ti]!=NULL){
        //if(ti<10){
		root=reducetree(root,key[ti++]);
        //}
		//printf("%s\n",key[ti++]);
	}
	//printf("end key list\n");
	treeprint(root);
	printf("finish print\n");
	return 0;
}
コード例 #21
0
/* C identifier frequency count */
int main(int argc, char *argv[])
{
	struct tnode *root;
	char word[MAXWORD];
	char *getword(char *, int);
	int flag;		/* 1 if inside comment block */
	int signf;		/* number of significant characters */

	flag = 0;
	root = NULL;
	signf = (argc > 1) ? atoi(argv[1]) : SIGNF;
	while (getword(word, MAXWORD) != NULL) {
		if (strcmp(word, "/*") == 0)
			flag = 1;
		if (flag == 1) {
			if (strcmp(word, "*/") == 0)
				flag = 0;
			continue;
		}
		if (identifier(word))
			root = addtree(root, word);
	}
	treeprint_signf(root, signf, 0);
/*	treeprint(root); */
	return 0;
}
コード例 #22
0
ファイル: enode.c プロジェクト: haplesshero13/lcc-lc3
static Tree addtree(int op, Tree l, Tree r) {
	Type ty = inttype;

	if (isarith(l->type) && isarith(r->type)) {
		ty = binary(l->type, r->type);
		l = cast(l, ty);
		r = cast(r, ty);		
	} else if (isptr(l->type) && isint(r->type))
		return addtree(ADD, r, l);
	else if (  isptr(r->type) && isint(l->type)
	&& !isfunc(r->type->type))
		{
			long n;
			ty = unqual(r->type);
			n = unqual(ty->type)->size;
			if (n == 0)
				error("unknown size for type `%t'\n", ty->type);
			l = cast(l, promote(l->type));
			if (n > 1)
				l = multree(MUL, cnsttree(signedptr, n), l);
			if (isunsigned(l->type))
				l = cast(l, unsignedptr);
			else
				l = cast(l, signedptr);
			if (YYcheck && !isaddrop(r->op))		/* omit */
				return nullcall(ty, YYcheck, r, l);	/* omit */
			return simplify(ADD, ty, l, r);
		}

	else
		typeerror(op, l, r);
	return simplify(op, ty, l, r);
}
コード例 #23
0
ファイル: ex-6.2.c プロジェクト: jaseemkp/KNR-C-PROG
// addtree: add a node with w, at of below p
struct tnode *addtree(struct tnode *p, char *w)
{
	int cond;
	if(p == NULL){ //a new word has arrived
	  p = talloc(); // make a new node
	  p->word = strdup(w);
	  p->count = 1;
	  p->left = p->right = NULL;
	}else if ((cond = strcmp(w, p->word)) == 0)
		p->count++; // repeated word
	else if(cond < 0)  //less than into left subtree
		p->left = addtree(p->left, w);
  	else //greater than into right subtree
		p->right = addtree(p->right, w);
	return p;
}
コード例 #24
0
ファイル: dict.c プロジェクト: vatlidak/undergrad
void addtree(Treeptr* p, char *w, int code)    				/* Insert word w into tree p */
{ 
	int cond;
  	if (*p == NULL) {                              			/* If tree is empty */
    		*p = malloc(sizeof(Treenode));    			/* Allocate space for new node */
		(*p)->word = malloc((strlen(w)+1) * sizeof(char));	/* Allocate space to copy word */
    		strcpy((*p)->word, w);                 			/* Copy word w to tree node */
    		(*p)->code=code;
    		(*p)->left = NULL;            				/* Left subtree of new node is empty */
		(*p)->right = NULL;          				/* Right subtree of new node is empty*/ 
	}
 	else if ((cond = strcmp(w, (*p)->word)) < 0)			/* Does word w precede word of current node?  */
    		addtree(&(*p)->left, w,code);				/* If yes, insert it into left subtree */
  	else if (cond > 0)       					/* Does it follow word of current node?  */
    		addtree(&(*p)->right, w,code);				/* If yes, insert it into right subtree */
  							/* If it is the same with word of current node, do not insert it */
}
コード例 #25
0
//addtree函数:在p的位置或p的下方增加一个w节点
struct tnode *
addtree(struct tnode *p, char *w){
	int cond;

	if(p == NULL){			//该单词是一个新单词
		p = talloc();
		p->word = strdup(w);
		p->count = 1;
		p->left = p->right = NULL;
	}else if((cond = strcmp(w, p->word)) == 0)
		p->count++;
	else if(cond < 0)	//如果小于该节点中的单词,则进入左子树
		p->left = addtree(p->left, w);
	else
		p->right = addtree(p->right, w);
	return p;
} 
コード例 #26
0
/* addtree: add a node with w, at or below p */
static struct tnode *addtree(struct tnode *p, char *w)
{
	int cond;

	if (p == NULL) {	/* a new word has arrived */
		p = talloc();
		p->word = strdup(w);
		p->count = 1;
		p->left = p->right = NULL;
	} else if ((cond = strcmp(w, p->word)) == 0)
		p->count++;	/* repeated word */
	else if (cond < 0)	/* less than into left subtree */
		p->left = addtree(p->left, w);
	else			/* greater than into right subtree */
		p->right = addtree(p->right, w);
	return p;
}
コード例 #27
0
ファイル: ex4.c プロジェクト: Ardra/k-r-exercise
struct tnode *addtree(struct tnode *p, char *w, int lnum)
{
    int cond;

    if (p == NULL) {
        p = talloc();
        p->word = strdup(w);
        p->linenum[0] = lnum;
        p->count = 1;
        p->left = p->right = NULL;
    } else if ((cond = strcmp(w, p->word)) == 0)
        p->linenum[p->count++] = lnum;
    else if (cond < 0)
        p->left = addtree(p->left, w, lnum);
    else
        p->right = addtree(p->right, w, lnum);
    return p;
}
コード例 #28
0
ファイル: 6-2.c プロジェクト: yuyabee/kandr2
  struct tnode *
addtree(struct tnode *p, char *w)
{
  int cond;

  if (p == NULL) {
    p = talloc();
    p->word = strdup(w);
    p->count = 1;
    p->left = p->right = NULL;
  } else if ((cond = strcmp(w, p->word)) == 0)
    p->count++;
  else if (cond < 0) /* strcmp return negative value if the first char* is smaller */
    p->left = addtree(p->left, w);
  else
    p-> right = addtree(p->right, w);
  return p;
}
コード例 #29
0
ファイル: largest_comp.c プロジェクト: KahnR/Misc-Code
void addtree (int info, node **t) {
  node *scan, *p;
  scan = *t;

  if (scan == NULL) {
    p = malloc(sizeof(node));
    p->item = info;
    p->left = NULL;
    p->right = NULL;
    *t = p;
  }
  else if (info < scan->item) {
    addtree(info, &scan->left);
  }
  else if (info > scan->item) {
    addtree(info, &scan->right);
  }
}
コード例 #30
0
ファイル: 6.4.c プロジェクト: mScotty/the_labyrinth
/* addtree: add a node with w, at or below p */
struct tnode *addtree(struct tnode *p, char *w, int lnum)
{
    int cond;

    if (p == NULL) {    /* a new word has arrived */
        p = talloc();   /* make a new node */
        p->word = strdup(w);
        p->linenum[0] = lnum;
        p->count = 1;
        p->left = p->right = NULL;
    } else if ((cond = strcmp(w, p->word)) == 0)
        p->linenum[p->count++] = lnum;  /* repeated word */
    else if (cond < 0)  
        p->left = addtree(p->left, w, lnum);
    else                
        p->right = addtree(p->right, w, lnum);
    return p;
}