赞
踩
这里 0 <= A.heap-size <= A.length
- PARENT(i)
- return i/2;
- LEFT(i)
- return 2*i;
- RIGHT(i)
- return 2*i+1;
(可用
宏定义实现)
在调用 MAX_HEAPIFY 的时候,我们假定根结点为 LEFT(i) 和 RIGHT(i) 的二叉树都是最大堆(这个是必须的假设。也就是说,在调整堆的某个结点 i 时,
必须保证 结点 i 的孩子结点都满足最大堆性质)
-
- MAX_HEAPIFY(A,i)
- l = LEFT(i); //获得左孩子结点
- r = RIGHT(A,i); //获得右孩子结点
- if l<=A.heap-size and A[l]>A[i] // 如果做孩子结点是堆元素,并且左孩子结点的关键值大于父结点的关键值
- largest = l; //记录结点关键字最大的下标
- else largest = i;
- if r<=A.heap-size and A[r] > A[largest]
- largest = r;
- if largest ≠ i
- exchange A[i] width A[largest]
- MAX_HEAPIFY(A,largest) // 调整堆
- BUILD_MAX_HEAP(A)
- A.heap-size = A.length
- for i = [A.length/2] downto 1
- MAX_HEAPIFY(A,i);
- HEAPSORT(A)
- BUILD_MAX_HEAP(A) // 建堆
- for i = A.length downto 2
- exchange A[1] with A[i]
- A.heap-size = A.heap-size - 1;
- MAX_HEAPIFY(A,1)
- EXTRACT_MAX(S)
- if A.heap-size < 1
- error "heap underflow"
- max = A[1]
- A[1] = A[A.heap-size]
- A.heap-size = A.heap-size - 1
- MAX_HEAPIFY(A,1)
- return max
-
-
- INCREASE_KEY(S,x,k)
- if key < A[i]
- error "new key is smaller than current key"
- A[i] = key
- while i > 1 and A[PARENT(i)] < A[i]
- exchange A[i] with A[PARENT(i)]
- i = PARENT(i);
-
-
- INSERT(S,x)
- A.heap-size = A.heap-size + 1;
- A[A.heap-size] = -无穷
- INCREASE_KEY(S,A.heap-size,x)
- #include "iostream"
-
- #define PARENT(i) (i/2)
- #define LEFT(i) (2*i)
- #define RIGHT(i) (2*i+1)
- #define Heapswap(a,b) (a = a+b, b=a-b, a= a-b)
-
- struct Heap {
- int A[256] = {0}; // 0位不要,下标从1开始
- unsigned length = 0; // 数组有效位数
- unsigned heapSize = 0; // 堆元素
- unsigned const MaxSize = 256; //数组容量
- };
-
- void InitialHeap(Heap& hp, int A[], int Arrlength, int HeapLength);//初始化堆
- void Print(Heap hp); // 打印 堆元素
- void MaxHeapify( Heap &hp, int i); // 维护堆的性质
- void BuildMaxHeap(Heap &hp); // 建堆
- void HeapSort(Heap &hp); // 堆排序算法
-
- int main(void)
- {
-
- #if 1 //测试
- Heap hp;
- int A[] = {9,10,18,1,12,6,9,11,13,2};
- //初始化
- InitialHeap(hp, A, 10, 0);
- //建堆
- BuildMaxHeap(hp);
- //输出数据
- Print(hp);
- std::cout << std::endl;
-
-
-
- //堆排序
- HeapSort(hp);
- for (size_t i = 1; i <= hp.length; i++)
- {
- std::cout << hp.A[i] << " ";
- }
- std::cout << std::endl;
-
- system("pause");
- #endif
- return 0;
- }
-
- void InitialHeap(Heap & hp, int A[], int Arrlength, int HeapLength)
- {
- for (int i = 0; i < Arrlength; i++)
- hp.A[i+1] = A[i];
- hp.length = Arrlength;
- hp.heapSize = HeapLength;
- }
-
- void Print(Heap hp)
- {
- for (size_t i = 1; i <= hp.heapSize; i++)
- {
- std::cout << hp.A[i] << " ";
- }
- }
-
- void MaxHeapify(Heap &hp, int i)
- {
- int left = LEFT(i);
- int right = RIGHT(i);
- int largest = -1;
- // 是堆元素并且是孩子结点大于父子结点
- if (left <= hp.heapSize && hp.A[left] > hp.A[i])
- largest = left;
- else
- largest = i;
- if (right <= hp.heapSize && hp.A[right] > hp.A[largest])
- largest = right;
-
- if (largest != i)
- {
- Heapswap(hp.A[largest], hp.A[i]);
-
- MaxHeapify(hp, largest);
- }
- }
-
- void BuildMaxHeap(Heap &hp)
- {
- hp.heapSize = hp.length;
- for (int i = hp.length / 2; i >= 1; i--)
- MaxHeapify(hp, i);
- }
-
- void HeapSort(Heap & hp)
- {
- BuildMaxHeap(hp); //建堆
- for (int i = hp.length; i >= 2; i--)
- {
- Heapswap(hp.A[1], hp.A[i]);
- hp.heapSize = hp.heapSize - 1;
- MaxHeapify(hp, 1);
- }
-
- }
-
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。