コード例 #1
0
ファイル: AvlNode.cs プロジェクト: guoyu07/DevelopAdvanced
        // Finds and removes replacement node for deletion (third case).
        private IAvlNode RemoveReplacement(IAvlNode node, ref IAvlNode replacement)
        {
            IAvlNode newNode;

            // If the bottom of the left tree has been found.
            if (node.LeftChild == AvlNode.NullNode)
            {
                // The replacement node is the node found at this point.
                replacement = node;

                // Get the node's right child. This will be needed as we
                // ascend back up the tree.
                newNode = node.RightChild;
            }
            // Else the bottom of the left tree has not been found.
            else
            {
                // Create new node and continue descending down the left tree.
                newNode = new AvlNode(node.Data,
                                      RemoveReplacement(node.LeftChild, ref replacement),
                                      node.RightChild);

                // If the node is out of balance.
                if (!newNode.IsBalanced())
                {
                    // Rebalance the node.
                    newNode = newNode.Balance();
                }
            }

            // Postconditions.
            Debug.Assert(newNode.IsBalanced());

            return(newNode);
        }