Пример #1
0
static void
heap_fix_top_down (struct heap *heap, unsigned pos)
{
  for (;;)
    {
      if (heap_get_left_child (pos) >= heap->elem_cnt)
        break;
      if (heap_less (heap, heap_get_left_child (pos), pos))
        {
          heap_swap (heap, heap_get_left_child (pos), pos);
          pos = heap_get_left_child (pos);
          continue;
        }

      if (heap_get_right_child (pos) >= heap->elem_cnt)
        break;
      if (heap_less (heap, heap_get_right_child (pos), pos))
        {
          heap_swap (heap, heap_get_left_child (pos), pos);
          pos = heap_get_left_child (pos);
          continue;
        }

      break;
    }
}
Пример #2
0
static void heap_siftup(heap_t *h, int k) {
    for ( ; ; ) {
        int l, r, s;
        l = k * 2 + 1; /* left child */
        r = k * 2 + 2; /* right child */

        /* find the smallest of the three */
        s = k;
        if (l < h->len && heap_less(h, l, s)) s = l;
        if (r < h->len && heap_less(h, r, s)) s = r;

        if (s == k) {
            return; /* statisfies the heap property */
        }

        heap_swap(h, k, s);
        k = s;
    }
}
Пример #3
0
static void heap_siftdown(heap_t *h, int k) {
    for ( ; ; ) {
        int p = (k - 1) / 2; /* parent */

        if (k == 0 || heap_less(h, p, k)) {
            return;
        }
        heap_swap(h, k, p);
        k = p;
    }
}
Пример #4
0
static void
heap_fix_bottom_up (struct heap *heap, unsigned pos)
{
  while (!heap_is_root (pos))
    {
      if (!heap_less (heap, pos, heap_get_parent (pos)))
        break;
      heap_swap (heap, pos, heap_get_parent (pos));
      pos = heap_get_parent (pos);
    }
}