private void CutLink(PatriciaTree.Node node) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (!(node is PatriciaTree.Leaf)) { throw new Exception(); } if (node == this._root) { this._root = (PatriciaTree.Node)null; } else { PatriciaTree.Node node1 = node == node.Parent.Left ? node.Parent.Right : node.Parent.Left; if (node.Parent == this._root) { node1.Parent = (PatriciaTree.Node)null; this._root = node1; } else if (node.Parent == node.Parent.Parent.Left) { node.Parent.Parent.Left = node1; } else { node.Parent.Parent.Right = node1; } } }
private void Branch(PatriciaTree.Node node, PatriciaTree.Node newNode, int bit) { if (PatriciaTree.BitHelper.GetBit(node.Key, bit) == PatriciaTree.BitHelper.GetBit(newNode.Key, bit)) { throw new ArgumentException("node or newNode"); } PatriciaTree.Node node1 = (PatriciaTree.Node) new PatriciaTree.BranchNode(this._nodes.Count, this.GetBranchKey(node, newNode, bit), bit); this._nodes.Add((PatriciaTree.INode)node1); if (node.Parent != null) { if (node.Parent.Left == node) { node.Parent.Left = node1; } else { node.Parent.Right = node1; } } else { this._root = node1; } if (node.Bit > bit && PatriciaTree.BitHelper.GetBit(node.Key, bit)) { node1.Left = newNode; node1.Right = node; } else { node1.Left = node; node1.Right = newNode; } }
public bool Remove(string key) { PatriciaTree.Node node = this.FindNode(key); if (node == null) { return(false); } this.CutLink(node); this._nodes.Remove((PatriciaTree.INode)node); this._items.Remove(key); return(true); }
private PatriciaTree.Node FindNode(PatriciaTree.Node node, string key) { if (node == null) { return((PatriciaTree.Node)null); } if (!(node is PatriciaTree.Leaf)) { return(this.FindNode(PatriciaTree.BitHelper.GetBit(key, node.Bit) ? node.Right : node.Left, key)); } if (!(node.Key == key)) { return((PatriciaTree.Node)null); } return(node); }
private void AddNode(PatriciaTree.Node node, IPatriciaTreeItem item) { if (node == null) { this._root = (PatriciaTree.Node) new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode) this._root); } else { int start = node.Parent == null ? 0 : node.Parent.Bit; if (node is PatriciaTree.BranchNode) { int bit = PatriciaTree.BitHelper.CompareBit(node.Key, item.Key, start, node.Bit); if (0 <= bit) { PatriciaTree.Leaf leaf = new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode)leaf); this.Branch(node, (PatriciaTree.Node)leaf, bit); } else { this.AddNode(PatriciaTree.BitHelper.GetBit(item.Key, node.Bit) ? node.Right : node.Left, item); } } else { if (!(node is PatriciaTree.Leaf)) { throw new Exception("invalid node type."); } int bit = PatriciaTree.BitHelper.CompareBit(node.Key, item.Key, start); if (0 >= bit) { throw new ArgumentException(nameof(item)); } PatriciaTree.Leaf leaf = new PatriciaTree.Leaf(this._nodes.Count, item); this._nodes.Add((PatriciaTree.INode)leaf); this.Branch(node, (PatriciaTree.Node)leaf, bit); } } }
private string GetBranchKey(PatriciaTree.Node node1, PatriciaTree.Node node2, int bit) { return(PatriciaTree.BitHelper.SubString(PatriciaTree.BitHelper.GetBitCount(node1.Key) <= bit ? node2.Key : node1.Key, bit)); }