public void PreOrderTraverse(Node _root, StringBuilder sb) { if (_root != null) { sb.Append(_root.Key + "%20"); for (int i = 0; i < _root.Paths.Count; i++) { FoundPath path = _root.Paths[i]; sb.Append(path.Path + ">"); for (int j = 0; j < path.Indexes.Count; j++) { sb.Append(path.Indexes[j]); if ((j + 1) != path.Indexes.Count) { sb.Append("*"); } } if ((i + 1) != _root.Paths.Count) { sb.Append("|"); } } sb.AppendLine(); PreOrderTraverse(_root.LeftNode, sb); PreOrderTraverse(_root.RightNode, sb); } }
private void gvPaths_SelectionChanged(object sender, EventArgs e) { if (browsePaths) { StringBuilder sb = new StringBuilder(); FileStream fs = new FileStream(gvPaths.SelectedRows[0].Cells[0].Value.ToString(), FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string line = ""; while ((line = sr.ReadLine()) != null) { sb.AppendLine(line); } sr.Close(); fs.Dispose(); txtFileContent.Text = sb.ToString(); FoundPath fp = gvPaths.SelectedRows[0].DataBoundItem as FoundPath; foreach (int index in fp.Indexes) { txtFileContent.Select(index == 0 ? index + 1 : index, txtSearch.Text.Length); txtFileContent.SelectionBackColor = Color.Orange; } } }
public void Insert(string keyword, string path, int index) { if (root == null) { root = new Node(keyword); FoundPath newFoundPath = new FoundPath(path); newFoundPath.Indexes.Add(index); root.Paths.Add(newFoundPath); } else { Node ptr = root; while (true) { if (ptr.Key.Equals(keyword, StringComparison.OrdinalIgnoreCase)) { bool path_found = false; foreach (FoundPath fp in ptr.Paths) { if (fp.Path.Equals(path, StringComparison.OrdinalIgnoreCase)) { fp.Indexes.Add(index); path_found = true; break; } } if (!path_found) { FoundPath newFoundPath = new FoundPath(path); newFoundPath.Indexes.Add(index); ptr.Paths.Add(newFoundPath); } break; } else if (keyword.CompareTo(ptr.Key) < 0) { if (ptr.LeftNode != null) { ptr = ptr.LeftNode; } else { ptr.LeftNode = new Node(keyword); FoundPath newFoundPath = new FoundPath(path); newFoundPath.Indexes.Add(index); ptr.LeftNode.Paths.Add(newFoundPath); break; } } else { if (ptr.RightNode != null) { ptr = ptr.RightNode; } else { ptr.RightNode = new Node(keyword); FoundPath newFoundPath = new FoundPath(path); newFoundPath.Indexes.Add(index); ptr.RightNode.Paths.Add(newFoundPath); break; } } } } }