C++之Binary Heap/Max Heap


  1 #include <iostream>
  2 #include <time.h>
  3 #include <random>
  4
  5 using namespace std;
  6
  7 //Binary Heap; Max Heap;
  8
  9 class BinaryHeap
 10 {
 11 public:
 12     BinaryHeap();
 13     BinaryHeap(int capacity);
 14     ~BinaryHeap();
 15
 16     int insert(int value);
 17     int getIndex(int value);
 18     int removeRoot();
 19     void print();
 20     bool isEmpty();
 21
 22 private:
 23     void sortUp(int start);
 24     void sortDown(int start, int end);
 25
 26     int *heap;
 27     int capacity;
 28     int size ;
 29 };
 30
 31 BinaryHeap::BinaryHeap()
 32 {
 33     this->size = 0;
 34     this->capacity = 50;
 35     heap = new int[this->capacity];
 36 }
 37
 38 BinaryHeap::BinaryHeap(int capacity)
 39 {
 40     this->size = 0;
 41     this->capacity = capacity;
 42     heap = new int[this->capacity];
 43 }
 44
 45 BinaryHeap::~BinaryHeap()
 46 {
 47     this->size = 0;
 48     this->capacity = 0;
 49     delete[] heap;
 50 }
 51
 52 int BinaryHeap::insert(int value)
 53 {
 54
 55     if (this->size==this->capacity) //The heap is full
 56     {
 57         return -1;
 58     }
 59     heap[this->size] = value;
 60     this->sortUp(this->size);
 61     this->size++;
 62     return 0;
 63 }
 64
 65 int BinaryHeap::getIndex(int value)
 66 {
 67     for (int i = 0; i < this->size; i++)
 68     {
 69         if (value==this->heap[i])
 70         {
 71             return i;
 72         }
 73     }
 74     return -1;
 75 }
 76
 77 int BinaryHeap::removeRoot()
 78 {
 79
 80     int index = 0;
 81     if (this->size==0)
 82     {
 83         return 1;//The heap is empty
 84     }
 85
 86     this->heap[index] = this->heap[--this->size];
 87     this->sortDown(index, this->size - 1);
 88
 89     return 0;
 90 }
 91
 92 void BinaryHeap::print()
 93 {
 94     for (int i = 0; i < this->size; i++)
 95     {
 96         cout << "No." << i + 1 << " : " << heap[i] << " " << endl;;
 97     }
 98 }
 99
100
101
102 bool BinaryHeap::isEmpty()
103 {
104     if (this->size == 0)
105     {
106         return true;
107     }
108     else
109     {
110         return false;
111     }
112 }
113
114
115
116 void BinaryHeap::sortUp(int start)
117 {
118     int c = start;  //The location of current node
119     int p = (c - 1) / 2; //The location of parent node
120     int temp = heap[c]; //The value of current node
121
122     while (c>0)
123     {
124         if (heap[p] > temp)
125         {
126             break;
127         }
128         else
129         {
130             heap[c] = heap[p];
131             c = p;
132             p = (p - 1) / 2;
133         }
134     }
135     heap[c] = temp;
136 }
137
138 void BinaryHeap::sortDown(int start, int end)
139 {
140     int c=start; //the location of current node
141     int l = 2*c + 1; //The location of left child
142     int temp = heap[c]; //The value of current node
143
144     while (l <= end)
145     {
146         if (l<end && heap[l]<heap[l+1])
147         {
148             l++;  //Choose the bigger one between left child and right child
149         }
150         if (temp>=heap[l])
151         {
152             break;
153         }
154         else
155         {
156             heap[c] = heap[l];
157             c = l;
158             l = 2 * l + 1;
159         }
160     }
161     heap[c] = temp;
162 }
163
164
165 int main()
166 {
167
168
169     BinaryHeap *b = new  BinaryHeap(50);
170
171     default_random_engine random(time(NULL)); //C++ 11
172     uniform_int_distribution<int> num(0, 999);//C++ 11
173
174     cout << "Insert 50 randon number which between 0~999 " << endl ;
175     for (int i = 0; i <50; i++)
176     {
177         b->insert(num(random));
178     }
179
180     cout << "Print: " << endl;
181     b->print();
182
183     cout << endl << endl;
184     cout << "Is the heap empty? " << endl;
185     cout << boolalpha << b->isEmpty() << endl << endl;
186
187     cout << "Remove" << endl;
188     switch (int n=b->removeRoot())
189     {
190     case 0:
191         cout << "Success! The root node has been removed!" << endl; break;
192     case 1:
193         cout << "Heap is empty! " << endl; break;
194     }
195     cout << endl;
196
197     cout << "Print: " << endl;
198     b->print();
199
200
201
202     system("pause");
203     return 0;
204 }
时间: 2024-12-14 16:34:12

C++之Binary Heap/Max Heap的相关文章

[Algorithm] How to use Max Heap to maintain K smallest items

Let's say we are given an array: [4,1,5,2,3,0,10] We want to get K = 3 smallest items from the array and using Max heap data structure. So this is how to think about it. 1. We take first K items put it into Max Heap: 5 /     \ 4          1 2. Then we

java.lang.OutOfMemoryError: PermGen space PermGen space &amp; java.lang.OutOfMemoryError: Java heap space Heap siz

java.lang.OutOfMemoryError: PermGen space PermGen space 由-XX:PermSize  -XX:MaxPermSize 引起 java.lang.OutOfMemoryError: Java heap space Heap siz 由-Xms -Xmx 引起 Liunx下修改:catalina.sh # OS specific support.  $var _must_ be set to either true or false. JAVA

Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个heap,一个是最小堆,一个是最大堆. 2.一直使maxHeap的size大于minHeap. 3. 当两边size相同时,比较新插入的value,如果它大于minHeap的最大值,把它插入到minHeap.并且把minHeap的最小值移动到maxHeap. ...具体看代码 1 /*********

(算法)Binary Tree Max Path Sum

题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For ex

Shallow heap &amp; Retained heap

所有包含Heap Profling功能的工具(MAT, Yourkit, JProfiler, TPTP等)都会使用到两个名词,一个是Shallow Size,另一个是 Retained Size. 这是两个在平时不太常见的名词,本文会对这两个名词做一个详细的解释. Shallow Size 对象自身占用的内存大小,不包括它引用的对象. 针对非数组类型的对象,它的大小就是对象与它所有的成员变量大小的总和.当然这里面还会包括一些java语言特性的数据存储单元. 针对数组类型的对象,它的大小是数组元

数据结构之Heap (Java)

Heap简介 Heap译为“堆”,是一种特殊的树形数据结构,它满足所有堆的特性:父节点的值大于等于子节点的值(max heap),或者小于等于子节点的值(min heap).对于max heap 根节点的值为整个树最大值,反之亦然,min heap 根节点的值为整个树最小值.本文采用Java编程语言简单实现min heap. Java Heap 对于大多数应用来说,Java堆 (Java Heap) 是Java虚拟机所管理的内存中最大的一块.Java堆是被所有线程共享的一块内存区域,在虚拟机启动

java heap space, PermGen space 错误 使用jvisualvm监测设置合理值

使用myeclipse启动tomcat 报java heap space ,PermGen space 错误,分别为 heap内存不足,PermGen内存不足需加大 tomcat启动项参数 Xmx 和 XX:MaxPermSizePermGen是指内存的永久保存区域,它用于存放class和 method 对象,以及String 对象(sun原文:permanent generation is the area of the heap where class and method objects

BZOJ 2333 SCOI 2011 棘手的操作 可并堆

做此题的原因 题号美 题目大意 给出一个序列,支持一堆操作(具体看下面).让你维护它. 思路 U x y:我们需要可并堆来将两个堆合并. A1 x v:将这个点从堆中拽出来,改了之后再合并回去. A2 x v:在堆顶打标记. A3:记录一个全局变量记录. F1 x:将这个点到堆顶的链上的所有标记下传,之后返回自己的大小. F2 x:返回堆顶. F3:用一个堆(set也行)维护所有堆顶的元素.需要仔细讨论一下. CODE #define _CRT_SECURE_NO_WARNINGS #inclu

堆(Heap)和二叉堆(Binary heap)

堆(Heap): The operations commonly performed with a heap are: create-heap: create an empty heap heapify: create a heap out of given array of elements find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap (aka, peek)