예제 #1
0
파일: trie.cpp 프로젝트: dushujun/ACM_SDNU
void insert(const char chars[], int len) {
  struct Node* ptr = ROOT;
  int i;
  for(i = 0; i < len; i++) {
   if(ptr->child[charToindex(chars[i])] == NULL) {
    ptr->child[charToindex(chars[i])] = createNewNode(chars[i]);
  }
  ptr = ptr->child[charToindex(chars[i])];
}
  ptr->type = COMPLETED;
}
예제 #2
0
파일: trie.cpp 프로젝트: dushujun/ACM_SDNU
int find(const char chars[], int len) {
  struct Node* ptr = ROOT;
  int i = 0;
  while(i < len) {
   if(ptr->child[charToindex(chars[i])] == NULL) {
   break;
  }
  ptr = ptr->child[charToindex(chars[i])];
  i++;
  }
  return (i == len) && (ptr->type == COMPLETED);
}
예제 #3
0
//Insert Node to Trie
int insert(const char chars[],int len,int lineNum)
{
	struct Node* ptr = ROOT;
	int i = 0;
	
	while(i < len)
	{
		int tempidx = charToindex(chars[i]);
		
		if(ptr->child[tempidx] == NULL)  //child doesn't exist
		{
			if(ptr->type == LEAF)  // at leaf node
			{
				ptr->type = COMPOSITE;
			}
			
			if(i < (len - 1)) //intermediate node
			{
				ptr->child[tempidx] = createNewNode(chars[i]);
				ptr->child[tempidx]->type = UNCOMPLETED;
				ptr->child[tempidx]->level = ptr->level + 1;
			}
			if(i == (len - 1)) //leaf node
			{
				ptr->child[tempidx] = createNewNode(chars[i]);
				ptr->child[tempidx]->type = LEAF;
				ptr->child[tempidx]->lineNum = lineNum;
				ptr->child[tempidx]->level = ptr->level + 1;
				ptr->child[tempidx]->content = chars;
			}
			 ptr = ptr->child[tempidx];
		}
		
		else if(ptr->child[tempidx] !=  NULL) // the node already existed
		{
			if(i == (len - 1))
			{
				ptr->child[tempidx]->type = COMPOSITE;
				ptr->child[tempidx]->lineNum = lineNum;
				ptr->child[tempidx]->content = chars;
			}
			 ptr = ptr->child[tempidx];	
		}
		i++;
	}
	return 0;
}