創建堆的數據結構(C++)

堆是一種數據結構,一般用數組的形式創建,數組的下標從1開始。
堆滿足兩點要求:
1.是一棵完全二叉樹
2.父節點的值比子節點的值大

c++代碼實現

main.cpp

#include<stdio.h>
#include<iostream>
using namespace std;

template <typename item>
class MaxHeap {
private:
	item* data;
	int count;

public:
	MaxHeap(int capacity) {
		data = new int[capacity + 1];
		count = 0;

	}
	~MaxHeap() {
		delete[] data;
	}
	int size() {
		return count;
	}

	bool isEmpty() {
		return count == 0;
	}

};
int main() {
	MaxHeap<int> maxheap = MaxHeap<int>(100);
	cout << maxheap.size()<<endl;
	

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章