public void Update(Func <TValue, TValue> transformer) { if (transformer == null) { throw new ArgumentNullException(nameof(transformer)); } bool removedItem; TValue newValue; if (Key == null) { // If the Key is null then it means that this node is part of a chain but doesn't have its own Key and Value, in which case leave // Value as whatever it currently is on the cloned node (should be default(T)) newValue = Value; removedItem = false; // This node doesn't represent a value and so we haven't removed anything } else { newValue = transformer(Value); removedItem = (newValue == null); } Key = removedItem ? null : Key; Value = newValue; LeftChild?.Update(transformer); MiddleChild?.Update(transformer); RightChild?.Update(transformer); }
public Node Clone() { var newNode = new Node() { Character = Character, LeftChild = LeftChild?.Clone(), MiddleChild = MiddleChild?.Clone(), RightChild = RightChild?.Clone(), Parent = Parent, Key = Key, Value = Value }; if (newNode.LeftChild != null) { newNode.LeftChild.Parent = newNode; } if (newNode.MiddleChild != null) { newNode.MiddleChild.Parent = newNode; } if (newNode.RightChild != null) { newNode.RightChild.Parent = newNode; } return(newNode); }
/// <summary> /// Returns a cloned copy of the node, deep-cloning all data. This will throw an exception for a null node. /// </summary> public Node Clone() { // To clone it, we copy Character, Parent, Key and Value onto a new instance and then clone the child nodes (if non-null). After this the // Parents on the child nodes will have to be set to the new instance so that a new chain can be formed. This should be called against // the root node such that an entirely new tree is formed. var newNode = new Node() { Character = Character, LeftChild = (LeftChild == null) ? null : LeftChild.Clone(), MiddleChild = (MiddleChild == null) ? null : MiddleChild.Clone(), RightChild = (RightChild == null) ? null : RightChild.Clone(), Parent = Parent, Key = Key, Value = Value }; if (newNode.LeftChild != null) { newNode.LeftChild.Parent = newNode; } if (newNode.MiddleChild != null) { newNode.MiddleChild.Parent = newNode; } if (newNode.RightChild != null) { newNode.RightChild.Parent = newNode; } return(newNode); }
/// <summary> /// Does the specified Node or any descendant have a true IsKey value? This will throw an exception for a null node. /// </summary> public bool ContainsAnyValues() { return( (Key != null) || ((LeftChild != null) && LeftChild.ContainsAnyValues()) || ((MiddleChild != null) && MiddleChild.ContainsAnyValues()) || ((RightChild != null) && RightChild.ContainsAnyValues()) ); }