Beispiel #1
0
    private IntBBSTNode ReBalance(IntBBSTNode node)
    {
        var balanceFactor = BalanceFactor(node);

        if (balanceFactor > 1)
        {
            if (BalanceFactor(node.left) > 0)
            {
                node = LeftLeft_Rotation(node);
            }
            else
            {
                node = LeftRight_Rotation(node);
            }
        }
        else if (balanceFactor < -1)
        {
            if (BalanceFactor(node.right) > 0)
            {
                node = RightLeft_Rotation(node);
            }
            else
            {
                node = RightRight_Rotation(node);
            }
        }
        return(node);
    }
Beispiel #2
0
    private IntBBSTNode RightLeft_Rotation(IntBBSTNode parent)
    {
        IntBBSTNode temp = parent.right;

        parent.right = LeftLeft_Rotation(temp);
        return(RightRight_Rotation(parent));
    }
Beispiel #3
0
    private IntBBSTNode LeftRight_Rotation(IntBBSTNode parent)
    {
        IntBBSTNode temp = parent.left;

        parent.left = RightRight_Rotation(temp);
        return(LeftLeft_Rotation(parent));
    }
Beispiel #4
0
 protected int BalanceFactor(IntBBSTNode node)
 {
     if (node == null)
     {
         return(0);
     }
     return(Depth(node.left) - Depth(node.right));
 }
Beispiel #5
0
 protected int Depth(IntBBSTNode node)
 {
     if (node == null)
     {
         return(0);
     }
     return(Math.Max(Depth(node.left), Depth(node.right)) + 1);
 }
Beispiel #6
0
 public void Add(int ele)
 {
     if (root_ == null)
     {
         root_ = new IntBBSTNode(ele);
         return;
     }
     root_ = Insert(root_, ele);
 }
Beispiel #7
0
    /// <summary>
    ///      A                 B
    ///     / \              /   \
    ///    X   B --->       A     C
    ///       /  \         / \     \
    ///      Y    C       X   Y     N
    ///             N
    /// </summary>
    /// <param name="parent">A</param>
    /// <returns></returns>
    private IntBBSTNode RightRight_Rotation(IntBBSTNode parent)
    {
        IntBBSTNode temp;

        temp         = parent.right;
        parent.right = temp.left;
        temp.left    = parent;
        return(temp);
    }
Beispiel #8
0
    private void Inorder(IntBBSTNode root, System.Action <IntBBSTNode> func)
    {
        if (root == null)
        {
            return;
        }

        Inorder(root.left, func);
        func.Invoke(root);
        Inorder(root.right, func);
    }
Beispiel #9
0
 private IntBBSTNode Insert(IntBBSTNode root, int value)
 {
     if (root == null)
     {
         root = new IntBBSTNode(value);
         return(root);
     }
     else if (value < root.key)
     {
         root.left = Insert(root.left, value);
         root      = ReBalance(root);
     }
     else if (value >= root.key)
     {
         root.right = Insert(root.right, value);
         root       = ReBalance(root);
     }
     return(root);
 }
Beispiel #10
0
 public IntBBSTNode(int ele, IntBBSTNode left, IntBBSTNode right)
 {
     this.key = ele; this.left = left; this.right = right;
 }
Beispiel #11
0
 public IntBBSTNode()
 {
     this.left = this.right = null;
 }
Beispiel #12
0
 public IntBBST()
 {
     this.root_ = null;
 }