Posted in C/C++ 3 条留言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
#define INCCAPACITY 100  
 
typedef int ElemType;
struct heap_operation; //前向声明
 
typedef struct Heap    //堆结构
{
	int capacity;  //容量
	int size;      //现有元素数量
	ElemType *data;  //指针,指向数组
        struct heap_operation *heap_op; // 操作函数
}Heap;
 
typedef struct heap_operation  //里面有一堆函数指针,类似于C++的虚表
{
	void (*init_heap)(Heap *heap, ElemType *_data, int capacity, int size);
	void (*build_heap)(Heap *heap);
	ElemType (*get_min)(Heap *heap);
	ElemType (*delete_min)(Heap *heap);
	void (*insert)(Heap *heap, ElemType e);
}heap_operation;
 
int left_child(int index)  //得到一个元素的左孩子下标
{
	return 2*index+1;
}
 
int right_child(int index) //得到一个元素的右孩子下标
{
	return 2*index+2;
}
 
int parent(int index)   //得到一个元素的父结点下标
{
	return (index-1)/2;
}
 
// 将元素下沉,这是堆操作的核心。
void filter_down(Heap *heap, int index)
{
	ElemType ele = heap->data[index];
	int i = index;
	int child=0;
	for(i=index; i*2+1 <= heap->size-1; i=child)
	{
		child = 2*i + 1;
		if(child < heap->size-1 && heap->data[child] > heap->data[child+1])
			child++;
		if(heap->data[child] < ele)
			heap->data[i] = heap->data[child];
		else
			break;
	}
	heap->data[i] = ele;
}
 
//初始化堆
void init_heap(Heap *heap, ElemType *_data, int capacity, int size)
{
	heap->capacity = capacity;
	heap->size = size;
	heap->data = _data;
}
 
 
// 建堆
void build_heap(Heap *heap)
{
	int i = parent(heap->size - 1);
	while(i>=0)
	{
		filter_down(heap, i);
		i --;
	}
}
 
ElemType get_min(Heap *heap)
{
	return heap->data[0];
}
 
void resizeheap(Heap *heap)
{
	ElemType * p;
	p = (ElemType *)malloc((heap->capacity+INCCAPACITY)*sizeof(ElemType));
	if(p==NULL)
        {
		printf("ERROR at line %d\n", __LINE__);
		exit(0);
	}
	memcpy(p, heap->data, sizeof(ElemType)*heap->capacity);
	heap->capacity += INCCAPACITY;
	free(heap->data);
	heap->data = p;
}
 
// 插入一个元素
void insert(Heap *heap, ElemType e)
{
	int i = 0;
	if(heap->size == heap->capacity) 
		resizeheap(heap);
	heap->data[heap->size] = e;
	heap->size++;
	i  = heap->size - 1;
	while(parent(i)>=0)
	{
		if(heap->data[parent(i)]>e)
		{
			heap->data[i] = heap->data[parent(i)];
			i = parent(i);
		}
		else
			break;
	}
	heap->data[i] = e;
}
 
ElemType delete_min(Heap *heap)
{
	ElemType i = heap->data[0];
        heap->data[0] = heap->data[ heap->size-1 ];
	heap->size --;
 
	filter_down(heap, 0);
	return i;
}
 
void print_array(ElemType array[], int n)
{
	int i = 0;
	while(i<n)
	{
		printf("%d ", array[i++]);
	}
	printf("\n");
}
 
void print_heap(Heap *heap)
{
	print_array(heap->data, heap->size);
}
 
int main()
{
	ElemType e;
        ElemType a[] = {30, 6, 7, 4, 5, 1, 6, 1};
	Heap aheap;
 
	heap_operation heap_op;
	heap_op.init_heap = init_heap; 
        heap_op.build_heap = build_heap;
	heap_op.get_min = get_min;
	heap_op.delete_min = delete_min;
        heap_op.insert = insert;
 
	aheap.heap_op = &heap_op;
 
	aheap.heap_op->init_heap(&aheap, a, sizeof(a)/sizeof(a[0]), sizeof(a)/sizeof(a[0]));
	aheap.heap_op->build_heap(&aheap);
 
	print_heap(&aheap);
 
	printf("min of the array %d\n", aheap.heap_op->get_min(&aheap));
        e = aheap.heap_op->delete_min(&aheap);	
	print_heap(&aheap);
 
	aheap.heap_op->insert(&aheap, e);
	print_heap(&aheap);
 
 
	return 0;
}
August 24, 2010