包括最大堆的建立,刪除,插入操作:
**如下:
#include
#include
#include
using
namespace std;
typedef
struct hnode* heap;
//堆的型別定義
struct hnode
;typedef heap maxheap;
//最大堆
#define inf 0x3f3f3f3f
#define error -1
maxheap creatmaxheap
(int maxsize)
bool
isempty
(maxheap h)
bool
isfull
(maxheap h)
void
percdown
(maxheap h,
int p)
h->data[parent]
=x;}
void
buildheap
(maxheap h)
intdeleteheap
(maxheap h)
maxitem=h-
>data[1]
; x=h-
>data[h-
>size--];
for(parent=
1;parent*
2<=h-
>size;parent=child)
h->data[parent]
=x;return maxitem;
}bool
insert
(maxheap h,
int x)
i=++h-
>size;
//i指向插入後堆的最後乙個元素的位置
for(
;h->data[i/2]
) h-
>data[i]
=h->data[i/2]
;//上濾x
h->data[i]
=x;return
true;}
intmain()
主函式是我隨便測試的,可以根據題目要求不同進行相應的變化。 最大堆 插入和刪除操作
最大堆是一棵完全二叉樹,也是一棵最大樹 即對於每個結點,其關鍵字的值不小於兒子結點的關鍵字值 一.堆的儲存結構 typedef struct element element heap max elements intn 0 二.在堆中插入元素 void element copy element ps...
最大堆的刪除
include include typedef struct heapstruct maxheap maxheap create int maxsize 堆的插入 void insert maxheap h,elementtype item i h size for h elements i 2 h...
最小最大堆
include include include include define max size 100 define swap x,y,t t x x y y t typedef struct element element heap max size void min max insert ele...