Exemplo n.º 1
0
void inorderprint(Bst* bst) { //중위 순회를 구현한 함수
	if(bst==NULL)
		return;
	inorderprint(bst->leftchild);
	printf("%s ", bst->value);
	inorderprint(bst->rightchild);
}
Exemplo n.º 2
0
void main(void) {
	int i;
	Bst* bst=allocationTree("one"); //첫 루트 트리노드를 생성
	char* data; //입력시 사용자 입력을 받을 변수
	char removedata[5]; //삭제시 사용자 입력을 받을 변수

	inorderprint(bst);
	printf("\n");
	for(i=1; i<=19; i++) { //19번 반복하면서 twet까지 입력한다.
		printf("새로운 노드 입력 : ");
		data=(char*)malloc(sizeof(char)*5);
		scanf("%s", data);
		insertTree(bst, allocationTree(data));
		inorderprint(bst);
		printf("\n");
	}
	for(i=1; i<=20; i++) { //총 20번 입력의 역순과 입력순서로 삭제하기 위한 반복문이다.
		printf("삭제 할 노드 입력 : ");
		scanf("%s", removedata);
		removeTree(bst, removedata);
		inorderprint(bst);
		printf("\n");
		leftdepth=0; //삭제할 노드의 양쪽 서브트리를 비교하기 위한 전역변수를 할때마다 초기화한다.
		rightdepth=0;
		leftnumnode=0;
		rightnumnode=0;
	}
}
Exemplo n.º 3
0
int inorderprint(struct bst_node *t)
{
  FILE *f_out;
  char filename2[] = "output.txt";
  int i;

  if((f_out = fopen(filename2,"w+")) == NULL)
    {
      printf("Can not open %s!\n", filename2);
      return 0;
    }
  if(t != NULL)
    {
      inorderprint(t->left);
      printf("%s : %d ", t->data.word, t->count);
      for(i=0; i< t->nlines; i++) printf(" %d ", t->lines[i]);
      printf("\n");
      inorderprint(t->right);
    }
  fclose(f_out);
  return 0;	
}
Exemplo n.º 4
0
main()
{
  struct bst_node *t=NULL;
  EleType item, item1;
  char buff[maxlength];
  struct bst_node** node;
  int i, n, k = 0, flag = 0, check, h = 0; //flag: bao truoc do co dau '.', '!', '?'

  FILE *f_in;
  char filename1[]="vanban.txt";
  if((f_in = fopen(filename1, "r")) == NULL)
    {
      printf("Can not open %s!\n", filename1);
      return 0;
    }
  
   FILE *f;  
   char filename3[] = "stopw.txt";
   if((f = fopen(filename3,"r")) == NULL)  
     {  
       printf("Can not open %s!\n", filename3);  
       fclose(f); 
       return 0;  
     }  

   struct bst_node *root=NULL;
   struct bst_node** node1;
   char buff1[maxlength];
   while(1)
     {
       fscanf(f, "%s", buff1);
       strcpy(item1.word, buff1);
       insert_node(&root, item1);
       if(feof(f)) break;
     }//Doc file stopw.txt
    fclose(f);
    
    int linum = 0;
    while (1)
      {
	fscanf(f_in, "%s", buff);
     
	n = strlen(buff);	 
	if (n == 0) continue; 
	if(k!=0)
	  {
	    if ((buff[0] >= 'A') && (buff[0] <= 'Z'))
	      {
		if (flag == 0)
		  {
		    if((buff[n-1] == '.') || (buff[n-1] == '!') || (buff[n-1] == '?'))
		      {
			flag = 1;
			continue;
		      }
		    else continue;		    
		  }
	      } //Xu ly danh tu rieng
	  }
	else k+=1;


	for(i=0; i<n; i++) 
	  { 
	    int j;	  
	    for(j=0; j<n; j++)  buff[i] = tolower(buff[i]);
	    if ((buff[i] == '.') || (buff[i] == '!') || (buff[i] == '?')) 
	      {
		flag = 1;
		buff[i] = '\0';
	      }
	    else flag = 0;
	    
	    if ((buff[i] == ',') || (buff[i] == ';') || (buff[i] == '\n') || (buff[i] == '\r'))
	      {
	 	buff[i] = '\0'; 
	      } 

	  }
	strcpy(item1.word, buff);
	node = search_node(&root, item1);
	if(*node != NULL) continue; //Xu li tu trong stopw.txt
	if(isdigit(buff[0]) != 0) continue; //Xu li so

	strcpy(item.word, buff);
	//	if(feof(f_in)) break;
	if (feof(f_in)) break;
	node = search_node(&t, item);
	if(*node != NULL) {
	  ((*node)->count)++;
	  //Add to Tree
	}
	else {
	  insert_node(&t, item);
	  ((*node)->lines[(*node)->nlines]) = 0;
	  ((*node)->nlines) = 0;
	}

      }

    rewind(f_in);
    while (1)
      {
	if (feof(f_in))
	  break;
	//fscanf(f_in, "%*[^\n]%*c", buff);
	fgets(buff, 100, f_in);
	buff[strlen(buff) - 1] = '\0';
	h+=1;
	if(buff==NULL) continue;

	char *pch;
	pch = strtok(buff, ",.;?!- 0123456789");
	while(pch != NULL)
	  {
	    for(i=0; i<strlen(pch); i++)  pch[i] = tolower(pch[i]);
	    strcpy(item.word, pch);
	    node = search_node(&t, item);
	    if(*node != NULL)
	      {
		if ((*node)->lines[(*node)->nlines - 1] != h) {
		  ((*node)->lines[(*node)->nlines]) = h;
		  ((*node)->nlines)++;
		}
	      }
	    pch = strtok(NULL, ",.;?!- 0123456789");
	  }
      }



    inorderprint(t);
    printf("\n");
    fclose(f_in);
    return 0;


}