1

I did not understand the error: v variable is been initialized In the call of the method less(k, v), inside the insert() method, because the variable v is initialized in line above... But even so the compiler gave me the error. My code is to insert a key in two priority queue, I added a test that i am doing with integers.

    public class MedianPQ <Key extends Comparable<Key>>{
      private MaxPq left;
      private MinPq right;

      public MedianPQ(int N, int M) {
        left = new MaxPq(N);
        right = new MinPq(M);
      }

      private boolean less( Key k, Key v) {
        return k.compareTo(v) < 0;
      }

      private boolean great( Key k, Key v) {
        return k.compareTo(v) > 0;
      }

      public void insert(Key k) {
        Key v;
        if( left.isEmpty() && right.isEmpty()) v = k; // initial
        else {
          if( less(k, v) && (left.size() < right.size() + 1)) {   //error
            left.insert(k);
          }
          else if( great(k, v) && (right.size() < left.size() + 1)) {
            right.insert(k);
          }
          else {
            if( left.size() == right.size() + 1) {
              right.insert(v);
              left.insert(k);
              v = (Key)left.max();
            }
            else {
              left.insert(v);
              right.insert(k);
              v = (Key)right.min();
            }
          }
        }
      }

Test:

public static void main(String[] args) {
    MedianPQ median = new MedianPQ(100, 100);
    Random rnd = new Random();
    for( int i = 0; i < 20; i++) {
      median.insert(rnd.nextInt(50));
    }
Marcelo de Sousa
  • 205
  • 2
  • 12

1 Answers1

1

v is declared as a local variable, but you never assign an initial value before reassigning another value

public void insert(Key k) {

Key v = null;

Or

Key v = new Key();

if( left.isEmpty() && right.isEmpty()) v = k; // initial

else 
{
    if( less(k, v) && (left.size() < right.size() + 1))
    {
        left.insert(k);
    }
    else if( great(k, v) && (right.size() < left.size() + 1)) 
    {
        right.insert(k);
    }
    else 
    {
       if( left.size() == right.size() + 1) 
       {
           right.insert(v);
           left.insert(k);
           v = (Key)left.max();
        }
        else 
        {
           left.insert(v);
           right.insert(k);
           v = (Key)right.min();
        }       
  }
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • `Key v = null` is not an initialization statement, because it will already default to being `null`. – Tim Biegeleisen Apr 03 '18 at 01:41
  • 2
    @TimBiegeleisen Local variables [don't have default values](https://stackoverflow.com/q/415687/5743988). – 4castle Apr 03 '18 at 01:46
  • @4castle But are you saying that, if checked, it would have a value other than `null`? Then what would that value be? – Tim Biegeleisen Apr 03 '18 at 01:47
  • @TimBiegeleisen In any situation where the compiler notices it might have to use a default value, the compiler just throws an error, so there's no way to test it. – 4castle Apr 03 '18 at 01:50
  • @TimBiegeleisen I have always initialized the classes and strings with null, since the values ​​that accept null, must be initialized in this way. Test it yourself and you will see that null is valid as initialization – Héctor M. Apr 03 '18 at 01:53
  • @4castle I have always initialized the classes and strings with null, since the values ​​that accept null, must be initialized in this way. Test it yourself and you will see that null is valid as initialization – Héctor M. Apr 03 '18 at 01:55