コード例 #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>	Remove a leaf node. </summary>
        /// <remarks>	Darrellp, 2/18/2011. </remarks>
        /// <param name="lfn">	node to remove. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        private void RemoveLeaf(LeafNode lfn)
        {
            // If we're the root, then tree go bye bye...
            if (lfn == NdRoot)
            {
                NdRoot = null;

                return;
            }

            // If we're a child of the root then all we do is change the root to our immediate sibling
            if (lfn.NdParent == NdRoot)
            {
                NdRoot = lfn.ImmediateSibling;

                return;
            }

            // Remove the leaf node and its parent
            //
            // We remove both the leafnode and it's parent (it's parent represents the edge
            // that just terminated in our new fortune vertex, hence it's need to be removed
            // also).  Our immediate sibling
            // is moved up to be a child of the grandparent.  This changes the height
            // balance on the grandparent since it loses a level.
            var innParent          = lfn.NdParent;
            var innGrandparent     = innParent.NdParent;
            var fIsParentLeftChild = innParent.IsLeftChild;

            // Remove our parent
            innParent.SnipFromParent();

            // Insert our sibling in place of our parent

            // Was our parent the left child of our grandparent?
            if (fIsParentLeftChild)
            {
                // Move sibling to be the left child of our grandparent
                innGrandparent.LeftChild = lfn.ImmediateSibling;
                innGrandparent.DecDht();
            }
            else
            {
                // Move sibling to be the right child of our grandparent
                innGrandparent.RightChild = lfn.ImmediateSibling;
                innGrandparent.IncDht();
            }

            // Link our former siblings together
            //
            // Now that we've been removed, our former siblings become direct siblings so link them together
            // in the adjacent leaf chain
            lfn.LinkSiblingsTogether();
        }