0

I have a homework that the teacher test if it's corrects by checking it's output using this website moodle.caseine.org, so to test my code the program execute these lines and compare the output with the expected one, this is the test :

Tas t = new Tas();
    Random r = new Random(123);
    for(int i =0; i<10000;i++)t.inser(r.nextInt());
        for(int i =0;i<10000;i++)System.out.println(t.supprMax());
        System.out.println(t);

And my Heap (Tas) class:

package td1;

import java.util.ArrayList;
import java.util.List;

public class Tas {
    private List<Integer> t;

    public Tas() {
        t = new ArrayList<>();
    }

    public Tas(ArrayList<Integer> tab) {
        t = new ArrayList<Integer>(tab);
    }

    public static int getFilsGauche(int i) {
        return 2 * i + 1;
    }

    public static int getFilsDroit(int i) {
        return 2 * i + 2;
    }

    public static int getParent(int i) {
        return (i - 1) / 2;
    }

    public boolean estVide() {
        return t.isEmpty();
    }

    @Override
    public String toString() {
        String str = "";
        int size = t.size();
        if (size > 0) {
            str += "[" + t.get(0);
            str += toString(0);
            str += "]";
        }
        return str;
    }

    public boolean testTas() {
        int size = t.size();
        int check = 0;
        if (size > 0) {
            for (int i = 0; i < t.size(); i++) {
                if (getFilsGauche(i) < size) {
                    if (t.get(i) < t.get(getFilsGauche(i))) {
                        check++;
                    }
                }
                if (getFilsDroit(i) < size) {
                    if (t.get(i) < t.get(getFilsDroit(i))) {
                        check++;
                    }
                }
            }
        }
        return check == 0;
    }

    public String toString(int i) {
        String str = "";
        int size = t.size();
        if (getFilsGauche(i) < size) {
            str += "[";
            str += t.get(getFilsGauche(i));
            str += toString(getFilsGauche(i));
            str += "]";
        }
        if (getFilsDroit(i) < size) {
            str += "[";
            str += t.get(getFilsDroit(i));
            str += toString(getFilsDroit(i));
            str += "]";
        }
        return str;
    }
//insert value and sort
public void inser(int value) { 
    t.add(value);
    int index = t.size() - 1;
    if (index > 0) {
        inserCheck(index);     // O(log n)
    }
}

public void inserCheck(int i) {
    int temp = 0;
    int parent = getParent(i);
    if (parent >= 0 && t.get(i) > t.get(parent)) {
        temp = t.get(parent);
        t.set(parent, t.get(i));
        t.set(i, temp);
        inserCheck(parent);
    }
}
//switch position of last element is list with first (deletes first and return it)
public int supprMax() { 
    int size = t.size();
    int max = 0;
    if (size > 0) {
        max = t.get(0);
        t.set(0, t.get(size - 1));
        t.remove(size - 1);
        supprMax(0);
    }
    else {
        throw new IllegalStateException();
    }
    return max;
}

public void supprMax(int i) {
    int size = t.size();
    int temp = 0;
    int index = i;
    if (getFilsGauche(i) < size && t.get(getFilsGauche(i)) > t.get(index)) {
        index = getFilsGauche(i);
    }
    if (getFilsDroit(i) < size && t.get(getFilsDroit(i)) > t.get(index)) {
        index = getFilsDroit(i);
    }
    if (index != i) {
        temp = t.get(index);
        t.set(index, t.get(i));
        t.set(i, temp);
        supprMax(index);
    }
}


public static void tri(int[] tab) {
        Tas tas = new Tas();
        for (int i = 0; i < tab.length; i++) {
            tas.inser(tab[i]);
        }
        for (int i = 0; i < tab.length; i++) {
            tab[i] = tas.supprMax();
        }
    }
}

The last 3 lines of the test are :

-2145024521 -2147061786 -2145666206

But the last 3 of my code are :

-2145024521 -2145666206 -2147061786

The problem are probably with the inser and supprMax methods. I hate to get a bad grade just because of 3 lines placement, because it is a program that verify the code, it dosn't care the the solution was close, it's still says it's wrong.

Marwane
  • 333
  • 3
  • 13
  • 1
    I don't see anything obvious in your code. To debug, you should consider calling `testTas` after every insertion, and after every removal. If `testTas` returns `false` then you know that the previous operation broke the heap. You can then run it again to that point and single-step your code to see where the error is. – Jim Mischel Jan 27 '23 at 05:54
  • 1
    An alternative, because the error only shows up at the end, is to let it run until just those three items are left, and then single-step the removal of `-2145024521`. I don't see an edge condition that would cause it to fail when there are only three items, but it's worth checking out. – Jim Mischel Jan 27 '23 at 05:59
  • Thank you for your reply, your second suggestion did the job, and i think the problem is with the tester, because as you can see my printed numbers are in ascending order, unlike the ones printed by the test. – Marwane Jan 27 '23 at 08:23
  • I don't know what you mean by *i think the problem is with the tester*. I meant for you to examine your data structure (the List `t`) and verify that it's in the correct order. Come to think of it, you're right ... the output *should* all be in descending order: you're working with a max-heap. Your code is operating correctly, and it's the test (from that web site?) that is in error. – Jim Mischel Jan 27 '23 at 13:57

0 Answers0