/// <summary> /// 创建关键字树 /// </summary> /// <param name="path"></param> private static void CreateTree(string path) { if (rootNode != null) { return; } lock (lockObject) { if (rootNode == null) { rootNode = new Node('R'); using (StreamReader reader = new StreamReader(path)) { while (!reader.EndOfStream) { string key = reader.ReadLine(); if (!string.IsNullOrEmpty(key)) { char[] chars = key.ToCharArray(); rootNode.Insert(chars, 0); } } } } } }
/// <summary> /// /// </summary> /// <param name="chars"></param> /// <param name="p"></param> internal void Insert(char[] chars, int index) { Node node = FindNode(chars[index]); if (node == null) { node = new Node(chars[index]); nodes.Add(node); } if (index == (chars.Length - 1)) { node.flag = 1; } index++; { if (index < chars.Length) { Insert(chars, index); } } }