当前位置: 首页 > news >正文

谷歌收录查询工具长沙网站优化方案

谷歌收录查询工具,长沙网站优化方案,photoshop等。,专业做农牧应聘的网站计算机中的堆数据结构 什么是堆 在计算机科学中,堆(Heap)是一种重要的数据结构,它用于在动态分配时存储和组织数据。堆是一块连续的内存区域,其中每个存储单元(通常是字节)都与另一个存储单元…

计算机中的堆数据结构

什么是堆

在计算机科学中,堆(Heap)是一种重要的数据结构,它用于在动态分配时存储和组织数据。堆是一块连续的内存区域,其中每个存储单元(通常是字节)都与另一个存储单元紧密相邻。

堆和栈是计算机内存的两种主要部分。其中,栈用于存储局部变量和函数调用的信息,而堆则用于存储动态分配的变量和数据结构。

堆的特点是可以动态地增加和减少内存,而且可以任意分配内存的大小。这意味着你可以在运行时分配内存,以存储例如动态数组,图形数据结构,优先级队列等数据。

堆的好处及适用场景

堆数据结构有许多优点,这使得它在许多计算场景中都非常有用。

  1. 动态内存分配:堆允许我们在运行时动态地分配和释放内存。这意味着我们可以在程序执行的过程中,根据需要创建或删除数据。
  2. 大小不定:与栈不同,堆的大小不是预先确定的。这意味着我们可以用它来存储大量的数据,只要可用的系统内存允许。
  3. 支持自定义数据类型:由于堆是通用的内存分配机制,因此可以用它来存储任何类型的数据,不仅仅是基本类型。

下面是一些适用的场景:

  • 动态数组:堆是创建动态数组(例如动态调整大小的数组)的理想场所。你可以在运行时根据需要增加或减少数组的大小。
  • 优先级队列:优先级队列经常使用堆来实现。在这种情况下,堆的特性允许我们有效地插入和删除元素,以及在O(1)时间内查找最大(或最小)元素。
  • 动态链接列表:在动态链接列表中,我们需要在运行时创建和删除节点。这也需要使用堆内存。
  • 图形和树结构:图形和树结构通常使用堆来实现,因为这些数据结构需要在运行时动态地添加和删除节点。

C++代码实现一个堆并测试

以下是一个简单的最小堆的C++实现。注意这个例子只是为了教育目的,并没有包含一些关键的功能,比如防止溢出或检查是否溢出。

然后,我们可以继续实现其他堆操作,例如删除元素,查找最小元素等。以下是一个更完整的堆实现,包括上述缺失的操作:

#include <iostream>  
#include <vector>  
#include <stdexcept>  // for std::out_of_range  class MinHeap {  
private:  std::vector<int> data;  // underlying data structure  int parent(int i) { return (i - 1) / 2; }  // parent index  int leftChild(int i) { return 2 * i + 1; }  // left child index  int rightChild(int i) { return 2 * i + 2; }  // right child index  void siftUp(int i) {  // sift element i up to its proper place  while (i > 0 && data[parent(i)] > data[i]) {  std::swap(data[parent(i)], data[i]);  i = parent(i);  }  }  void siftDown(int i) {  // sift element i down to its proper place  int minIndex = i;  // index of current minimum element  int l = leftChild(i);  // left child index  if (l < data.size() && data[l] < data[minIndex]) {  minIndex = l;  }  int r = rightChild(i);  // right child index  if (r < data.size() && data[r] < data[minIndex]) {  minIndex = r;  }  if (i != minIndex) {  // swap i and minIndex if necessary and repeat siftDown on affected subtree  std::swap(data[i], data[minIndex]);  siftDown(minIndex);  }  }  void siftUpForInsert(int i) {  // sift element i up to its proper place after insert for heap property to be maintained  while (i > 0 && data[parent(i)] > data[i]) {  std::swap(data[parent(i)], data[i]);  i = parent(i);  }  }  public:  void insert(int value) {  // insert value into heap and maintain heap order property  data.push_back(value);  // append value to the end of the vector and remember its index (size - 1)  siftUpForInsert(data.size() - 1);  // sift up to maintain heap order property (parent is larger than its children) after insert  }  int extractMin() {  // extract the current minimum element from heap and maintain heap order property  if (data.empty()) { throw std::out_of_range("Heap is empty"); }  // heap is empty, so there is no min element throw an exception here to indicate that the situation cannot be handled and the program should stop execution with an error message to user indicating the error situation that occurred here.  int minElement = data[0];  // store the minimum element in a temporary variable minElement before swapping it with the last element in the vector and deleting it from the vector in the next step (data.pop_back()) as this will change the size of the vector and all further indices will shift downwards by one position in memory.  std::swap(data[0], data[data.size() - 1]);  // swap the first element with the last element in the vector as they will have swapped roles after this step (the last element will become the new first element/minimum element in its new position in memory while the first element will become the last element in its new position in memory after this swap operation) for maintaining the heap property after extract operation.  data.pop_back();  // remove the last element from the vector as it has just become unnecessary/redundant/no longer required in memory after the previous swapping step to maintain heap order property as required. As it is removed, all further indices will shift downwards by one position in memory for maintaining the heap property after extract operation.  siftDown(0);  // sift down the new first element/minimum element to maintain heap order property after extract operation as required. The root/first element is always at index 0 in a heap as shown in all figures above for heap data structure shown above in this code segment also. Heap is a complete binary tree (each node has either two children or no children). Binary tree is a type of tree where each node has}
http://www.dinnco.com/news/56154.html

相关文章:

  • 网站繁体和中文这么做企业文化标语经典
  • 福建省建设信息网站山东一级造价师
  • 北京市房屋和城乡建设委员会网站2022年seo还值得做吗
  • 创新的南昌网站建设seo三人行网站
  • wordpress更改地址后404.3seo外链怎么做
  • 南京 网站制作公司谷歌chrome浏览器
  • vs网站开发表格大小设置让百度收录自己的网站
  • 空调维修技术支持深圳网站建设象山seo外包服务优化
  • 做国外市场哪个网站好seo外包 靠谱
  • 网站的关键词搜索怎么做微信投放广告多少钱
  • 服务好的微网站建设百度seo教程
  • 做收藏品的网站免费关键词挖掘网站
  • 网站创建费用seo推广招聘
  • 网站建设与网页设计制作360线上推广
  • 乌鲁木齐市网站建设磁力bt种子搜索
  • wordpress 网站小模块长沙seo步骤
  • 建网站提供下载西安网站到首页排名
  • 租号网站怎么做的百度网址大全官网
  • 招聘设计师去哪个网站网络营销理论基础
  • 产品设计图片素材seo优化软件大全
  • 什么样的网站做百度广告好百度客服人工
  • 重庆建设网站公司哪家好网店代运营的套路
  • 芜湖网站建设公司seo关键词
  • 公司建立网站怎么做分录推广平台有哪些
  • 政府网站制作公司seo优化百度技术排名教程
  • 曲阜市住房和城乡建设局网站互联网营销师证书
  • 东莞网站建设营销服务平台让顾客心动的句子
  • 企业做网站建设口碑营销的产品有哪些
  • 招聘网站开发需要多长时间真正的免费建站在这里
  • 响应式网站建设对企业营销贵州网站seo