【C++】堆的构建/插入/删除

代码在上一次堆的构建问题的基础上进行了优化,完善了其他功能👇

#include<iostream>
using namespace std;

template<class T>
class MinHeap {
private:
	T* heapArray;//存放堆数据的数组
	int currentSize;//当前堆中的元素个数
	int maxSize;//堆的大小
	//交换位置x和位置y的元素
	void swap(int x, int y) {
		T pas = heapArray[x];
		heapArray[x] = heapArray[y];
		heapArray[y] = pas;
	}
	//构建堆
	void BuildHeap() {
		cout << "你想加入多少元素?" << endl;
		int size;
		cin >> size;
		currentSize = size;
		cout << "请加入你的元素:" << endl;
		for (int i = 0; i < size; i++) {
			T value;
			cin >> value;
			heapArray[i] = value;
		}
		for (int i = currentSize / 2 - 1 ; i >= 0; i--) {
			SiftDown(i);
		}
	}
public:
	//构造函数,参数n为堆的大小
	MinHeap(const int n) {
		if (n <= 0) {
			currentSize = 0;
			maxSize = 0;
			heapArray = new T;
			return;
		}
		currentSize = 0;
		maxSize = n;
		heapArray = new T[maxSize];
		BuildHeap();
	}
	//虚析构函数
	virtual ~MinHeap() {
		delete[]heapArray;
	}
	//层序遍历打印堆中元素
	void print() {
		for (int i = 0; i < currentSize; i++) {
			cout << heapArray[i] << " ";
		}
	}
	//判断是否为叶结点
	bool isLeaf(int pos)const {
		return (pos >= currentSize / 2) && (pos < currentSize);
	}
	//返回左孩子的位置
	int LeftChild(int pos)const {
		return 2 * pos + 1;
	}
	//返回右孩子的位置
	int RightChild(int pos)const {
		return 2 * pos + 2;
	}
	//返回父结点的位置
	int Parent(int pos)const {
		return (pos - 1) / 2;
	}
	//向堆中插入新元素
	bool Insert(const T& newNode) {
		if (currentSize == maxSize) {
			return false;
		}
		heapArray[currentSize] = newNode;
		SiftUp(currentSize);
		currentSize++;
		return true;
	}
	//从堆顶删除最小值
	T& RemoveMin() {
		if (currentSize == 0) {
			cout << "Can't Delete!";
			exit(1);
		}
		else {
			swap(0, --currentSize);
			if (currentSize > 1) {
				SiftDown(0);
			}
			return heapArray[currentSize];
		}
	}
	//删除给定下标元素,并记录删除的元素
	bool Remove(int pos, T& node) {
		if ((pos < 0) || (pos >= currentSize)) {
			return false;
		}
		node = heapArray[pos];
		heapArray[pos] = heapArray[--currentSize];//用最后的元素替代被删除元素
		if (heapArray[Parent(pos)] > heapArray[pos]) {
			SiftUp(pos);
		}
		else {
			SiftDown(pos);
		}
		return true;
	}
	//从pos开始向上调整
	void SiftUp(int pos) {
		int temppos = pos;
		T temp = heapArray[temppos];
		while ((temppos > 0) && (heapArray[Parent(temppos)] > temp)) {
			heapArray[temppos] = heapArray[Parent(temppos)];
			temppos = Parent(temppos);
		}
		heapArray[temppos] = temp;
	}
	//从pos开始向下筛选
	void SiftDown(int pos) {
		int i = pos;
		int j = LeftChild(i);
		T temp = heapArray[i];
		while (j < currentSize) {
			if ((j < currentSize - 1) && (heapArray[j] > heapArray[j + 1])) {
				j++;
			}
			if (temp > heapArray[j]) {
				heapArray[i] = heapArray[j];
				i = j;
				j = LeftChild(j);
			}
			else {
				break;
			}
		}
		heapArray[i] = temp;
	}
};

int main() {
	//MinHeap<int> test(100);
	MinHeap<int> test(INT_MAX / 8);
	test.print();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是顺序表的构建插入删除、查找、反转的C语言实现: 1. 顺序表的构建 typedef struct{ int *elem; //存储空间基址 int length; //当前长度 int size; //分配的存储容量 }SqList; //初始化顺序表 void InitList(SqList &L, int size){ L.elem = (int*)malloc(size*sizeof(int)); //动态分配存储空间 if(!L.elem) exit(0); //分配失败 L.length = 0; //空表长度为0 L.size = size; //存储容量为size } 2. 顺序表的插入 //在顺序表L的第i个位置插入元素e bool ListInsert(SqList &L, int i, int e){ if(i<1 || i>L.length+1) return false; //插入位置不合法 if(L.length >= L.size) return false; //存储空间已满 for(int j=L.length; j>=i; j--){ L.elem[j] = L.elem[j-1]; //将第i个位置及以后的元素后移 } L.elem[i-1] = e; //插入元素e L.length++; //表长加1 return true; } 3. 顺序表的删除 //删除顺序表L的第i个位置的元素,并用e返回其值 bool ListDelete(SqList &L, int i, int &e){ if(i<1 || i>L.length) return false; //删除位置不合法 e = L.elem[i-1]; //保存待删除元素 for(int j=i; j<L.length; j++){ L.elem[j-1] = L.elem[j]; //将第i个位置之后的元素前移 } L.length--; //表长减1 return true; } 4. 顺序表的查找 //在顺序表L中查找值为e的元素,并返回其位置 int LocateElem(SqList L, int e){ for(int i=0; i<L.length; i++){ if(L.elem[i] == e) return i+1; //找到元素,返回位置 } return 0; //未找到元素,返回0 } 5. 顺序表的反转 //将顺序表L中的元素顺序反转 void ListReverse(SqList &L){ int temp; for(int i=0; i<L.length/2; i++){ //只需遍历一半的元素 temp = L.elem[i]; L.elem[i] = L.elem[L.length-i-1]; L.elem[L.length-i-1] = temp; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mitch311

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值