コード例 #1
0
ファイル: ATree.cs プロジェクト: anderscui/cslib
        private void RemoveNode(ANode <T> node)
        {
            ANode <T> tmp = null;

            if (node.Degree == 2)
            {
                tmp       = node.Left;
                path[++p] = tmp;
                // Find max node of left sub tree as the new root.
                while (tmp.Right.IsNotNull())
                {
                    tmp       = tmp.Right;
                    path[++p] = tmp;
                }
                node.Item = tmp.Item;
                if (path[p - 1] == node)
                {
                    path[p - 1].Left = tmp.Left;
                }
                else
                {
                    path[p - 1].Right = tmp.Left;
                }
            }
            else
            {
                tmp = node.Left;
                if (tmp.IsNull())
                {
                    tmp = node.Right;
                }
                if (p > 0)
                {
                    if (path[p - 1].Left == node)
                    {
                        path[p - 1].Left = tmp;
                    }
                    else
                    {
                        path[p - 1].Right = tmp;
                    }
                }
                else
                {
                    _root = tmp;
                }
            }

            // p points to the deleted node.
            while (p > 0)
            {
                int bf = (node.Item.CompareTo(path[p - 1].Item) < 0) ? -1 : 1;
                path[--p].BalanceFactor += bf;
                bf = path[p].BalanceFactor;
                if (bf != 0)
                {
                    // if abs(bf) is 1 or the height is not changed after rotation, stop backtracking.
                    if (bf == 1 || bf == -1 || !RotateSubTree(bf))
                    {
                        break;
                    }
                }
            }
        }