public void MoveTo(SuffixNode node, int distanceFromNode) { CurrentNode = node; Distance = distanceFromNode; }
public SuffixTree() { Root = new SuffixNode(this); }
public void MoveTo(SuffixNode node) { CurrentNode = node; Distance = 0; }
/// <summary> /// Adds a new leaf at the current position (also a new branch, if we are not currently on a node), /// and sets Next to the branch node (the leaf's parent). /// </summary> /// <returns>Whether the branch node already existed</returns> protected bool AddBranch(int k) { var parent = LastNode; if (Distance != 0) { // Adding branch between two nodes var next = CurrentNode; // split edge & add branch node // (next also describes the edge between parent and next) var branchNode = new SuffixNode(parent, next.From, next.From + Distance - 1); parent.Children.Remove(next); // HashSet Add/Remove operations are constant parent.Children.Add(branchNode); branchNode.Children.Add(next); next.Parent = branchNode; next.From += Distance; // branchNode becomes parent of the new leaf parent = branchNode; } // else Add branch to existing node // add new leaf var leaf = new SuffixNode(parent, k); parent.Children.Add(leaf); #if DEBUG Console.WriteLine(" Added leaf {0} {1}: {2}", leaf, Distance == 0 ? "to existing branch" : "to new branch", parent); #endif CurrentNode = parent; return Distance == 0; }
/// <summary> /// Adds a new link /// </summary> private void AddLink(SuffixNode startNode, SuffixNode linkedNode) { #if DEBUG Console.WriteLine(" Added link from {0} to {1}", startNode, linkedNode); #endif // link is only a simple pointer var newLink = new SuffixLink { Node = linkedNode }; Links.Add(startNode, newLink); }
/// <summary> /// Move to the link that the given branch links to /// </summary> /// <returns>The length of the substring for that link</returns> private void MoveToLink(SuffixNode branch) { SuffixLink link; if (Links.TryGetValue(branch, out link)) { // Move to link and return saved length MoveTo(link.Node); #if DEBUG Console.WriteLine(" Moved along link to: {0}", CurrentNode); #endif } else if (CurrentNode != Tree.Root) { throw new SuffixTreeBuilderException("Unable to build SuffixTree: Internal node did not have a link: " + CurrentNode); } }
internal SuffixNode(SuffixNode parent, int from, int to) : this(parent.Tree, from, to) { Parent = parent; Parent.Children.Add(this); }
/// <summary> /// Ctor for leaves /// </summary> internal SuffixNode(SuffixNode parent, int from) : this(parent, from, -1) { }