public void AddChild(DisseminateNode <TKey, TValue> child)
        {
            child.Parent = this;
            ChildrenCount++;

            if (FirstChild == null)
            {
                FirstChild = child;
                return;
            }

            // Always add it as the last child
            FirstChild.InsertBefore(child);
            Debug.Assert(RightSibling != null && LeftSibling != null);
        }
        /// <summary>
        /// Gracefully remove this node from the parent -- connect neighbouring siblings and update the parent.
        /// Children are preserved.
        /// </summary>
        public void CutFromFamily()
        {
            try
            {
                Debug.Assert(LeftSibling != null && RightSibling != null);

                if (LeftSibling == this)
                {
                    Debug.Assert(RightSibling == this);
                }

                if (RightSibling == this)
                {
                    Debug.Assert(LeftSibling == this);
                }

                if (Parent == null)
                {
                    return;
                }

                // Update the parent
                if (Parent.ChildrenCount == 1)
                {
                    // We are the only child
                    Debug.Assert(LeftSibling == RightSibling && RightSibling == this);
                    Parent.FirstChild    = null;
                    Parent.ChildrenCount = 0;
                    return;
                }

                if (Parent.FirstChild == this)
                {
                    Debug.Assert(RightSibling != this); // We already checked that we are not the only child
                    Parent.FirstChild = RightSibling;
                }

                Parent.ChildrenCount--;
            }
            finally
            {
                Parent = null;
                Cut();
            }
        }
 private void Clear()
 {
     Parent        = null;
     FirstChild    = null;
     ChildrenCount = 0;
 }
 private NodeTraversalToken <DisseminateNode <TKey, TValue>, NodeTraversalAction> GetNodeTraversalToken(DisseminateNode <TKey, TValue> node, NodeTraversalAction action)
 {
     return(new NodeTraversalToken <DisseminateNode <TKey, TValue>, NodeTraversalAction>(node, action));
 }