简单大根堆的实现

摘要:
74断裂;75heap_array[index]=heap_array[larger_child];76指数=较大的儿童;77}78heap_array[index]=top;//当我第一次写这句话的时候,我错过了79}8081itmin()82{83MaxHeap<int>heap;84cout<heap.IsEmpty()<<endl;;85heap.Push;86heap.Punch;87heap.Puck;88cout<<heap.Top()<˂endl;89heap.Push;90heap.Puse;91cout<˂heap.Top()<˂endl;92heap.Pop();93heap.Pop();94cout<heap.Top(()<end l;9596return0;97}请参阅猎豹数据结构和算法说明!
 1 //头文件定义--heap.h
 2 #ifndef _MAXHEAP_H_
 3 #define _MAXHEAP_H_
 4 template<class T>
 5 class MaxHeap{
 6 public:
 7     MaxHeap(int size=10);
 8     virtual ~MaxHeap();
 9 
10     bool IsEmpty();
11     void Pop();
12     void Push(const T&);
13     const T& Top()const;
14 private:
15     int current_size;//堆的当前节点个数
16     int max_size;//设置堆的最大节点个数
17     T *heap_array;
18 
19     void TrickleUp(int index);//插入新节点(末尾),接着向上渗透
20     void TrickleDown(int index);//删除根节点(用最后一个节点补到根节点),接着向下渗透
21 };
22 #endif
 1 #include<iostream>
 2 using namespace std;
 3 #include"Heap.h"
 4 template<class T>
 5 MaxHeap<T>::MaxHeap(int size = 10)
 6 {
 7     if (size < 1)
 8         throw "max size must be >=1";
 9     max_size = size;
10     current_size = 0;
11     heap_array = new T[max_size];//int *a = new int[100];开辟一个大小为100的整型数组空间
12 }
13 
14 template<class T>
15 bool MaxHeap<T>::IsEmpty()
16 {
17     return current_size == 0;
18 }
19 
20 template<class T>
21 MaxHeap<T>::~MaxHeap()
22 {
23     delete[] heap_array;
24 }
25 
26 template<class T>
27 void MaxHeap<T>::Push(const T& e)
28 {
29     if (current_size == max_size) throw "max_array is full";
30     heap_array[current_size] = e;
31     TrickleUp(current_size++);
32 }
33 
34 template<class T>
35 void MaxHeap<T>::TrickleUp(int index)//向上渗透,将新插入的元素向上调整到合适的位置
36 {
37     T bottom = heap_array[index];
38     int parent = (index - 1) / 2;
39     while (index > 0 && heap_array[parent] < bottom)//注意这个地方自己第一次写成了heap_array[parent]<heap_array[index]
40     {
41         heap_array[index] = heap_array[parent];
42         index = parent;
43         parent = (parent - 1) / 2;
44     }
45     heap_array[index] = bottom;
46 }
47 
48 template<class T>
49 void MaxHeap<T>::Pop()
50 {
51     heap_array[0] = heap_array[--current_size];
52     TrickleDown(0);
53 }
54 
55 template<class T>
56 const T& MaxHeap<T>::Top()const
57 {
58     return heap_array[0];
59 }
60 
61 template<class T>
62 void MaxHeap<T>::TrickleDown(int index)//向下渗透,将当前位置的元素向下调整到合适位置,保持大根堆的性质
63 {
64     T top = heap_array[index];
65     int larger_child;
66     while (index < current_size / 2)//这是完全二叉树的性质(最后一个非叶子节点的位置索引),好好体会
67     {
68         int lchild = 2 * index + 1;
69         int rchild = 2 * (index + 1);
70         if (rchild < current_size && heap_array[lchild] < heap_array[rchild])
71             larger_child = rchild;
72         else larger_child = lchild;
73         if (top >= heap_array[index])//!!
74             break;
75         heap_array[index] = heap_array[larger_child];
76         index = larger_child;
77     }
78     heap_array[index] = top;//这一句我第一次写时遗漏了
79 }
80 
81 int main()
82 {
83     MaxHeap<int> heap(20);
84     cout << heap.IsEmpty() << endl;;
85     heap.Push(7);
86     heap.Push(24);
87     heap.Push(9);
88     cout << heap.Top() << endl;
89     heap.Push(25);
90     heap.Push(28);
91     cout << heap.Top() << endl;
92     heap.Pop();
93     heap.Pop();
94     cout << heap.Top() << endl;
95 
96     return 0;
97 }

 参考猎豹数据结构和算法讲解!

免责声明:文章转载自《简单大根堆的实现》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[转]Mybatis出现:无效的列类型: 1111 错误Angular实现类似vuex状态管理功能、全局数据管理与同步更新下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

PLSQL常用配置之窗口/版面保存、SQL格式化/美化、SQL注释去掉注释等快捷键配置、登陆历史修改配置

//Blog.csdn.net/eyeidolon/article/details/8251791 PLSQL常用配置的快捷键配置,如窗口/布局保存、SQL格式化/美化和SQL注释删除,以及登录历史修改1的配置。PL/SQLDeveloper记住登录密码当使用PL/SQLDeveloper时,默认情况下PL/SQLDeveloper会执行此窗口中的所有SQL...

怎么使用vscode合并分支

1.切换分支到本地开发2.代码完成后提交代码到本地仓库3.切换分支到需要merge的test分支先pull一下,之后再合并分支—我开发的是这个feature,就合并这个分支4.当合并分支后,需要重新提交到远程:点击一下,直接提交...

MySQL 字段类型占用空间

MySQL支持多种列类型:数值类型、日期/时间类型和字符串(字符)类型。)1或2个字节,取决于枚举值的个数SET(‘value1’,’value2’,…)1、2、3、4或者8个字节,取决于set成员的数目上表的M只是为了说明占用空间大小,在实际创建表中char、varchar,20指的是字符而不是字节;那么字符和字节的转换要看字符集,utf-8下,1字符=3...

dbeaver 驱动安装

一、背景:在Windows10安装dbeaver数据库连接工具,点“测试连接”的时候出现报错如下:ErrorresolvingdependenciesMavenartifact'maven:/mysql:mysql-connector-java:RELEASE'nofound经网上查询是dbeaver驱动无法下载的问题。二、解决方法:打开“窗口”-˃“首选项...

选包

安装系统后,将不会安装一些基本工具。此时,您可以根据yum的要求安装它们。你也可以使用任何你想要的时尚。...

NFC应用于公交卡

由于历史遗留,NFC的兼容性实在太差。一个完备的NFC产品,应该包括三个部分:NFC硬件、统一的系统接口支持、App。这种情况下,手机NFC相当于公交公司的读卡和写卡器。...