数据结构之堆

1. 堆数据结构的实现

// https://mp.weixin.qq.com/s/uset3_bbDNiU6Sqk44m0Ig
public class Heap {
  private Node[] heapArray;
  private int maxSize;
  private int currentSize;

  public Heap(int mx) {
      maxSize = mx;
      currentSize = 0;
      heapArray = new Node[maxSize];

  }

  public boolean isEmpty() {
      return (currentSize == 0) ? true : false;
  }

  public boolean isFull() {
      return (currentSize == maxSize) ? true : false;
  }

  public boolean insert(int key) {
      if (isFull()) {
          return false;
      }
      Node newNode = new Node(key);
      heapArray[currentSize] = newNode;
      trickleUp(currentSize++);
      return true;
  }

  //向上调整
  public void trickleUp(int index) {
      int parent = (index - 1) / 2;
      //父节点的索引
      Node bottom = heapArray[index];
      //将新加的尾节点存在bottom中
      while (index > 0 && heapArray[parent].getKey() < bottom.getKey()) {
          heapArray[index] = heapArray[parent];
          index = parent;
          parent = (parent - 1) / 2;
      }
      heapArray[index] = bottom;

  }

  public Node remove() {
      Node root = heapArray[0];
      heapArray[0] = heapArray[--currentSize];
      trickleDown(0);
      return root;
  }


  //向下调整
  public void trickleDown(int index) {
      Node top = heapArray[index];
      int largeChildIndex;
      while (index < currentSize / 2) {
          //while node has at least one child
          int leftChildIndex = 2 * index + 1;
          int rightChildIndex = leftChildIndex + 1;
          //find larger child
          if (rightChildIndex < currentSize &&
                  //rightChild exists?
                  heapArray[leftChildIndex].getKey() < heapArray[rightChildIndex].getKey()) {
              largeChildIndex = rightChildIndex;
          } else {
              largeChildIndex = leftChildIndex;
          }

          if (top.getKey() >= heapArray[largeChildIndex].getKey()) {
              break;
          }
          heapArray[index] = heapArray[largeChildIndex];
          index = largeChildIndex;
      }
      heapArray[index] = top;
  }


  //根据索引改变堆中某个数据
  public boolean change(int index, int newValue) {
      if (index < 0 || index >= currentSize) {
          return false;
      }

      int oldValue = heapArray[index].getKey();
      heapArray[index].setKey(newValue);
      if (oldValue < newValue) {
          trickleUp(index);
      } else {
          trickleDown(index);
      }
      return true;
  }

  public void displayHeap() {
      System.out.println("heapArray(array format): ");
      for(int i = 0; i < currentSize; i++) {
          if (heapArray[i] != null) {
              System.out.print(heapArray[i].getKey() + " ");
          } else {
              System.out.print("--");
          }
      }
  }
}

class Node {
  private int iData;

  public Node(int key) {
      iData = key;
  }

  public int getKey() {
      return iData;
  }

  public void setKey(int key) {
      iData = key;
  }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值