public TrieNodePtr(TrieNodePtr parent, TrieNode childNode, int newIndex) : this(childNode) { leftChangeIndex = parent.numChanges == 0 ? newIndex : parent.leftChangeIndex; rightChangeIndex = newIndex; numChanges = parent.numChanges + 1; }
public TrieNodePtr(TrieNode childNode) { node = childNode; leftChangeIndex = -1; rightChangeIndex = -1; numChanges = 0; }
public TrieNodePtr(TrieNodePtr parent, TrieNode childNode) : this(childNode) { leftChangeIndex = parent.leftChangeIndex; rightChangeIndex = parent.rightChangeIndex; numChanges = parent.numChanges; }
private static int TraversalTrie(TrieNode StartNode, int Sum, string name) { name += (char)(StartNode.value + 64); Sum += StartNode.value; StartNode.count--; if (!StartNode.hasChild) { System.Console.WriteLine(name); return Sum * (++GlobalCount); } int count = 0; for (int i = 0; i < 26; i++ ) { count += StartNode.child[i].count; } if (count <= StartNode.count) { System.Console.WriteLine(name); return Sum * (++GlobalCount); } else { for (int i = 0; i < 26; i++) { if (StartNode.child[i].count != 0) return TraversalTrie(StartNode.child[i], Sum, name); } System.Console.WriteLine(name); return Sum * (++GlobalCount); } }
/// <summary> /// Create a new TrieNode instance. /// </summary> /// <param name="character">The character for the TrieNode.</param> /// <param name="children">Children of TrieNode.</param> /// <param name="isWord">If root TrieNode to this TrieNode is a word.</param> /// <param name="wordCount"></param> internal TrieNode(char character, IDictionary<char, TrieNode> children, int wordCount, TrieNode parent) { Character = character; Children = children; WordCount = wordCount; Parent = parent; }
private TrieNode Insert(char c, TrieNode node) { if (node.Contains(c)) return node.GetChild(c); else { int n = Convert.ToByte(c) - TrieNode.ASCIIA; TrieNode t = new TrieNode(); node.nodes[n] = t; return t; } }
private TrieNode Contains(char c, TrieNode node) { if (node.Contains(c)) { return node.GetChild(c); } else { return null; } }
private static void GetWords(List<TrieNode> words) { var fileName = @"..\..\words.txt"; string line; using (StreamReader reader = new StreamReader(fileName)) { while ((line = reader.ReadLine()) != null) { TrieNode node = new TrieNode(null, '?'); node.AddWord(line); words.Add(node); } } }
private void TrieTraversal(TrieNode current, int sum, int count) { if (current.value == -1) tempsum = 0; if (current.count != 0) { tempsum += current.value; //System.Console.WriteLine("level " + depth + ": value = " + current.value + " count = " + current.count); } if (current.hasChild) { for (int i = 0; i < 26; i++) { if (current.child[i] != null) TrieTraversal(current.child[i], tempsum, count); } } else { sum += tempsum * count; tempsum = 0; count++; return; } }
public static void Main() { /* Trie implementation of trie, used in this problem was taken from http://stackoverflow.com/questions/12190326/parsing-one-terabyte-of-text-and-efficiently-counting-the-number-of-occurrences with only minor modifications. */ Console.WriteLine("Processing text file..."); DateTime startAt = DateTime.Now; TrieNode root = new TrieNode(null, '?'); var path = "../../1000words.txt"; DataReader newReader = new DataReader(path, ref root); newReader.ThreadRun(); int distinctWordCount = 0; int totalWordCount = 0; root.GetTopCounts(root, ref distinctWordCount, ref totalWordCount); string[] words = new string[] { "Lorem", "lorem", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "line", "a" }; // The loop simulates 10000 word searches in the trie for (int i = 0; i < 1000; i++) { foreach (var word in words) { Console.WriteLine("Word: {0}, count {1}", word, root.GetCount(word) > -1 ? root.GetCount(word) : 0); } Console.WriteLine(); } Console.WriteLine("Number of distinct words in the set: {0}", distinctWordCount); DateTime stopAt = DateTime.Now; Console.WriteLine("Input data processed in {0} secs", new TimeSpan(stopAt.Ticks - startAt.Ticks).TotalSeconds); }
public static void Main(string[] args) { string path = @"..\..\text.txt"; Console.WriteLine("Counting words..."); // read the text DateTime startTime = DateTime.Now; TrieNode root = new TrieNode(null, '?'); DataParser newReader = new DataParser(path, ref root); newReader.ParseData(); DateTime stopTime = DateTime.Now; Console.WriteLine("Input data processed in {0} secs.", new TimeSpan(stopTime.Ticks - startTime.Ticks).TotalSeconds); Console.WriteLine(); Console.WriteLine("List of found words:"); // read the words to be searched List<TrieNode> wordsToSearch = new List<TrieNode>(); GetWords(wordsToSearch); int distinctWordCount = 0; int totalWordCount = 0; root.GetWordCount(ref wordsToSearch, ref distinctWordCount, ref totalWordCount); wordsToSearch.Reverse(); foreach (TrieNode node in wordsToSearch) { Console.WriteLine("{0} - {1} times", node.ToString(), node.WordCount); } Console.WriteLine(); Console.WriteLine("{0} words counted", totalWordCount); Console.WriteLine("{0} distinct words found", distinctWordCount); Console.WriteLine(); Console.WriteLine("done."); }
// Driver public void Go() { // Input keys (use only 'a' // through 'z' and lower case) string rline; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader("words.txt"); List <String> keys = new List <string>(); while ((rline = file.ReadLine()) != null) { keys.Add(rline); } //using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"C:\Users\Brett\Desktop\dict.txt")) //{ // while ((rline = file.ReadLine()) != null) // { // keys.Add(rline); // if (!rline.Contains("&")) // { // file2.WriteLine(rline.ToLower()); // } // } //} root = new TrieNode(); // Construct trie int i; for (i = 0; i < keys.Count; i++) { insert(keys[i]); } //keys = null; string line = ""; found = new Dictionary <string, int>(); do { //read a complete line char c = Console.ReadKey().KeyChar; found.Clear(); //check if line is empty or not if (c == '\r') { addWeight(line); Console.Clear(); Console.WriteLine("Searching: "); line = ""; } else if (Char.IsLetter(c)) { line += c.ToString(); searchAll(line); var test = found; Console.Clear(); Console.WriteLine("Searching: " + line); Console.WriteLine(found.Count + " found"); int count = 0; foreach (var item in found.OrderByDescending(x => x.Value)) { if (count == 20) { break; } string ln = item.Key; if (item.Value > 0) { ln += " (suggested)"; } Console.WriteLine(ln); count++; } //Console.Write("Line was = " + line); } } while (line != null); }
internal void SetChild(TrieNode child) { Children[child.Character] = child; }
internal void RemoveNode(TrieNode rootTrieNode) { // set this node's word count to 0 WordCount = 0; RemoveNode_Recursive(rootTrieNode); }
private void addWord(string word) { TrieNode node = this; //Start at root Preconditions.checkState(node.CurrentLength == 0); for (int cIdx = 0; cIdx < word.Length; ++cIdx) { char c = word[cIdx]; int cInt = (int)c - aInt; if (node.children[cInt] == null) { TrieNode newNode = new TrieNode(); newNode.CurrentLength = cIdx + 1; node.children[cInt] = newNode; node.childrenList.Add(new Tuple<int, TrieNode>(cInt, newNode)); } node = node.children[cInt]; } Preconditions.checkState(node.WordMatch == null); node.WordMatch = word; }
/// <summary> /// Recursive method to remove word. Remove only if node does not /// have children and is not a word node and has a parent node. /// </summary> private void RemoveNode_Recursive(TrieNode rootTrieNode) { if (Children.Count == 0 // should not have any children && !IsWord // should not be a word && this != rootTrieNode // do not remove root node ) { var parent = Parent; Parent.RemoveChild(Character); Parent = null; parent.RemoveNode_Recursive(rootTrieNode); } }
/** Initialize your data structure here. */ public Trie() { this.root = new TrieNode(); }
public void AddChild() { this.child = new TrieNode[26]; this.hasChild = true; for (int i = 0; i < 26; i++) { child[i] = new TrieNode(); } }
/// <summary> /// Create a new Trie instance. /// </summary> /// <param name="rootTrieNode">The root TrieNode.</param> internal Trie(TrieNode rootTrieNode) { this.rootTrieNode = rootTrieNode; }
/// <summary> /// Recursive method to add word. Gets the first char of the word, /// creates the child TrieNode if null, and recurses with the first /// char removed from the word. If the word length is 0, return. /// </summary> private void AddWord(TrieNode trieNode, char[] word) { if (word.Length == 0) { trieNode.WordCount++; return; } var c = Utilities.FirstChar(word); TrieNode child = trieNode.GetChild(c); if (child == null) { child = TrieFactory.CreateTrieNode(c, trieNode); trieNode.SetChild(child); } var cRemoved = Utilities.FirstCharRemoved(word); AddWord(child, cRemoved); }
public Trie() { Head = new TrieNode(); Head.value = -1; }
/// <summary> /// Recursive method to get longest words starting from given TrieNode. /// </summary> private void GetLongestWords(TrieNode trieNode, ICollection<string> longestWords, StringBuilder buffer, ref int length) { if (trieNode.IsWord) { if (buffer.Length > length) { longestWords.Clear(); length = buffer.Length; } if (buffer.Length >= length) { longestWords.Add(buffer.ToString()); } } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); GetLongestWords(child, longestWords, buffer, ref length); // Remove recent character buffer.Length--; } }
public DataParser(string path, ref TrieNode root) { this.root = root; this.path = path; }
/// <summary> /// Recursive method to get all the words starting from given TrieNode. /// </summary> private void GetWords(TrieNode trieNode, ICollection<string> words, StringBuilder buffer) { if (trieNode.IsWord) { words.Add(buffer.ToString()); } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); GetWords(child, words, buffer); // Remove recent character buffer.Length--; } }
public DataReader(string path, ref TrieNode root) { this.trie = root; this.path = path; }
public static TrieNode createRootNode(Dictionary dict) { TrieNode root = new TrieNode(); root.CurrentLength = 0; for (int dIdx = 0; dIdx < dict.words.Count; ++dIdx) { root.addWord(dict.words[dIdx]); } return root; }
public Trie() { root = new TrieNode(); }