Ejemplo n.º 1
0
 private int bfactor(AVLNode <T> elem)
 {
     return(height(elem.right) - height(elem.left));
 }
Ejemplo n.º 2
0
        public void remove(T item)
        {
            AVLNode <T> curr_node = node;

            while (true)
            {
                if (item.CompareTo(curr_node.data) == 0)
                {
                    bool isLeftChild = true;
                    if (Object.ReferenceEquals(curr_node, curr_node.parent.right))
                    {
                        isLeftChild = false;
                    }

                    if (curr_node.left == null && curr_node.right == null)
                    {
                        if (isLeftChild)
                        {
                            curr_node.parent.left = null;
                        }
                        else
                        {
                            curr_node.parent.right = null;
                        }
                        count--;
                        return;
                    }
                    else if (curr_node.left == null && curr_node.right != null)
                    {
                        if (isLeftChild)
                        {
                            curr_node.parent.left = curr_node.right;
                        }
                        else
                        {
                            curr_node.parent.right = curr_node.right;
                        }
                        fullBalance(curr_node);
                        count--;
                        return;
                    }
                    else if (curr_node.left != null && curr_node.right == null)
                    {
                        if (isLeftChild)
                        {
                            curr_node.parent.left = curr_node.left;
                        }
                        else
                        {
                            curr_node.parent.right = curr_node.left;
                        }
                        fullBalance(curr_node);
                        count--;
                        return;
                    }
                    else if (curr_node.left != null && curr_node.right != null)
                    {
                        AVLNode <T> temp = curr_node.right;
                        while (temp.left != null)
                        {
                            temp = temp.left;
                        }
                        curr_node.data = temp.data;
                        if (temp.right != null)
                        {
                            if (Object.ReferenceEquals(temp, temp.parent.left))
                            {
                                temp.parent.left = temp.right;
                            }
                            else
                            {
                                temp.parent.right = temp.right;
                            }
                        }
                        else
                        if (Object.ReferenceEquals(temp, temp.parent.left))
                        {
                            temp.parent.left = null;
                        }
                        else
                        {
                            temp.parent.right = null;
                        }
                        fullBalance(curr_node);
                        count--;
                        return;
                    }
                }
                else if (curr_node == null)
                {
                    return;
                }
                else if (item.CompareTo(curr_node.data) < 0)
                {
                    curr_node = curr_node.left;
                }
                else if (item.CompareTo(curr_node.data) > 0)
                {
                    curr_node = curr_node.right;
                }
            }
        }