Exemplo n.º 1
0
        private string PrintNode(AvlNode node, int tabs = 0)
        {
            var prefix = "\n|";

            for (int i = 0; i < tabs; i++)
            {
                prefix += "--";
            }

            if (node == null)
            {
                return(prefix + "None");
            }

            tabs++;

            return(prefix + "V: " + node.Value + "" + PrintNode(node.Left, tabs) + "" + PrintNode(node.Right, tabs));
        }
Exemplo n.º 2
0
        public AvlNode DeleteNode(AvlNode root, int key)
        {
            if (root == null)
            {
                return(root);
            }

            if (key < root.Value)
            {
                root.Left = DeleteNode(root.Left, key);
            }
            else if (key > root.Value)
            {
                root.Right = DeleteNode(root.Right, key);
            }
            else
            {
                if (root.Left == null || root.Right == null)
                {
                    AvlNode temp = null;

                    if (temp == root.Left)
                    {
                        temp = root.Right;
                    }
                    else
                    {
                        temp = root.Left;
                    }

                    if (temp == null)
                    {
                        temp = root;
                        root = null;
                    }
                    else
                    {
                        root = temp;
                    }
                }
                else
                {
                    var temp = MinValueNode(root.Right);
                    root.Value = temp.Value;
                    root.Right = DeleteNode(root.Right, temp.Value);
                }
            }

            if (root == null)
            {
                return(root);
            }

            root.Height = Math.Max(Height(root.Left), Height(root.Right)) + 1;

            var balance = GetBalance(root);

            if (balance > 1 && GetBalance(root.Left) >= 0)
            {
                return(RightRotate(root));
            }

            if (balance > 1 && GetBalance(root.Left) < 0)
            {
                root.Left = LeftRotate(root.Left);
                return(RightRotate(root));
            }

            if (balance < -1 && GetBalance(root.Right) <= 0)
            {
                return(LeftRotate(root));
            }

            if (balance < -1 && GetBalance(root.Right) > 0)
            {
                root.Right = RightRotate(root.Right);
                return(LeftRotate(root));
            }

            return(root);
        }