Пример #1
0
        private void RemoveChild(LeafNode toBeRemoved)
        {
            if( toBeRemoved == null ) return; 
            
            if( toBeRemoved == GetFirstChild() )
            {
                child = toBeRemoved.GetRightSibling();
                if( child != null )
                {
                    child.SetLeftSibling( null );
                }
            }
            else
            {
                var siblingIter = child;
                while ( siblingIter != null && siblingIter != toBeRemoved ) 
                {
                    siblingIter = siblingIter.GetRightSibling();
                }

                if( siblingIter == toBeRemoved )
                {
                    if( toBeRemoved.HasLeftSibling() ) 
                    {
                        toBeRemoved.GetLeftSibling().SetRightSibling( toBeRemoved.GetRightSibling() );
                    }

                    if( toBeRemoved.HasRightSibling() )
                    {
                        toBeRemoved.GetRightSibling().SetLeftSibling( toBeRemoved.GetLeftSibling() );
                    }
                }
            }

            toBeRemoved.SetParent( null );
            toBeRemoved.SetLeftSibling( null );
            toBeRemoved.SetRightSibling( null );
            ChildCount--;
        }
Пример #2
0
        protected void AddChild(LeafNode newChild)
        {
            if( newChild.HasParent() )
            {
                newChild.GetParent().RemoveChild( newChild ); 
            }

            if( IsLeaf() )
            {
                SetChild( newChild );
                newChild.SetLeftSibling( null );
            }
            else
            {
                var siblingIter = child;
                while ( siblingIter != null && siblingIter.HasRightSibling() )
                {
                    siblingIter = siblingIter.GetRightSibling();
                }

                if( siblingIter != null )
                {
                    siblingIter.SetRightSibling( newChild );
                    newChild.SetLeftSibling( siblingIter );
                }
            }

            newChild.SetParent( this );
            ChildCount++;
        }