利用上一節的最大堆,中間省略了很多該刪和修改的東西,總是時學習演算法嗎~~~~~懶了一些啊,o(∩_∩)o
#include
#include
struct heap;
int paraent(int i);
int leftchild(int i);
int rightchild(int i);
void max_heap(int data,int i,int len);
void bulid_max_heap(int data,int len);
void heap_sort(int data,int len);
int heap_max(struct heap *m_heap);
void printff(struct heap *m_heap);
void insert(struct heap *m_heap,int data);
int main()
; //建立10個資料,測試
bulid_max_heap(ndata,10);
struct heap *m_heap=(struct heap *)malloc(sizeof(struct heap));
m_heap->data=ndata;
m_heap->size=10;
for (int i = 0; i < 10; ++i)
int temp=heap_max(m_heap);
printf("%d ", temp);
printf("/n");
printff(m_heap);
printf("/n");
temp=heap_max(m_heap);
printf("%d ", temp);
printf("/n");
printff(m_heap);
printf("/n");
insert(m_heap,13);
insert(m_heap,7);
printff(m_heap);
printf("/n");
temp=heap_max(m_heap);
printf("%d ", temp);
printf("/n");
printff(m_heap);
printf("/n");
system("puase");
return 0;
}void printff(struct heap *m_heap)
int leftchild(int i)
int rightchild(int i)
void max_heap(int data,int i,int len)
else
return;
max_heap(data,largest,len);
}void bulid_max_heap(int data,int len)
int heap_max(struct heap *m_heap)
void insert(struct heap *m_heap,int data)
}
優先佇列《堆》
1.模型 兩個基本操作 insert等價enqueue deletemin刪除最小者 dequeue 2.簡單的實現 1 簡單鍊錶 遍歷刪除min或者排序刪除min 2 使用二叉查詢樹。反覆除去min會使得樹不平衡,並且bst還支援許多不需要的操作。3.二叉堆 優先佇列的實現普遍使用二叉堆,堆有兩個...
優先佇列 堆
印表機列印作業一般是放在佇列中的。如果按照先來先列印的順序,有乙個100頁的列印任務,那麼會讓後面短小的任務等待很長時間。更合理的做法也許是最後處理最耗時的列印任務,不管它是不是最後提交上來的。在多使用者作業系統中,作業系統讓哪個程式使用cpu,是需要決定從佇列裡面選擇的。一般做法是從隊頭獲得程式,...
優先佇列 堆
優先佇列 佇列是乙個操作受限的線性表,資料只能在一端進入,另一端出來,具有先進先出的性質。有時在佇列中需要處理優先順序的情況,即後面進入的資料需要提前出來,這裡就需要優先佇列。優先佇列是至少能夠提供插入和刪除最小值這兩種操作的資料結構。對應於佇列的操作,插入相當於入隊,刪除最小相當於出隊。鍊錶,二叉...