TernarySearchTrie如果加载的词典是平衡的,将大幅缩短检索路径
这里给出一个c#的实现的生成平衡索引的代码
public class BinaryIndexList
{
private static List<int> source = null;
private static List<int> result = null;
public static List<int> CreateIndex(int length)
{
result = new List<int>();
source=Enumerable.Range(0, length).ToList();
Iterate(source);
return result;
}
private static void Iterate(List<int> block)
{
int len = block.Count();
int mid = len / 2;
int val = block[mid];
result.Add(val);
int count_left = mid - 0;
if (count_left > 0)
{
List<int> block_left = block.Take(count_left).ToList();
Iterate(block_left);
}
int count_right = len - mid - 1;
if (count_right > 0)
{
List<int> block_right = block.Skip(mid + 1).ToList();
Iterate(block_right);
}
}
}