当前位置:   article > 正文

C语言_堆的基本操作_请设计int empty(heap h)函数。 该函数判断堆h是否空堆,如果是空堆返回1,否则返回

请设计int empty(heap h)函数。 该函数判断堆h是否空堆,如果是空堆返回1,否则返回

本片博客主要包含一下几个内容:

堆的基本操作

1、创建堆

2、调整堆(向下调整)

3、插入

4、移除堆顶元素

5、返回堆元素个数

6、判断是不是空堆

7、返回堆顶元素

8、向上调整堆

9、交换两个数

10、判断堆是否已满,如果满了,就增容;如果没有满,就返回

11、销毁堆

12、小于比较

13、大于比较

14、头文件代码

15、测试代码


 


1、创建堆


  1. void CreateHeap(Heap *hp, HPDataType* arr, int size, Compare Com)
  2. {
  3. int cur = 0;
  4. assert (hp != NULL);
  5. hp->_hp = (HPDataType*)malloc (size * sizeof (HPDataType));
  6. if (hp->_hp == NULL)
  7. {
  8. assert (0);
  9. return ;
  10. }
  11. //拷贝元素
  12. memcpy (hp->_hp, arr, size*sizeof (HPDataType));
  13. hp->size = size;
  14. hp->capacity = size;
  15. hp->_com = Com;
  16. //调整成堆
  17. cur = ((size-1-1)>>1); //size-1为最大下标,再减一除二为最后一个节点的双亲节点
  18. for (; cur>=0; cur--)
  19. {
  20. AdjustDown (hp, cur);//向下调整堆
  21. }
  22. }


2、调整堆(向下调整)


  1. void AdjustDown (Heap* hp, int parent)//parent 为开始调整的节点
  2. {
  3. int child = 0;
  4. assert (hp != NULL);
  5. child = (parent<<1) + 1; //孩子节点等于双亲节点乘二加一
  6. while (child < hp->size)
  7. {
  8. if (child+1 < hp->size)//如果右孩子存在,找左右孩子中最小的孩子
  9. {
  10. if (hp->_com (hp->_hp[child], hp->_hp[child+1]))
  11. {
  12. child += 1;
  13. }
  14. }
  15. if (hp->_com(hp->_hp[parent], hp->_hp[child]))
  16. {
  17. Swap (&hp->_hp[parent], &hp->_hp[child]);
  18. }
  19. else
  20. {
  21. return;
  22. }
  23. parent = child;
  24. child = (parent<<1) + 1;
  25. }
  26. }


3、插入

 

  1. void InsertHeap (Heap* hp, HPDataType data) //插入元素
  2. {
  3. assert (hp != NULL);
  4. //判断还有没有空间,有的话就插入元素,没有就增容再插入元素
  5. CheakHeap (hp);
  6. hp->_hp[hp->size] = data;
  7. hp->size++;
  8. //向上调整堆
  9. AdjustUp (hp, (hp->size)-1);
  10. }



4、移除堆顶元素

  1. //移除元素,(把堆顶元素和最后一个元素交换,size--就把堆顶元素删除了,最后在调整一下堆顶元素)
  2. void RemoveHeap (Heap* hp)
  3. {
  4. assert (hp != NULL);
  5. Swap (&hp->_hp[0], &hp->_hp[hp->size-1]);
  6. hp->size--;
  7. AdjustDown (hp, 0, hp->_com);
  8. }


5、返回堆元素个数


  1. int SizeHeap (Heap* hp) //返回堆元素个数
  2. {
  3. assert (hp != NULL);
  4. return hp->size;
  5. }


6、判断是不是空堆


  1. int IsHeapEmpty (Heap* hp) //判断是不是空堆,空堆返回1,非空返回0
  2. {
  3. assert (hp != NULL);
  4. return (hp->size == 0);
  5. }


7、返回堆顶元素


  1. HPDataType HeapTop (Heap* hp) //返回堆顶元素
  2. {
  3. assert (hp != NULL);
  4. return hp->_hp[0];
  5. }


8、向上调整堆


  1. void AdjustUp (Heap* hp, int child)
  2. {
  3. int parent = 0;
  4. assert (hp != NULL);
  5. parent = (child - 1)/2;
  6. while (child)
  7. {
  8. if (!(hp->_com(hp->_hp[child], hp->_hp[parent])))
  9. {
  10. Swap (&hp->_hp[child], &hp->_hp[parent]);
  11. }
  12. child = parent;
  13. parent = (child - 1)/2;
  14. }
  15. }


9、交换两个数


  1. void Swap (HPDataType*p, HPDataType* q)
  2. {
  3. HPDataType tmp;
  4. assert (p != NULL && q != NULL);
  5. tmp = *p;
  6. *p = *q;
  7. *q = tmp;
  8. }

 


10、判断堆是否已满,如果满了,就增容;如果没有满,就返回


  1. void CheakHeap (Heap* hp) //判断堆是否已满,如果满了,就增容;如果没有满,就返回
  2. {
  3. int newCapacity = 0;
  4. int i = 0;
  5. HPDataType* temp;
  6. assert (hp != NULL);
  7. if (hp->capacity > hp->size)
  8. {
  9. return ;
  10. }
  11. //如果堆已满,增容
  12. newCapacity = 2 * (hp->capacity)+3;
  13. temp = (HPDataType*)malloc (newCapacity * sizeof (HPDataType)); //开辟新空间
  14. if (temp == NULL)
  15. {
  16. perror ("CheakHeap::malloc>>");
  17. return ;
  18. }
  19. //拷贝元素
  20. for (; i<hp->size; ++i)
  21. {
  22. temp[i] = hp->_hp[i];
  23. }
  24. //释放原空间
  25. free (hp->_hp);
  26. hp->_hp = NULL;
  27. //让原空间指向新开辟的空间
  28. hp->_hp = temp;
  29. hp->capacity = newCapacity;
  30. }

11、销毁堆


  1. void DestroyHeap (Heap* hp)
  2. {
  3. assert (hp != NULL);
  4. free (hp->_hp);
  5. hp->_hp = NULL;
  6. hp->capacity = 0;
  7. hp->size = 0;
  8. printf ("销毁成功\n");
  9. }


12、小于比较


  1. //Less 和 Greater两个函数用来比较两个数的大小,在建堆时用函数指针的形式调用,用来分别建大堆小堆
  2. int Less (HPDataType pLeft, HPDataType pRight) //小于比较
  3. {
  4. assert (pLeft != NULL && pRight != NULL);
  5. if (pLeft > pRight)
  6. {
  7. return 0;
  8. }
  9. else
  10. {
  11. return 1;
  12. }
  13. }


13、大于比较


  1. int Greater (HPDataType pLeft, HPDataType pRight) //大于比较
  2. {
  3. if (pLeft > pRight)
  4. {
  5. return 1;
  6. }
  7. else
  8. {
  9. return 0;
  10. }
  11. }

14、头文件代码


  1. #ifndef __HEAP_H__
  2. #define __HEAP_H__
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. //堆:是一个数组,其中的元素是按照二叉树的顺序在数组中存放的
  8. //任意一个节点的数据都比其左右孩子都小---小堆
  9. //任意一个节点的数据都比它的左右孩子大---大堆
  10. typedef int HPDataType;
  11. typedef int (*Compare) (HPDataType pLeft, HPDataType pRight);
  12. typedef struct Heap
  13. {
  14. HPDataType* _hp;
  15. int capacity;
  16. int size;
  17. Compare _com;
  18. }Heap;
  19. void CreateHeap(Heap *hp, HPDataType* arr, int size, Compare Com); //创建堆
  20. void AdjustDown (Heap* hp, int parent); //调整堆(向下调整)
  21. void InsertHeap (Heap* hp, HPDataType data); //插入
  22. void RemoveHeap (Heap* hp); //移除元素,(把堆顶元素和最后一个元素交换,size--就把堆顶元素删除了,最后在调整一下堆顶元素)
  23. int SizeHeap (Heap* hp); //返回堆元素个数
  24. int IsHeapEmpty (Heap* hp); //判断是不是空堆
  25. HPDataType HeapTop (Heap* hp); //返回堆顶元素
  26. void AdjustUp (Heap* hp, int child); //向上调整堆
  27. void Swap (HPDataType* p, HPDataType*q); //交换两个数
  28. void CheakHeap (Heap* hp); //判断堆是否已满,如果满了,就增容;如果没有满,就返回
  29. void DestroyHeap (Heap* hp); //销毁堆
  30. int Less (HPDataType pLeft, HPDataType pRight); //小于比较
  31. int Greater (HPDataType pLeft, HPDataType pRight); //大于比较
  32. #endif

15、测试代码


  1. #include "Heap.h"
  2. void test_Heap()
  3. {
  4. //test_CreateHeap
  5. int ret = 0;
  6. HPDataType top = 0;
  7. int arr[] = {53, 17, 78, 9, 45, 65, 87, 23, 31};
  8. Heap hp;
  9. CreateHeap (&hp, arr, sizeof(arr)/sizeof(arr[0]), Less);
  10. //test_SizeEmptyTopHeap
  11. ret = SizeHeap(&hp);
  12. printf ("堆的大小为:%d\n", ret);
  13. ret = IsHeapEmpty(&hp);
  14. if (ret == 1)
  15. {
  16. printf ("这是空堆!\n");
  17. }
  18. else
  19. {
  20. printf ("不是空堆!\n");
  21. }
  22. top = HeapTop(&hp);
  23. printf ("堆顶元素是:%d\n", top);
  24. //test_InsertHeap
  25. InsertHeap(&hp, 5);
  26. //test_RemoveHeap
  27. RemoveHeap(&hp);
  28. DestroyHeap (&hp);
  29. }
  30. int main ()
  31. {
  32. test_Heap();
  33. return 0;
  34. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/688363
推荐阅读
相关标签
  

闽ICP备14008679号