[問題] 如何做出標準的 堆積樹 ?

看板C_and_CPP (C/C++)作者 (大笨羊)時間9年前 (2016/11/18 10:00), 編輯推噓2(205)
留言7則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win10 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 小弟想寫一個堆積樹...可是我覺得我的方法好像很拙劣 因為仔細看了一下 發現好像是寫成了 選擇排序 =.= O(n^2) 產生的有點慢 以下是小弟的程式碼: void myHeap(int data[], int size) { int point_Top = 0; int nav_pointer; while (point_Top < size) { for (nav_pointer =point_Top+1; nav_pointer < size; nav_pointer++) { if (data[point_Top] < data[nav_pointer]) { swap(&data[point_Top], &data[nav_pointer]); } } point_Top++; } } ----------------------下面是書上教的(我看不懂 囧)----------------------------- void cheap(int data[], int b, int bound) { while(b<bound/2){ int cnode = b * 2 + 1; if (cnode + 1 < bound) { if (data[cnode] < data[cnode + 1]) cnode++; } if (data[b] < data[cnode]) { swap(&data[b],&data[cnode]); } int i; for (i = 0; i < bound; i++) { printf("%d,",data[i]); } printf("\n"); b = cnode; } ----------------------------------------------------------------------------- 感謝大家>< 餵入的資料(Input):預期的正確結果(Expected Output):錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.195.157 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1479434410.A.443.html

11/18 22:04, , 1F
...heap =\= heap sort
11/18 22:04, 1F

11/18 22:05, , 2F
書上用的是用array 存一個tree 你先研究一下怎麼用arra
11/18 22:05, 2F

11/18 22:05, , 3F
y存binary tree
11/18 22:05, 3F

11/19 12:41, , 4F
先拿一張紙和一隻筆畫一個binary complete tree 然後從
11/19 12:41, 4F

11/19 12:41, , 5F
頂點開始依序往下編號 去觀察編號間的關係
11/19 12:41, 5F

11/20 19:27, , 6F
好的 謝謝大家
11/20 19:27, 6F

11/21 23:59, , 7F
關鍵在父子的index
11/21 23:59, 7F
文章代碼(AID): #1OBc2gH3 (C_and_CPP)
文章代碼(AID): #1OBc2gH3 (C_and_CPP)