public static RadixTrieNode AddChild(RadixTrieNode parent, string key, object value) { var childNode = CreateChildNode(key, value); AddChild(parent, childNode); return(childNode); }
//add public void Add(string key, object value) { int index = 0; RadixTrieNode parent = this.root; while (index < key.Length) { string token = key.Substring(index, key.Length - index); //if there are no child add a new node if (parent.Children == null || parent.Children.Count == 0) { RadixNodeUtils.AddChild(parent, key, value); return; } foreach (var child in parent.Children) { int prefixLength = this.GetPrefixLength(token, child.Key); ////if common is 0 then add new token //if (prefixLength == 0) //{ // RadixNodeUtils.AddChild(parent, token, value); // break; //} //if common is same as token length, then make sure node is marked as terminal if (prefixLength == token.Length) { child.Value = value; return; } //see if the key needs to be resized; not always, what if needs to be used if (prefixLength < child.Key.Length) { RadixNodeUtils.SplitParent(child, prefixLength, token, value); return; } } //no matching prefix RadixNodeUtils.AddChild(parent, token, value); } //get longest }
public static void AddChild(RadixTrieNode parent, params RadixTrieNode[] children) { if (parent == null) { throw new ArgumentNullException("parent"); } if (parent.Children == null) { parent.Children = CreateChildCollection(); } foreach (var child in children) { parent.Children.Add(child); } }
public static void SplitParent(RadixTrieNode parent, int prefixLength, string key, object value) { //get the new leaf key string newLeafKey = parent.Key.Substring(prefixLength, parent.Key.Length - prefixLength); //get the new parent key string newParentKey = parent.Key.Substring(0, prefixLength); //get the new child key string newChildKey = key.Substring(prefixLength, key.Length - prefixLength); //create new leaf node RadixTrieNode newLeafNode = CreateChildNode(newLeafKey, parent.Value); //create new leaf node RadixTrieNode newChildNode = CreateChildNode(newChildKey, value); //modify parent node (maybe this should be refactored to use immutable object) parent.Key = newParentKey; parent.Value = null; AddChild(parent, newLeafNode, newChildNode); }