Beispiel #1
0
        private static AvlTreeNode <T> RightRotate(AvlTreeNode <T> node)
        {
            var next = node.Left;

            node.Left  = next.Right;
            next.Right = node;
            return((AvlTreeNode <T>)next);
        }
Beispiel #2
0
        private static AvlTreeNode <T> LeftRotateTree(AvlTreeNode <T> node)
        {
            var right = (AvlTreeNode <T>)node.Right;

            switch (right.Balance)
            {
            case Balance.RH:
                node.Balance = right.Balance = Balance.EH;
                return(LeftRotate(node));

            case Balance.LH:
                //左旋后右旋
                node.Balance = right.Balance = Balance.EH;
                node.Right   = RightRotate((AvlTreeNode <T>)node.Right);
                return(LeftRotate(node));
            }
            throw new ArgumentException("树结构错误");
        }
Beispiel #3
0
        public override bool Add(T item)
        {
            if (_root == null)
            {
                _root = new AvlTreeNode <T>(item)
                {
                    Balance = Balance.EH
                };
                return(true);
            }
            bool top = false;

            if (InsertNode((AvlTreeNode <T>)_root, null, item, ref top))
            {
                _count++;
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        private bool InsertNode(AvlTreeNode <T> node, AvlTreeNode <T> parent, T item, ref bool isHigh)
        {
            var flag = _comparer.Compare(item, node.Value);

            if (flag == 0)
            {
                return(false);
            }
            bool isLeft = false;

            if (flag > 0)
            {
                if (node.Right == null)
                {
                    switch (node.Balance)
                    {
                    case Balance.EH:
                        node.Balance = Balance.RH;
                        break;

                    case Balance.LH:
                        node.Balance = Balance.EH;
                        break;

                    case Balance.RH:
                        node.Balance = Balance.RH;
                        break;
                    }
                    isHigh     = node.Balance != Balance.EH;
                    node.Right = new AvlTreeNode <T>(item)
                    {
                        Balance = Balance.EH
                    };
                    return(true);
                }
            }
            else
            {
                if (node.Left == null)
                {
                    switch (node.Balance)
                    {
                    case Balance.EH:
                        node.Balance = Balance.LH;
                        break;

                    case Balance.LH:
                        node.Balance = Balance.LH;
                        break;

                    case Balance.RH:
                        node.Balance = Balance.EH;
                        break;
                    }
                    isHigh    = node.Balance != Balance.EH;
                    node.Left = new AvlTreeNode <T>(item);
                    return(true);
                }
                isLeft = true;
            }
            var result = InsertNode((AvlTreeNode <T>)(isLeft ? node.Left : node.Right), node, item, ref isHigh);

            if (result)
            {
                if (isHigh)
                {
                    isHigh = false;
                    switch (node.Balance) //长高,需要平衡(需要判断长高的是左节点还是右节点)
                    {
                    case Balance.EH:      //
                        node.Balance = isLeft ? Balance.LH : Balance.RH;
                        isHigh       = true;
                        break;

                    case Balance.LH:
                        if (isLeft)
                        {
                            //右旋平衡
                            if (parent == null)
                            {
                                _root = RightRotateTree(node);
                            }
                            else
                            {
                                parent.Left = RightRotateTree(node);
                            }
                        }
                        else
                        {
                            node.Balance = Balance.EH;
                        }
                        break;

                    case Balance.RH:
                        if (isLeft)
                        {
                            node.Balance = Balance.EH;
                        }
                        else
                        {
                            //左旋平衡
                            if (parent == null)
                            {
                                _root = LeftRotateTree(node);
                            }
                            else
                            {
                                parent.Right = LeftRotateTree(node);
                            }
                        }
                        break;
                    }
                }
            }
            return(result);
        }