Exemple #1
0
 /// <summary>
 /// 左旋一次,再右旋一次
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public AVLTreeNode turnLeftRight(AVLTreeNode node)
 {
     node.left = turnLeft(node.left);
     return(turnRight(node));
 }
Exemple #2
0
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="root"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public AVLTreeNode delete(AVLTreeNode root, int data)
        {
            if (root == null)
            {
                return(null);
            }

            if (data < root.data)
            {
                root.left = delete(root.left, data);
                if (getHeight(root.left) - getHeight(root.right) == 2)
                {
                    if (data < root.left.data)
                    {
                        Console.WriteLine("left=" + data);
                        root = turnRight(root);
                    }
                    else
                    {
                        root = turnLeftRight(root);
                    }
                }
            }
            else if (data > root.data)
            {
                root.right = delete(root.right, data);
                if (getHeight(root.right) - getHeight(root.left) == 2)
                {
                    if (data > root.right.data)
                    {
                        root = turnLeft(root);
                    }
                    else
                    {
                        root = turnRightLeft(root);
                    }
                }
            }
            else if (data == root.data)//等于
            {
                if (root.left != null && root.right != null)
                {
                    //寻找后继者,为右子树中最小的点
                    root.data = getSon(root.right).data;
                    //删除那个后继者
                    root.right = delete(root.right, root.data);
                }
                else
                {
                    if (root.left != null)
                    {
                        root = root.left;
                    }
                    else if (root.right != null)
                    {
                        root = root.right;
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            //更新高度
            root.height = getMaxHeight(root.left, root.right);
            return(root);
        }
Exemple #3
0
 /// <summary>
 /// 先右旋一次在左旋一次
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public AVLTreeNode turnRightLeft(AVLTreeNode node)
 {
     node.right = turnRight(node.right);
     return(turnLeft(node));
 }