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); }