堆的一些基本操作實現

2021-10-04 06:23:14 字數 863 閱讀 3475

//關於建堆和對其的一些操作

#include

#include

using

namespace std;

const

int maxn=

100;

int heap[maxn]

,n=10

;//將第乙個結點儲存在陣列的一號位

//函式功能:對heap陣列在low到high的範圍內向下調整

void

downadjust

(int low,

int high)

if(heap[j]

)else}}

//建堆 時間複雜度為o(n)

void

createheap()

}//刪除堆頂元素,時間複雜度為log(n)

void

deletetop()

//刪除任意的元素

void

deleterandom

(int i)

//如果想要新增元素,可以把元素放在陣列尾部,不斷和父節點比較,直到比父節點大或者到達根結點

//這裡需要向上調整

void

upadjust

(int low,

int high)

else}}

//新增元素

void

insert

(int x)

void

printh()

cout<}void

heapsort()

//排完序之後陣列就是從大到小排列的了

}int

main()

堆相關的一些操作

include includeusing namespace std const int maxn 100 heap為堆,n為元素個數 int heap maxn n 10 對heap陣列在 low,high 範圍進行向下調整 其中low為欲調整結點的陣列下標,high一般為堆的最後乙個元素的陣列下...

堆的一些簡單操作

僅僅是記錄一下自己寫過的堆操作的 個人覺得堆的優勢在於建立和獲取最小或者最大元的方便,有任何次序要求的演算法,堆並不合適。include include include typedef struct priorityqueue priorityqueue void printpriorityqueu...

linux的一些基本操作

bash的基本操作 ctrl c 結束當前程序 ctrl z 暫停當前程序,放入後台 l 清屏 s 停止鍵盤響應 q恢復 d 標準輸入結束 r 搜尋歷史命令 linux自啟動命令 etc rc.d rc.local裡面新增 會對所有的使用者起作用 bashrc對單個使用者起作用 qt開發 1 ui設...