字典树

#if 0
//trie tree solution
#define SEARCHWORD_MAXLEN 12
#define PREFIX_MAXLEN 8

int mstrcmp(const char *a, const char *b)
{
	int i;
	for (i = 0; a[i] != '\0'; i++)
	{
		if (a[i] != b[i])
			return a[i] - b[i];
	}
	return a[i] - b[i];
}

int mstrncmp(const char *a, const char *b, int len)
{
	for (int i = 0; i < len; i++)
	{
		if (a[i] != b[i])
		return a[i] - b[i];
	}
	return 0;
}

int mstrlen(const char *a)
{
	int len = 0;
	while (a[len] != '\0')
		len++;
	return len;
}

void mstrcpy(char *dest, const char *src)
{
	int i = 0;
	while (src[i] != '\0')
	{
		dest[i] = src[i];
		i++;
	}
	dest[i] = src[i];
}

void mstrncpy(char *dest, const char *src, int len)
{
	for (int i = 0; i<len; i++)
	{
		dest[i] = src[i];
	}
	dest[len] = '\0';
}

#define MAXN 26
struct node
{
	int freq;
	char str[13];
	node *next[MAXN];
};

node *head;

int result_cnt = 0;
node result[100];

void init()
{
	head = new node();
	for (int i = 0; i < MAXN; ++i)
		head->next[i] = 0;
	head->freq = 0;
}

void search(char searchWord[SEARCHWORD_MAXLEN + 1])
{
	int len = mstrlen(searchWord);

	node* t = head;
	node* p = head;
	for (int i = 0; i < len; ++i)
	{
		int c = searchWord[i] - 'a';
		if (p->next[c] == 0)
		{
			t = new node();
			t->freq = 0;
			p->next[c] = t;
		}
		p = p->next[c];

		if (i == len - 1)
		{
			if (p->freq == 0)
				mstrcpy(p->str, searchWord);
			p->freq++;
		}
	}
}

node get_max_value()
{
	int i = 0;
	int j = 0;
	node max;
	max = result[0];
	int cnt = 0;
	for (i = 1; i < result_cnt; i++)
	{
		if (result[i].freq > max.freq)
		{
			max = result[i];
			cnt = i;
		}
		else if (result[i].freq == max.freq)
		{
			int len1 = mstrlen(result[i].str);
			int len2 = mstrlen(max.str);

			int len = len1;
			if (len > len2)
				len = len2;

			bool is_equal = true;
			for (j = 0; j < len; j++)
			{
				if (result[i].str[j] != max.str[j])
				{
					if (result[i].str[j] < max.str[j])
					{
						max = result[i];
						cnt = i;
					}
					is_equal = false;
					break;
				}
			}

			if (is_equal == true)
			{
				if (len2 > len1)
				{
					max = result[i];
					cnt = i;
				}
			}

		}
	}
	result[cnt].freq = 0;
	result[cnt].str[0] = '\0';

	return max;
}


void traverse_trie_tree(node* p)
{
	if (p == 0)
		return;

	for (int i = 0; i<26; i++)
	{
		if (p->next[i] > 0)
		{
			if (p->next[i]->freq > 0)
			{
				mstrcpy(result[result_cnt].str, p->next[i]->str);
				result[result_cnt].freq += p->next[i]->freq;
				result_cnt++;
			}
			traverse_trie_tree(p->next[i]);
		}
	}
}

int autoComplete(char prefix[PREFIX_MAXLEN + 1], char retWords[5][SEARCHWORD_MAXLEN + 1])
{
	node *p = head;

	result_cnt = 0;

	for (int i = 0; i < 100; i++)
	{
		result[i].freq = 0;
		result[i].str[0] = '\0';
	}

	int len = mstrlen(prefix);
	for (int i = 0; i < len; ++i)
	{
		int c = prefix[i] - 'a';
		if (p->next[c] == 0)
		{
			result_cnt = 0;
			return result_cnt;
		}
		p = p->next[c];
	}

	if (p->freq != 0)
	{
		//找到与自己一样的字符串个数
		mstrcpy(result[result_cnt].str, p->str);
		result[result_cnt].freq += p->freq;
		result_cnt++;
	}

	//遍历剩下节点
	traverse_trie_tree(p);

	for (int i = 0; i < result_cnt && i < 5; i++)
		mstrcpy(retWords[i], get_max_value().str);

	if (result_cnt > 5)
		result_cnt = 5;
	return result_cnt;
}

#endif
posted @ 2019-04-13 15:26  调皮的贝叶斯  阅读(82)  评论(0)    收藏  举报