算法导论6.5-7习题解答

CLRS 6.5-7 :
HEAP-DELETE(A, i)操作将结点i中的项从堆A中删去。对含n个元素的最大堆,请给出时间为O(lgn)的HEAP-DELETE的实现。
算法思想:
对即将被删的i处的元素设置为堆中末尾的元素,同时堆的大小减1.
此题同时附带6.2-5的解答
代码如下:

  1 #include <iostream>
  2 using namespace std;
  3 //修正i的位置,在此处已经假设i的子节点都是堆
  4 static void max_heapify(int*&a, int i, int length);
  5 //建立数组的堆
  6 void build_max_heap(int*&a, int length);
  7 //利用堆对数组重新排序,总是拿第一个和最后一个对调,数组长度减一
  8 void heap_sort(int*&a, int length);
  9 //删除ith元素
 10 void heap_delete(int*&a, int i, int length);
 11 //插入x
 12 void heap_insert(int*&a, int x, int length);
 13 //将ith位置上的数增加到key
 14 void increase_key(int*&a, int i, int key);
 15 
 16 int main()
 17 {
 18     const int LEN =10;
 19     int b[10] = {12, 43, 0, -4, 98, 75, 64, 88, 5, 32};
 20     int* a = new int[LEN];
 21     for(int i =0; i < LEN; i++)
 22         a[i] = b[i];
 23     heap_sort(a, LEN);
 24     for(int i =0; i < LEN; i++)
 25         cout<<a[i]<<endl;
 26     cout<<"****"<<endl;
 27 
 28     increase_key(a, 7, 100);
 29     heap_sort(a, LEN);
 30     for(int i =0; i < LEN; i++)
 31         cout<<a[i]<<endl;
 32     cout<<"****"<<endl;
 33 
 34     heap_insert(a, 10, LEN);
 35     heap_sort(a, LEN + 1);
 36     for(int i =0; i < LEN + 1; i++)
 37         cout<<a[i]<<endl;
 38     cout<<"****"<<endl;
 39 
 40     heap_delete(a, 1, LEN + 1);
 41     heap_sort(a, LEN);
 42     for(int i =0; i < LEN; i++)
 43         cout<<a[i]<<endl;
 44     cout<<"****"<<endl;
 45     return 0;
 46 }
 47 
 48 static void max_heapify(int*&a, int i, int length)
 49 {
 50     int largest = i;
 51     while(largest <= length -1) {
 52         int left =2*largest +1;
 53         int right =2*largest +2;
 54         int temp = largest;
 55         if(left <= length -1&& a[left] > a[largest]) {
 56             largest = left;
 57         }
 58         if(right <= length -1&& a[right] > a[largest]) {
 59             largest = right;
 60         }
 61         if(largest != temp) {
 62             int exchange = a[largest];
 63             a[largest] = a[temp];
 64             a[temp] = exchange;
 65         } else {
 66             break;
 67         }
 68     }
 69 }
 70 
 71 void build_max_heap(int*&a, int length)
 72 {
 73     int root = length/2-1;
 74     for(int i = root; i >=0; i--)
 75         max_heapify(a, i, length);
 76 }
 77 
 78 void heap_sort(int*&a, int length)
 79 {
 80     build_max_heap(a, length);
 81     for(int i = length -1; i >=1; i--) {
 82         int temp = a[0];
 83         a[0] = a[i];
 84         a[i] = temp;
 85         max_heapify(a, 0, i);
 86     }
 87 }
 88 
 89 void heap_delete(int*&a, int i, int length)
 90 {
 91     if(i != length -1) {
 92         a[i] = a[length -1];
 93         max_heapify(a, i, length); 
 94     }
 95 }
 96 
 97 void heap_insert(int*&a, int x, int length)
 98 {
 99     int* temp = a;
100     a = new int[length +1];
101     for(int i =0; i < length; i++)
102         a[i] = temp[i];
103     delete temp;
104     //用-10000000代替负无穷
105     a[length] =-10000000;
106     increase_key(a, length, x);
107 }
108 
109 void increase_key(int*&a, int i, int key)
110 {
111     if(a[i] > key) {
112         cout<<"key should be larger than a[i]"<<endl;
113     } else {
114         int parent = (i -1)/2;
115         a[i] = key;
116         while(parent >= 0 && a[parent] < key) {
117             int temp = a[parent];
118             a[parent] = key;
119             a[i] = temp;
120             i = parent;
121             parent = (i -1)/2;
122         }
123     }
124 }

 

posted on 2011-03-20 20:06  NULL00  阅读(1425)  评论(0编辑  收藏  举报

导航