示例#1
0
  void shiftUp(int i) {
    Woman temp = A.get(i);
    Woman temp2;
    temp.index = i;
    while (i > 1 && A.get(parent(i)).dilation <= A.get(i).dilation) {
      if (A.get(parent(i)).dilation == A.get(i).dilation
          && A.get(parent(i)).order < A.get(i).order) {
        break;
      }

      temp = A.get(i);
      temp2 = A.get(parent(i));
      temp.index = parent(i);
      temp2.index = i;
      A.set(i, temp2);
      A.set(parent(i), temp);
      i = parent(i);
    }
  }
示例#2
0
  void shiftDown(int i) {
    Woman temp = A.get(i);
    Woman temp2;
    temp.index = i;

    boolean isLeft = true;
    boolean isRight = true;

    while (i <= BinaryHeapSize) {
      isLeft = true;
      isRight = true;

      int leftMax = 0, leftMaxId = 0;
      int rightMax = 0, rightMaxId = 0;

      int maxV = A.get(i).dilation, max_id = i;
      if (left(i) <= BinaryHeapSize && maxV <= A.get(left(i)).dilation) {
        if (maxV == A.get(left(i)).dilation) {
          if (A.get(i).order < A.get(left(i)).order) isLeft = false;
        }
        if (isLeft) {
          leftMax = A.get(left(i)).dilation;
          leftMaxId = left(i);
        }
      }

      if (right(i) <= BinaryHeapSize && maxV <= A.get(right(i)).dilation) {
        if (maxV == A.get(right(i)).dilation) {
          if (A.get(i).order < A.get(right(i)).order) isRight = false;
        }
        if (isRight) {
          rightMax = A.get(right(i)).dilation;
          rightMaxId = right(i);
        }
      }

      if (rightMax > leftMax) {
        maxV = A.get(right(i)).dilation;
        max_id = right(i);
      } else if (rightMax < leftMax) {
        maxV = A.get(left(i)).dilation;
        max_id = left(i);
      } else if (rightMax == leftMax) {
        if (A.get(rightMaxId).order > A.get(leftMaxId).order) {
          maxV = A.get(left(i)).dilation;
          max_id = left(i);
        } else if (A.get(rightMaxId).order < A.get(leftMaxId).order) {
          maxV = A.get(right(i)).dilation;
          max_id = right(i);
        }
      }

      if (max_id != i) {
        temp = A.get(i);
        temp2 = A.get(max_id);
        temp.index = max_id;
        temp2.index = i;
        A.set(i, temp2);
        A.set(max_id, temp);
        i = max_id;
      } else break;
    }
  }