fixHa.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**************************************************
  2. *
  3. * fixHa.c (for spread)
  4. * This routine is to restore a 'heap' order upon
  5. * removing a cell from the heap, which is ordered
  6. * by the min_cost of a cell.
  7. *
  8. **************************************************/
  9. #include <stdlib.h>
  10. #include "costHa.h"
  11. #include "local_proto.h"
  12. struct costHa *fixHa(long go_pos, struct costHa *heap, long heap_len)
  13. {
  14. long vacant, smaller_child;
  15. if (heap_len == 0)
  16. return NULL;
  17. vacant = go_pos;
  18. while (2 * vacant <= heap_len) {
  19. smaller_child = 2 * vacant;
  20. if ((2 * vacant < heap_len) &&
  21. (heap[2 * vacant + 1].min_cost < heap[2 * vacant].min_cost))
  22. smaller_child++;
  23. if (heap[heap_len].min_cost > heap[smaller_child].min_cost) {
  24. heap[vacant].min_cost = heap[smaller_child].min_cost;
  25. heap[vacant].angle = heap[smaller_child].angle;
  26. heap[vacant].row = heap[smaller_child].row;
  27. heap[vacant].col = heap[smaller_child].col;
  28. vacant = smaller_child;
  29. }
  30. else
  31. break;
  32. }
  33. heap[vacant].min_cost = heap[heap_len].min_cost;
  34. heap[vacant].angle = heap[heap_len].angle;
  35. heap[vacant].row = heap[heap_len].row;
  36. heap[vacant].col = heap[heap_len].col;
  37. return heap;
  38. }