用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.5 KiB

  1. // 20:50
  2. // 22:31
  3. #include <iostream>
  4. const int N = 1000;
  5. class BigHeap {
  6. private:
  7. int *_internal_array;
  8. int len = 0;
  9. bool external = false;
  10. int get_left_child(int index) {
  11. return this->_internal_array[index * 2];
  12. }
  13. int get_right_child(int index) {
  14. return this->_internal_array[index * 2 + 1];
  15. }
  16. int get_parent(int index) {
  17. return this->_internal_array[index / 2];
  18. }
  19. void adjust_up(int index) {
  20. while (index > 1 && this->_internal_array[index] > this->get_parent(index)) {
  21. std::swap(this->_internal_array[index], this->_internal_array[index / 2]);
  22. index /= 2;
  23. }
  24. }
  25. void adjust_down(int index) {
  26. while (index * 2 <= this->len) {
  27. int target = index;
  28. if (this->_internal_array[target] < this->get_left_child(index)) // 如果左节点更大
  29. target = index * 2; // 左节点换上来
  30. if (index * 2 + 1 <= this->len && this->_internal_array[target] < this->get_right_child(index)) // 如果右节点更大
  31. target = index * 2 + 1; // 右节点换上来
  32. if (index == target)
  33. break;
  34. // 如果满足了当前节点比左右子节点大,就不用继续下沉了
  35. std::swap(this->_internal_array[index], this->_internal_array[target]);
  36. index = target;
  37. }
  38. }
  39. // 好像不太对,算了不用了,叶子节点直接判断就行了
  40. int get_last_non_leaf_index(int len) {
  41. int pow_of_2 = 1;
  42. int times = 0;
  43. while (pow_of_2 < len) {
  44. pow_of_2 *= 2; // 也可以写成 pow_of_2 <<= 2;
  45. times++;
  46. }
  47. return times;
  48. }
  49. public:
  50. BigHeap() { // 构造函数,在初始化时被调用
  51. this->_internal_array = new int[N];
  52. }
  53. BigHeap(int *external_array, int len) { // 也是构造函数,但是使用外部的数组初始化,数组要从1开始
  54. this->_internal_array = external_array;
  55. this->len = len;
  56. for (int i = this->len; i >= 1; i--) {
  57. adjust_down(i);
  58. }
  59. this->external = true;
  60. }
  61. ~BigHeap() { // 析构函数,在对象被删除时被调用
  62. if (!this->external)
  63. delete this->_internal_array;
  64. }
  65. int get_length() {
  66. return this->len;
  67. }
  68. void insert(int num) {
  69. this->_internal_array[++this->len] = num;
  70. int index = this->len;
  71. adjust_up(index);
  72. }
  73. int pop() {
  74. int result = this->_internal_array[1];
  75. this->_internal_array[1] = this->_internal_array[this->len--];
  76. adjust_down(1);
  77. return result;
  78. }
  79. };
  80. int a[N];
  81. int len;
  82. int main() {
  83. std::cin >> len;
  84. for (int i = 1; i <= len; i++)
  85. std::cin >> a[i];
  86. BigHeap big_heap_internal;
  87. for (int i = 1; i <= len; i++) {
  88. big_heap_internal.insert(a[i]);
  89. }
  90. std::cout << "逐个插入后,逐个弹出输出的结果为:" << std::endl;
  91. while (big_heap_internal.get_length() > 0) {
  92. std::cout << big_heap_internal.pop() << " ";
  93. }
  94. std::cout << std::endl;
  95. BigHeap big_heap_external(a, len);
  96. std::cout << "一次性初始化后,逐个弹出输出的结果为:" << std::endl;
  97. while (big_heap_external.get_length() > 0) {
  98. std::cout << big_heap_external.pop() << " ";
  99. }
  100. std::cout << std::endl;
  101. return 0;
  102. }