In the data structure, the heap is a kind of very important structure. A heap structure is a set of array objects that we can think of as a complete binary tree.
Max Heap: each father node in the heap is larger than its child node.
Minimum heap: each father node in the heap is smaller than its child node.
is a maximum heap:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/59/wKioL1cbOTSjDiLRAAAZq4jMjWY012.png "title=" p%95z " ~A17_NA7) q280v4qc.png "alt=" Wkiol1cbotsjdilraaazq4jmjwy012.png "/>
When implementing the code my test sequence is: int a[] = {10, 11, 13, 12, 16, 18, 15, 17, 14, 19};
We put out its picture for easy analysis.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/5A/wKioL1cbVunRJvBdAABYpvWkEaw905.png "title=" lfpb4@ [(Bdt~xcj5g_[(]f1.png "alt=" Wkiol1cbvunrjvbdaabypvwkeaw905.png "/>
The implementation code is as follows:
Creating a header file Heap.hpp
#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>using namespace std; #include < Assert.h> #include <vector>template <class T>class Heap{public: heap () :_a (NULL) {} //constructs the heap: first receives each element, then adjusts the element according to the heap characteristic heap (const t* array, size_t size) { _a.reserve (size); for (size_t i = 0; i < size; i++) { _a.push_back (Array[i]); } //Build Heap int size = size; for (int j = (_a.size () - 2) / 2; j >=0; j --) { _adjustdown (j, size); } } //Copy Construction heap (Const vector<T >& vec) :_a (NULL) { _a.reserve (Vec.size ()); for (size_t i = 0; i < size; i++) { _a.push_back ( Vec[i]); } } // Insert an element x: first inserted into the order table and then adjusted to the specific element sizeTo determine the position of the inserted element void push (const t& x) { _a.push_back (x); _ Adjustup (_a.size () - 1) } //Delete root node void pop () { size_t size = _a.size (); assert (size > 0);//Defensive programming, Determines whether the element swap (_a[0], _a[size - 1]) can be deleted;// If the root node of the heap is deleted directly, the heap structure is disturbed _a.pop_back ();//The root node is swapped with the last node of the heap, and then the element is deleted. and adjust it to the right position size = _a.size (); _adjustdown (0,size); } //access the root node of the heap t& geTtop () { size_t size = _ A.size (); assert (size > 0); return _a[0]; } //to adjust the root node down void _adjustdown (size_t parent,size_t size) { size_t child = 2 * parent + 1; while (child<size) { if ( CHILD+1&NBSP;<&NBSP;SIZE&NBSP;&&&NBSP;_A[CHILD]&NBSP;<&NBSP;_A[CHILD&NBSP;+&NBSP;1]) { child++; } if (_a[child] > _a[parent]) { swap (_a[child], _a[parent]); parent = child; child = 2 * parent + 1; } else { break; } } } //upward adjustment void _adjustup (Int child) { //(child-2)/2 calculates the subscript of the parent node at this time, regardless of whether it is Zuozi or right subtree after the insertion of the node size_t parent = (child - 1) / 2; int index = child; size_ T size = _a.size (); while (child<size) { if (index % 2 == 0 && _a[index - 1] > _a[index]) &Nbsp; { --child; } if (index % 2 != 0 && index + 1 < child && _a[index] &NBSP;<&NBSP;_A[INDEX&NBSP;+&NBSP;1]) { ++child ; } if (_a[child]>_a[parent]) { swap (_a [Child], _a[parent]); child = parent; parent = (child-1)/2; } else { break; } } } bool emPty () { size_t size = _ A.size (); assert (size >= 0); return size == 0; } size_t size () { size_t Size = _a.size (); assert (size >= 0); return size; }private: vector<T> _a;};
Creating source Files Heap.cpp
#define _crt_secure_no_warnings 1#include "heap.hpp" void Test () {int a[] = {10, 11, 13, 12, 16, 18, 15, 17, 14, 19}; Heap<int> H1 (A, sizeof (a)/sizeof (a[0])); heap<int> H2 (H1); Cout<
With respect to the size (), GetTop () function we can test by writing the appropriate test case with the test function (), and the heap h1,h2 whether or not the implementation of Chengdagen is changed, we can view it by debugging.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/5D/wKiom1cbb3-gg6vPAACUnW3YXOM564.png "title=" 8r4bfk1gii4]4q$1u$2ytew.png "alt=" Wkiom1cbb3-gg6vpaacunw3yxom564.png "/>
This article is from "Han Jing's Blog", please make sure to keep this source http://10740184.blog.51cto.com/10730184/1767076
Implementation of the "Data structure" heap (including: Default member function, insert element Push, delete element pop, access root node top, empty, size)