Example #1
0
    public override void insert(int data)
    {
        avl_node temp = new avl_node(data);

        insert_data(root, temp, null);
        correct_bf(path(temp));
    }
Example #2
0
    private int balance(avl_node node)
    {
        int left_height  = height(node.left);
        int right_height = height(node.right);

        return(left_height - right_height);
    }
Example #3
0
    void correct_bf(List <binary_node <int> > arr)
    {
        foreach (avl_node i in arr)
        {
            i.bf = balance(i);
        }
        arr.Reverse();
        int c = 0;
        binary_node <int> check = new avl_node(0);

        foreach (binary_node <int> i in arr)
        {
            if (c == 1)
            {
                if (i.left == null || i.right == check.left || i.right == check.right)
                {
                    i.right = check;
                }
                else if (i.right == null || i.left == check.left || i.left == check.right)
                {
                    i.left = check;
                }
                arr.Reverse();
                correct_bf(arr);
                return;
            }
            if (-1 > i.bf || i.bf > 1)
            {
                check = check_conditions(i);
                if (check == null)
                {
                    continue;
                }
                else
                {
                    c = 1;
                }
            }
        }
        if (c == 1)
        {
            root = check;
        }
    }
Example #4
0
 public avl_tree(int data)
 {
     root = new avl_node(data);
 }