域名

聊聊堆Heap和二叉堆的实现和特性

字号+作者:益华科技来源:应用开发2025-11-05 10:06:00我要评论(0)

复制//Java importjava.util.Arrays; importjava.util.NoSuchElementException;

聊聊堆Heap和二叉堆的实现和特性
复制// Java  import java.util.Arrays;  import java.util.NoSuchElementException;  public class BinaryHeap {      private static final int d = 2;      private int[] heap;      private int heapSize;      /**       * This will initialize our heap withdefaultsize.       */      public BinaryHeap(int capacity) {          heapSize = 0;          heap = new int[capacity + 1];          Arrays.fill(heap,聊聊 -1);      }      public boolean isEmpty() {          return heapSize == 0;      }      public boolean isFull() {          return heapSize == heap.length;      }      private int parent(int i) {          return (i - 1) / d;      }      private int kthChild(int i, int k) {          return d * i + k;      }      /**       * Inserts new element into heap       * Complexity: O(log N)       * As worst case scenario, we need to traverse till the root       */      public void insert(int x) {          if (isFull()) {              throw new NoSuchElementException("Heap is full, No space to insert new element");          }          heap[heapSize] = x;          heapSize ++;          heapifyUp(heapSize - 1);      }      /**       * Deletes element atindex x       * Complexity: O(log N)       */      publicintdelete(int x) {          if (isEmpty()) {              throw new NoSuchElementException("Heap is empty, No element to delete");          }          int maxElement = heap[x];          heap[x] = heap[heapSize - 1];          heapSize--;         heapifyDown(x);          return maxElement;      }      /**       * Maintains the heap property while inserting an element.       */      private void heapifyUp(int i) {          int insertValue = heap[i];          while (i > 0 && insertValue > heap[parent(i)]) {              heap[i] = heap[parent(i)];              i = parent(i);          }          heap[i] = insertValue;      }      /**       * Maintains the heap property while deleting an element.       */      private void heapifyDown(int i) {          int child;          inttemp = heap[i];          while (kthChild(i, 1) < heapSize) {              child = maxChild(i);              if (temp >= heap[child]) {                  break;              }              heap[i] = heap[child];              i = child;          }          heap[i] = temp;      }      private int maxChild(int i) {          int leftChild = kthChild(i, 1);          int rightChild = kthChild(i, 2);          return heap[leftChild] > heap[rightChild] ? leftChild : rightChild;      }      /**       * Prints all elements of the heap       */      public void printHeap() {          System.out.print("nHeap = ");          for (int i = 0; i < heapSize; i++)              System.out.print(heap[i] + " ");          System.out.println();      }      /**       * This method returns the max element of the heap.       * complexity: O(1)       */      publicint findMax() {          if (isEmpty())              throw new NoSuchElementException("Heap is empty.");          return heap[0];      }      publicstatic void main(String[] args) {          BinaryHeap maxHeap = new BinaryHeap(10);          maxHeap.insert(10);          maxHeap.insert(4);          maxHeap.insert(9);          maxHeap.insert(1);          maxHeap.insert(7);          maxHeap.insert(5);          maxHeap.insert(3);          maxHeap.printHeap();          maxHeap.delete(5);          maxHeap.printHeap();          maxHeap.delete(2);          maxHeap.printHeap();      }  }  1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 步骤:1. 终端运行复制代码代码如下:复制代码代码如下:复制代码代码如下:sudo /etc/init.d/apache2 restart附xampp的ModRewrite开启方法:要开启Mod Rewrite功能其实是很简单的:1.在你的XAMPP安装目录下找到 httpd.conf 这个文件( 位于etchttpd.conf)2.用vim或其他文本编辑器打开它3.找到 “AllowOverride None”, 替换为”AllowOverride All“。(修改第一个就可以了)4.再找到”#LoadModule rewrite_module modules/mod_rewrite.so“,把前面的”#”号去掉5.重启XAMPP然后Mod Rewrite功能就开启了:)

    步骤:1. 终端运行复制代码代码如下:复制代码代码如下:复制代码代码如下:sudo /etc/init.d/apache2 restart附xampp的ModRewrite开启方法:要开启Mod Rewrite功能其实是很简单的:1.在你的XAMPP安装目录下找到 httpd.conf 这个文件( 位于etchttpd.conf)2.用vim或其他文本编辑器打开它3.找到 “AllowOverride None”, 替换为”AllowOverride All“。(修改第一个就可以了)4.再找到”#LoadModule rewrite_module modules/mod_rewrite.so“,把前面的”#”号去掉5.重启XAMPP然后Mod Rewrite功能就开启了:)

    2025-11-05 09:46

  • 电脑设计玩具刀头教程——打造独特的创意玩具(用电脑设计刀头,实现你的创意灵感)

    电脑设计玩具刀头教程——打造独特的创意玩具(用电脑设计刀头,实现你的创意灵感)

    2025-11-05 09:38

  • 现代科技带来的智能手表(探索智能手表的功能与应用,以及未来发展趋势)

    现代科技带来的智能手表(探索智能手表的功能与应用,以及未来发展趋势)

    2025-11-05 09:16

  • 华为VR电脑串流教程(华为VR电脑串流教程,助你打破空间限制,畅游虚拟世界)

    华为VR电脑串流教程(华为VR电脑串流教程,助你打破空间限制,畅游虚拟世界)

    2025-11-05 07:21

网友点评