<commit_before>import test from 'tape';
import BinaryHeap from '../src';

test('should tell if empty', t => {
  t.plan(3);

  const heap = new BinaryHeap();
  
  t.equal(heap.isEmpty(), true, 'heap is empty');
  
  heap.push(1);

  t.equal(heap.isEmpty(), false, 'heap is no longer empty');

  heap.pop();

  t.equal(heap.isEmpty(), true, 'heap is empty again');
});

test('should throws if trying to pop while empty', t => {
  t.plan(1);

  const heap = new BinaryHeap();

  t.throws(() => heap.pop(), Error, 'can\'t pop an empty heap');
});

test('should order values', t => {
  t.plan(1);

  const sampleSize = 10;

  const numbers = [];
  for(let i = 0; i < sampleSize; i++) {
    numbers[i] = Math.random() * 100;
  }

  const sorted = numbers.sort((a, b) => a - b);

  const heap = new BinaryHeap();
  numbers.forEach(n => heap.push(n));

  let i = 0;
  const result = [];
  while(!heap.isEmpty()) {
    if(i++ > sampleSize) break; 
    result.push(heap.pop());
  }<commit_msg>add tree navigation methods<commit_after>import test from 'tape';
import BinaryHeap from '../src';

test('should tell if empty', t => {
  t.plan(3);

  const heap = new BinaryHeap();
  
  t.equal(heap.isEmpty(), true, 'heap is empty');
  
  heap.push(1);

  t.equal(heap.isEmpty(), false, 'heap is no longer empty');

  heap.pop();

  t.equal(heap.isEmpty(), true, 'heap is empty again');
});

test('should throws if trying to pop while empty', t => {
  t.plan(1);

  const heap = new BinaryHeap();

  t.throws(() => heap.pop(), Error, 'can\'t pop an empty heap');
});

test('should give parent index', t => {
  t.plan(4);

  const heap = new BinaryHeap();

  /**
   * We use an array to lay down the data, in the form:
   * [parent, leftChild, rightChild, leftChildLeftChild, leftChildRightChild, etc...]
   */

  const leftChildIndex = heap.getLeftChildIndex(0);
  const rightChildIndex = heap.getRightChildIndex(0);
  const firstParentIndex = heap.getParentIndex(1);
  const secondParentIndex = heap.getParentIndex(2);

  t.equal(leftChildIndex, 1, 'left child index is correct');
  t.equal(rightChildIndex, 2, 'right child index is correct');
  t.equal(firstParentIndex, 0, 'left child parent is correct')
  t.equal(secondParentIndex, 0, 'right child parent is correct')
});

test('should order values', t => {
  t.plan(1);

  const sampleSize = 10;

  const numbers = [];
  for(let i = 0; i < sampleSize; i++) {
    numbers[i] = Math.random() * 100;
  }

  const sorted = numbers.sort((a, b) => a - b);

  const heap = new BinaryHeap();
  numbers.forEach(n => heap.push(n));

  let i = 0;
  const result = [];
  while(!heap.isEmpty()) {
    if(i++ > sampleSize) break; 
    result.push(heap.pop());
  }
{'fraction_non_alphanumeric': 0.17509293680297397, 'fraction_numerical': 0.011524163568773234, 'mean_word_length': 3.485, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1492010', 'n_tokens_mistral': 952, 'n_tokens_neox': 903, 'n_words': 273}