private int CountPrefixRecursively(HybridTrieNode node, char[] key, int position) { if (node == null) { return(0); } if (key[position] < node.Character) { return(CountPrefixRecursively(node.LeftChild, key, position)); } else if (key[position] > node.Character) { return(CountPrefixRecursively(node.RightChild, key, position)); } else { if (position < key.Length - 1) { return(CountPrefixRecursively(node.MiddleChild, key, position + 1)); } else { if (node.MiddleChild == null) { return(1); } else { return(CountWords(node.MiddleChild) + (node.IsFinalNode() ? 1 : 0)); } } } }
private bool SearchRecursively(HybridTrieNode node, char[] key, int position) { if (node == null) { return(false); } if (key[position] < node.Character) { return(SearchRecursively(node.LeftChild, key, position)); } else if (key[position] > node.Character) { return(SearchRecursively(node.RightChild, key, position)); } else { if (position < key.Length - 1) { return(SearchRecursively(node.MiddleChild, key, position + 1)); } else { return(node.IsFinalNode()); } } }
private int CountWordsRecursively(HybridTrieNode node, int wordsCounter) { if (node != null) { if (node.IsFinalNode()) { wordsCounter++; } wordsCounter = CountWordsRecursively(node.LeftChild, wordsCounter); wordsCounter = CountWordsRecursively(node.RightChild, wordsCounter); wordsCounter = CountWordsRecursively(node.MiddleChild, wordsCounter); } return(wordsCounter); }
private int CalculateHeightRecursively(HybridTrieNode node, int heightCounter, int heightResult) { if (node != null) { heightCounter++; heightResult = CalculateHeightRecursively(node.LeftChild, heightCounter, heightResult); if (heightCounter > heightResult && node.IsFinalNode()) { heightResult = heightCounter; } heightResult = CalculateHeightRecursively(node.RightChild, heightCounter, heightResult); heightResult = CalculateHeightRecursively(node.MiddleChild, heightCounter, heightResult); } return(heightResult); }
private void PrintRecursively(HybridTrieNode previousNode, HybridTrieNode nextNode, Color color, GraphPrinter graphPrinter) { if (nextNode != null) { if (previousNode != null) { if (color.Equals(Color.BLUE)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.BLUE); } else if (color.Equals(Color.RED)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.RED); } else if (color.Equals(Color.GREEN)) { graphPrinter.PrintEdge(previousNode, nextNode, Color.GREEN); } if (nextNode.IsFinalNode()) { if (color.Equals(Color.BLUE)) { graphPrinter.PrintNode(nextNode, Color.BLUE, Color.BLUE, Style.DASHED); } else if (color.Equals(Color.RED)) { graphPrinter.PrintNode(nextNode, Color.RED, Color.RED, Style.DASHED); } else if (color.Equals(Color.GREEN)) { graphPrinter.PrintNode(nextNode, Color.GREEN, Color.GREEN, Style.DASHED); } } graphPrinter.PrintNodeLabel(nextNode); } PrintRecursively(nextNode, nextNode.LeftChild, Color.BLUE, graphPrinter); PrintRecursively(nextNode, nextNode.MiddleChild, Color.RED, graphPrinter); PrintRecursively(nextNode, nextNode.RightChild, Color.GREEN, graphPrinter); } }
private List <string> ListWordsRecursively(HybridTrieNode node, String word, List <String> listWords) { if (node != null) { ListWordsRecursively(node.LeftChild, word, listWords); word += node.Character; if (node.IsFinalNode()) { listWords.Add(word); } ListWordsRecursively(node.MiddleChild, word, listWords); word = RemoveLastCaracter(word); ListWordsRecursively(node.RightChild, word, listWords); } return(listWords); }
private bool RemoveRecursively(HybridTrieNode parent, HybridTrieNode node, char[] key, int position) { if (node == null) { return(false); } var log = true; if (key[position] < node.Character) { log = RemoveRecursively(node, node.LeftChild, key, position); } else if (key[position] > node.Character) { log = RemoveRecursively(node, node.RightChild, key, position); } else { // If we are on our last character. if (position == key.Length - 1) { if (node.IsFinalNode()) { // The node is an end node, remove the end value. node.MakeFinalNode(0); } else { // If there is no end key as it does not exist within the tree. return(false); } } else if (position < key.Length + 1) { log = RemoveRecursively(node, node.MiddleChild, key, position + 1); } } // Only remove if the node's middle child is null and if the node is not an end key. // If log is false, the value has never been found. Thus the key does not exist within this tree. if (log && node.MiddleChild == null && !node.IsFinalNode()) { // Case 1: No children, safe to delete. if (!node.HasChildren()) { Transplant(parent, node, null); } // Case 2: Right is null, transplant it to left. else if (node.RightChild == null) { Transplant(parent, node, node.LeftChild); } // Case 3: Left is null, transplant it to right. else if (node.LeftChild == null) { Transplant(parent, node, node.RightChild); } // Case 4: Both left and right children exists, find successor and transplant. else { HybridTrieNode successor = FindSuccessor(node.RightChild); HybridTrieNode successorParent = FindSuccessorParent(successor); // If successor node has left child(ren). if (successorParent != node) { Transplant(parent, node, node.RightChild); } else { Transplant(parent, node, successor); } // Make the node's left child as left child for successor node. successor.LeftChild = node.LeftChild; } node = null; } return(log); }