Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test suite fail with MinHeap #23

Open
SBolivarLoL opened this issue Oct 5, 2022 · 1 comment
Open

Test suite fail with MinHeap #23

SBolivarLoL opened this issue Oct 5, 2022 · 1 comment

Comments

@SBolivarLoL
Copy link

When I run the npx jest Heap one test suite fails and says:
error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.

12     delete(): number {
                 ~~~~~~

Test Suites: 1 failed, 1 total

I can't find the error and every function that has to return a value returns a value. And delete is not even on line 12.

This is my code:

export default class MinHeap {
  public length: number;
  private data: number[];


  constructor() {
    this.data = [];
    this.length = 0;
  }

  insert(value: number): void {
    this.data[this.length] = value;
    this.heapifyUp(this.length);
    this.length++;
  }

  delete(): number {
    if (this.length === 0) {
      return -1;
    }

    const out = this.data[0];
    this.length--;

    if (this.length === 0) {
      this.data = [];
      return out;
    }

    this.data[0] = this.data[this.length];
    this.heapifyDown(0);
    return out;
  }

  private heapifyDown(idx: number): void {
    const Lidx = this.leftChild(idx);
    const Ridx = this.rightChild(idx);

    if (idx >= this.length || Lidx >= this.length) {
      return;
    }

    const lV = this.data[Lidx];
    const rV = this.data[Ridx];
    const v = this.data[idx];

    if (lV > rV && v > rV) {
      this.data[idx] = rV;
      this.data[Ridx] = v;
      this.heapifyDown(Ridx);
    } else if (rV > lV && v > lV) {
      this.data[idx] = lV;
      this.data[Lidx] = v;
      this.heapifyDown(Lidx);
    }
  }

  private heapifyUp(idx: number): void {
    if (idx === 0) {
      return;
    }

    const p = this.parent(idx);
    const parentV = this.data[p];
    const v = this.data[idx];

    if (parentV > v) {
      this.data[idx] = parentV;
      this.data[p] = v;
      this.heapifyUp(p)
    }
  }

  private parent(idx: number): number {
    return Math.floor((idx - 1) / 2);
  }

  private leftChild(idx: number): number {
    return idx * 2 + 1;
  }

  private rightChild(idx: number): number {
    return idx * 2 + 2;
  }
}
@maxrzaw
Copy link
Contributor

maxrzaw commented Oct 13, 2022

Your implementation passed all the tests when I tested it.

Could jest be referencing a MinHeap file in another day?
This would happen if you edited the MinHeap file in a src/dayN directory where N is not the latest day and a freshly generated MinHeap.ts file exists in the latest day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants