private static void _MatchRecursive(Node node, ISet<string> rtn, string letters, string prefix, int? maxMatches) { if (maxMatches != null && rtn.Count == maxMatches) return; if (node == null) { if (!rtn.Contains(letters)) rtn.Add(letters); return; } letters += node.Letter.ToString(); if (prefix.Length > 0) { if (node.ContainsKey(prefix[0])) { _MatchRecursive(node[prefix[0]], rtn, letters, prefix.Remove(0, 1), maxMatches); } } else { foreach (char key in node.Keys) { _MatchRecursive(node[key], rtn, letters, prefix, maxMatches); } } }
//Find a node given the key("Jo") public static bool Find(Node node, string key) { //Is key empty if (string.IsNullOrEmpty(key)) return true;//terminal Node //get the first character string first = key.Substring(0, 1); //get the tail: key - first character string tail = key.Substring(1); Node curNode = node.Children.GetNodeByKey(first); //loop until you locate the key i.e. "Jo" if (curNode != null) { return Find(curNode, tail); } else { //not found, return false return false; } }
//Locates a node based on the key; eg. Locates "Jo" node on the tree public static Node GetChildNode(Node node, string key) { //Is key empty if (string.IsNullOrEmpty(key)) return node;//terminal Node? //get the first character string first = key.Substring(0, 1); //get the tail: key - first character string tail = key.Substring(1); //current node Node curNode = node.Children.GetNodeByKey(first); //loop until you locate the key i.e. "Jo" if (curNode != null) { return GetChildNode(curNode, tail); } else { //not found, return null return null; } }
//Adds a node into the children list public void Add(Node node) { //cannot readd a same node if (_owner.IsParentOrChild(node)) throw new InvalidOperationException("Cannot add an ancestor or descendant."); _children.Add(node); node.Parent = _owner; }
private void InnerAddWord(string wordSubString, Node element) { if (string.IsNullOrEmpty(wordSubString) || string.IsNullOrWhiteSpace(wordSubString)) { element.Occurances++; return; } var currChar = wordSubString[0].ToString(); if (!element.Children.ContainsKey(currChar)) { element.Children.Add(currChar, new Node(currChar) { Parent = element }); } this.InnerAddWord(wordSubString.Substring(1), element.Children[currChar]); }
public static void Insert(Node root, string word) { if (String.IsNullOrWhiteSpace(word)) return; int index; var crawler = root; for (int level = 0; level < word.Length; level++) { index = char_to_index(word[level]); if (crawler.CharList[index] == null) crawler.CharList[index] = new Node(); crawler = crawler.CharList[index]; } crawler.IsLeaf = true; }
public static bool Search(Node root, string word) { if (string.IsNullOrWhiteSpace(word)) return false; int index; var crawler = root; for (int i = 0; i < word.Length; i++) { index = char_to_index(word[i]); if (crawler.CharList[index] == null) return false; crawler = crawler.CharList[index]; } return (crawler != null && crawler.IsLeaf); }
private bool InnerTryFindWord(string wordSubString, out int occurances, Node element) { if (string.IsNullOrEmpty(wordSubString) || string.IsNullOrWhiteSpace(wordSubString)) { occurances = element.Occurances; return true; } var currChar = wordSubString[0].ToString(); if (!element.Children.ContainsKey(currChar)) { occurances = 0; return false; } return this.InnerTryFindWord(wordSubString.Substring(1), out occurances, element.Children[currChar]); }
/* Gets all the childern starting from a string ex: returns all names starting from Jo -> John, Joe, Joseph etc*/ public static void GetChildrenStartingFromKey(Node node, string key) { //Get the node which has the value of "Jo" Node curNode = GetChildNode(node, key); //if "Jo" found: if (curNode != null) { //get the children of "Jo" -> John, Joe, Joseph etc IEnumerator iterator = curNode.GetDepthNodes(); while (iterator.MoveNext()) { //get child Node childNode = (Node)iterator.Current; //Does child have a valid full name if (childNode.IsTerminal) Console.WriteLine(childNode.Value); } } }
public Trie() { this.root = new Node(""); }
//Removes a node public void Remove(Node node) { _children.Remove(node); node.Parent = null; }
public TrieFilter() { RootNode = new Node { Letter = Node.Root }; }
static void Main(string[] args) { //Root Node Node root = new Node("", ""); //Loads Names TextReader reader; string textLine; try { reader = new StreamReader(@"..\..\names.txt"); while ((textLine = reader.ReadLine()) != null) { root = Trie.InsertNode(textLine, root); } reader.Close(); } catch (Exception ex) { Console.WriteLine("Error Opening File: " + ex.Message); } Console.WriteLine("Enter Option"); Console.WriteLine("-------------------------------------"); Console.WriteLine("1. DFS on Trie"); Console.WriteLine("2. BFS on Trie"); Console.WriteLine("3. Find a Name on the Directory"); Console.WriteLine("4. Directory Lookup"); Console.WriteLine("5. Exit"); Console.WriteLine("--------------------------------------"); Console.Write("Enter Option:"); int option = int.Parse(Console.ReadLine()); switch (option) { case 1: IEnumerator iterator = root.GetDepthNodes(); while (iterator.MoveNext()) { Node n = (Node)iterator.Current; Console.WriteLine(n.Value); } break; case 2: IEnumerator iterator2 = root.GetBreadthNodes(); while (iterator2.MoveNext()) { Node n = (Node)iterator2.Current; Console.WriteLine(n.Value); } break; case 3: Console.Write("Enter Name to Find(Case Sensitive):"); string name = Console.ReadLine(); bool fnd = Trie.Find(root, name); if (fnd) Console.WriteLine("Found"); else Console.WriteLine("Not Found"); break; case 4: Console.Write("Enter Name to Lookup(Case Sensitive):"); string name2 = Console.ReadLine(); Trie.GetChildrenStartingFromKey(root, name2); break; } Console.WriteLine("--------------------------------------"); Console.WriteLine("Press any key to exit!"); Console.ReadLine(); }
internal NodeCollection(Node owner) { if (null == owner) throw new ArgumentNullException("Owner"); _owner = owner; }
//determines if this node shares hierarchy with given node public bool IsParentOrChild(Node node) { if (node == this) return true; if (this.IsParentOfNode(node)) return true; if (this.IsChildOfNode(node)) return true; return false; }
//Determins if this node is ancestor of the given node public bool IsParentOfNode(Node node) { if (_children.Contains(node)) return true; foreach (Node childNode in _children) if (childNode.IsParentOfNode(node)) return true; return false; }
//Determins if this node is descendant of the given node public bool IsChildOfNode(Node node) { if (null == _parent) return false; if (node == _parent) return true; return _parent.IsChildOfNode(node); }
//Inserts Names into the Trie data structure public static Node InsertNode(string name, Node root) { //Is name null? if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("Null Key"); //set the index, start inserting characters int index = 1; //key string key; //start with the root node Node currentNode = root; //loop for all charecters in the name while (index <= name.Length) { //get the key character key = name[index - 1].ToString(); //does the node with same key already exist? Node resultNode = currentNode.Children.GetNodeByKey(key); //No, this is a new key if (resultNode == null) { //Add a node Node newNode = new Node(key, name.Substring(0, index)); //If reached the last charaecter, this is a valid full name if (index == name.Length) newNode.IsTerminal = true; //add the node to currentNode(i.e. Root node for the first time) currentNode.Children.Add(newNode); //set as the current node currentNode = newNode; } else { //node already exist, set as tghe current node //and move to the next character in the name currentNode = resultNode; } //move to the next character in the name index++; } //all done, return root node return root; }
//Determins if the children contains a node, test for equality public bool Contains(Node node) { return _children.Contains(node); }