private void rebalance(Node<T> node)
    {
        for ( ; node != null; node = node.parent) {
            if (node.balanceFactor() == 2) {
                if (node.left.balanceFactor() == -1) {
                    rotateLeft(node.left);
                }
                node = rotateRight(node);
            }
            else if (node.balanceFactor() == -2) {
                if (node.right.balanceFactor() == 1) {
                    rotateRight(node.right);
                }
                node = rotateLeft(node);
            }
            if (node.parent == null) {
                root = node;
            }
        }
    }