/// <summary> /// Create a node from the given parameters. /// </summary> /// <param name="Parent">Parent node of this node.</param> /// <param name="key">The Key of this node</param> /// <param name="children">This node children</param> /// <param name="suffixes">This node's suffixes</param> public Node(Node parent, Char key, Dictionary<Char, Node> children, List<Word> suffixes) { this.Key = char.ToLower(key); this.Parent = parent; this.children = children; this.suffixes = suffixes; }
/// <summary> /// Create an empty node with no children and no suffixes. /// </summary> /// <param name="Parent">Parent node of this node.</param> /// <param name="key">The Key of this node</param> public Node(Node parent, Char key) { this.Key = char.ToLower(key); this.Parent = parent; this.children = new Dictionary<Char, Node>(); this.suffixes = new List<Word>(); }
/// <summary> /// Remove the last character of the currently entered prefix. /// </summary> public void BackMatch() { if (current != root) { current = current.Parent; Prefix = Prefix.Substring(0, Prefix.Length - 1); } }
public PrefixMatcher(Node root) { this.root = this.current = root; this.Prefix = ""; }
/// <summary> /// Clear the currently entered prefix. /// </summary> public void ResetMatch() { this.current = this.root; this.Prefix = ""; }
/// <summary> /// advance the Matcher to next match /// </summary> /// <param name="next">the next key to find</param> /// <returns>True if the current node has the key, False otherwise</returns> public bool NextMatch(char next) { if (current.hasKey(next)) { current = current.GetChild(next); Prefix += next; return true; } return false; }
private void removeWord(Node node, Word word) { if (word == null || node == null) return; char key; Node runner = node; if (runner.DecrementSuffix(word)) { while (runner != root) { key = runner.Key; runner.RemoveSuffix(word); runner = runner.Parent; } } Matcher.ResetMatch(); }
/// <summary> /// Add a child node associated with a Key to this node and return the node. /// </summary> /// <param name="Key">Key to associated with the child node.</param> /// <returns>If given Key already exists then return the existing child node, else return the new child node.</returns> public Node addChild(char key) { key = char.ToLower(key); if (children.ContainsKey(key)) return children[key]; else { var newChild = new Node(this, key); children.Add(key, newChild); return newChild; } }