示例#1
0
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
    struct ListNode* head = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode* travel = head;
    struct ListNode* temp = NULL;
    struct ListNode* heap = (struct ListNode *) malloc(sizeof(struct ListNode) * listsSize);
    int i = 0, p = 0, c = 0, hi = 0;
    if (listsSize == 0) return NULL; 
    head->next = NULL;
    for (i = 0; i < listsSize; i ++) {
        if (*(lists+ i) != 0)        
        *(heap + (hi ++)) = *(*(lists + i)); 
    }
    //build heap
    for (i = (listsSize - 1) / 2; i > -1; i --) {
        //heap down
        p = i;
        c = 2 * p + 1;
        while (c < hi) {
            if (c + 1 < hi && cmp(heap + c  + 1, heap + c) < 0) c ++;
            if (cmp(heap + p, heap + c) > 0) {
                swap_heap(heap, p, c);
            } else break;
            p = c;
            c = 2 * p + 1;
        }
    }
    while (heap != NULL) {
        temp = (struct ListNode *) malloc(sizeof(struct ListNode));
        temp->val = heap->val;
        temp->next = NULL;
        travel->next = temp;
        travel = temp;
        
        if (heap->next == NULL) {
            heap[0] = heap[hi - 1];
            hi --;
            if (hi == 0) break;
        } else {
            heap->val = heap->next->val;
            heap->next = heap->next->next;
        }
        
        //*heap = *(heap->next);
        //heap down
        p = 0;
        c = 2 * p + 1;
        while (c < hi) {
            if (c + 1 < hi && cmp(heap + c  + 1, heap + c) < 0) c ++;
            if (cmp(heap + p, heap + c) > 0) {
                swap_heap(heap, p, c);
            } else break;
            p = c;
            c = 2 * p + 1;
        }
    }
    return head->next;
}
示例#2
0
  // Remove a timer from the heap and list of timers.
  void remove_timer(per_timer_data& timer)
  {
    // Remove the timer from the heap.
    std::size_t index = timer.heap_index_;
    if (!heap_.empty() && index < heap_.size())
    {
      if (index == heap_.size() - 1)
      {
        heap_.pop_back();
      }
      else
      {
        swap_heap(index, heap_.size() - 1);
        heap_.pop_back();
        if (index > 0 && Time_Traits::less_than(
              heap_[index].time_, heap_[(index - 1) / 2].time_))
          up_heap(index);
        else
          down_heap(index);
      }
    }

    // Remove the timer from the linked list of active timers.
    if (timers_ == &timer)
      timers_ = timer.next_;
    if (timer.prev_)
      timer.prev_->next_ = timer.next_;
    if (timer.next_)
      timer.next_->prev_= timer.prev_;
    timer.next_ = 0;
    timer.prev_ = 0;
  }
    void heapify(std::vector< edge3<NUM_T> >& Q, std::vector<NODE_T>& nodes_to_Q,
                 NODE_T i) {

        do {
            // TODO: change to loop
            NODE_T l= LEFT(i);
            NODE_T r= RIGHT(i);
            NODE_T smallest;
            if ( (l<Q.size()) && (Q[l]._dist<Q[i]._dist) ) {
                smallest= l;
            } else {
                smallest= i;
            }
            if ( (r<Q.size())&& (Q[r]._dist<Q[smallest]._dist) ) {
                smallest= r;
            }

            if (smallest==i) return;

            swap_heap(Q, nodes_to_Q, i, smallest);
            i= smallest;
            
        } while (true);
        
    } // end heapify
 void heap_decrease_key(std::vector< edge3<NUM_T> >& Q, std::vector<NODE_T>& nodes_to_Q,
                        NODE_T v, NUM_T alt) {
     NODE_T i= nodes_to_Q[v];
     Q[i]._dist= alt;
     while (i>0 && Q[PARENT(i)]._dist>Q[i]._dist) {
         swap_heap(Q, nodes_to_Q, i, PARENT(i));
         i= PARENT(i);
     }
 } // heap_decrease_key
示例#5
0
 // Move the item at the given index up the heap to its correct position.
 void up_heap(std::size_t index)
 {
   while (index > 0)
   {
     std::size_t parent = (index - 1) / 2;
     if (!Time_Traits::less_than(heap_[index].time_, heap_[parent].time_))
       break;
     swap_heap(index, parent);
     index = parent;
   }
 }
示例#6
0
 // Move the item at the given index up the heap to its correct position.
 void up_heap(std::size_t index)
 {
   std::size_t parent = (index - 1) / 2;
   while (index > 0
       && Time_Traits::less_than(heap_[index].time_, heap_[parent].time_))
   {
     swap_heap(index, parent);
     index = parent;
     parent = (index - 1) / 2;
   }
 }
示例#7
0
 // Move the item at the given index down the heap to its correct position.
 void down_heap(std::size_t index)
 {
   std::size_t child = index * 2 + 1;
   while (child < heap_.size())
   {
     std::size_t min_child = (child + 1 == heap_.size()
         || Time_Traits::less_than(
           heap_[child].time_, heap_[child + 1].time_))
       ? child : child + 1;
     if (Time_Traits::less_than(heap_[index].time_, heap_[min_child].time_))
       break;
     swap_heap(index, min_child);
     index = min_child;
     child = index * 2 + 1;
   }
 }
 void heap_remove_first(std::vector< edge3<NUM_T> >& Q, std::vector<NODE_T>& nodes_to_Q) {
     swap_heap(Q, nodes_to_Q, 0, (NODE_T)Q.size()-1);
     Q.pop_back();
     heapify(Q,nodes_to_Q , 0);
 } // heap_remove_first