Java語(yǔ)言實(shí)現(xiàn)二叉堆的打印代碼分享
二叉堆是一種特殊的堆,二叉堆是完全二元樹(shù)(二叉樹(shù))或者是近似完全二元樹(shù)(二叉樹(shù))。二叉堆有兩種:最大堆和最小堆。最大堆:父結(jié)點(diǎn)的鍵值總是大于或等于任何一個(gè)子節(jié)點(diǎn)的鍵值;最小堆:父結(jié)點(diǎn)的鍵值總是小于或等于任何一個(gè)子節(jié)點(diǎn)的鍵值。
打印二叉堆:利用層級(jí)關(guān)系
我這里是先將堆排序,然后在sort里執(zhí)行了打印堆的方法printAsTree()
public class MaxHeap<T extends Comparable<? super T>> { private T[] data; private int size; private int capacity; public MaxHeap(int capacity) { this.capacity = capacity; this.size = 0; this.data = (T[]) new Comparable[capacity + 1]; } public MaxHeap(T[] arr) {//heapify,數(shù)組建堆 capacity = arr.length; data = (T[]) new Comparable[capacity + 1]; System.arraycopy(arr, 0, data, 1, arr.length); size = arr.length; for (int i = size / 2; i >= 1; i--) { shiftDown(i); } } public int size() { return this.size; } public int getCapacity() { return this.capacity; } public boolean isEmpty() { return size == 0; } public T seekMax() { return data[1]; } public void swap(int i, int j) { if (i != j) { T temp = data[i]; data[i] = data[j]; data[j] = temp; } } public void insert(T item) { size++; data[size] = item; shiftUp(size); } public T popMax() { swap(1, size--); shiftDown(1); return data[size + 1]; } public void shiftUp(int child) { while (child > 1 && data[child].compareTo(data[child / 2]) > 0) { swap(child, child / 2); child /= 2; } } /** * @param a data數(shù)組中某個(gè)元素的下角標(biāo) * @param b data數(shù)組中某個(gè)元素的下角標(biāo) * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo) */ private int max(int a, int b) { if (data[a].compareTo(data[b]) < 0) {//如果data[b]大 return b;//返回b } else {//如果data[a]大 return a;//返回a } } /** * @param a data數(shù)組中某個(gè)元素的下角標(biāo) * @param b data數(shù)組中某個(gè)元素的下角標(biāo) * @param c data數(shù)組中某個(gè)元素的下角標(biāo) * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo) */ private int max(int a, int b, int c) { int biggest = max(a, b); biggest = max(biggest, c); return biggest; } public void shiftDown(int father) { while (true) { int lchild = father * 2; int rchild = father * 2 + 1; int newFather = father;//這里賦不賦值無(wú)所謂,如果把下面這個(gè)return改成break,那就必須賦值了 if (lchild > size) {//如果沒(méi)有左、右孩子 return; } else if (rchild > size) {//如果沒(méi)有右孩子 newFather = max(father, lchild); } else {//如果有左、右孩子 newFather = max(father, lchild, rchild); } if (newFather == father) {//如果原父結(jié)點(diǎn)就是三者最大,則不用繼續(xù)整理堆了 return; } else {//父節(jié)點(diǎn)不是最大,則把大的孩子交換上來(lái),然后繼續(xù)往下堆調(diào)整,直到滿(mǎn)足大根堆為止 swap(newFather, father); father = newFather;//相當(dāng)于繼續(xù)shiftDown(newFather)。假如newFather原來(lái)是father的左孩子,那就相當(dāng)于shiftDown(2*father) } } } public static <T extends Comparable<? super T>> void sort(T[] arr) { int len = arr.length; MaxHeap<T> maxHeap = new MaxHeap<>(arr); maxHeap.printAsTree(); for (int i = len - 1; i >= 0; i--) { arr[i] = maxHeap.popMax(); } } public static void printArr(Object[] arr) { for (Object o : arr) { System.out.print(o); System.out.print("\t"); } System.out.println(); } public void printSpace(int n) {//打印n個(gè)空格(在這里用‘\t'來(lái)代替) for (int i = 0; i < n; i++) { System.out.printf("%3s", ""); } } public void printAsTree() { int lineNum = 1;//首先遍歷第一行 int lines = (int) (Math.log(size) / Math.log(2)) + 1;//lines是堆的層數(shù) int spaceNum = (int) (Math.pow(2, lines) - 1); for (int i = 1; i <= size; ) { //因?yàn)樵赱1...size]左閉右閉區(qū)間存數(shù)據(jù),data[0]不存數(shù)據(jù) //每層都是打印這個(gè)區(qū)間[2^(層數(shù)-1) ... (2^層數(shù))-1]。如果堆里的數(shù)不夠(2^層數(shù))-1個(gè),那就打印到size。所以取min((2^層數(shù))-1,size). for (int j = (int) Math.pow(2, lineNum - 1); j <= Math.min(size, (int) Math.pow(2, lineNum) - 1); j++) { printSpace(spaceNum); //打印spaceNum個(gè)空格 System.out.printf("%3s", data[j]);//打印數(shù)據(jù) System.out.printf("%3s", "");//圖片中綠色方框 printSpace(spaceNum);//打印spaceNum個(gè)空格 i++;//每打印一個(gè)元素就 + 1 } lineNum++; spaceNum = spaceNum / 2; System.out.println(); } } public static void main(String args[]) { Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6, 1, 3, 6, 1, 1}; sort(arr); } }
執(zhí)行結(jié)果:
總結(jié)
以上就是本文關(guān)于Java語(yǔ)言實(shí)現(xiàn)二叉堆的打印代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
java后臺(tái)實(shí)現(xiàn)支付寶支付接口和支付寶訂單查詢(xún)接口(前端為APP)
這篇文章主要介紹了java后臺(tái)實(shí)現(xiàn)支付寶支付接口和支付寶訂單查詢(xún)接口(前端為APP),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-08-08SpringBoot項(xiàng)目如何設(shè)置權(quán)限攔截器和過(guò)濾器
這篇文章主要介紹了使用lombok時(shí)如何自定義get、set方法問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Spring中ApplicationContextAware的使用方法詳解
ApplicationContextAware?通過(guò)它Spring容器會(huì)自動(dòng)把上下文環(huán)境對(duì)象調(diào)用ApplicationContextAware接口中的setApplicationContext方法,這篇文章主要介紹了Spring中ApplicationContextAware的作用,需要的朋友可以參考下2023-03-03SpringBoot多級(jí)緩存實(shí)現(xiàn)方案總結(jié)
所謂多級(jí)緩存,是指在整個(gè)系統(tǒng)架構(gòu)的不同系統(tǒng)層面進(jìn)行數(shù)據(jù)緩存,以提升訪問(wèn)速度,多級(jí)緩存就是為了解決項(xiàng)目服務(wù)中單一緩存使用不足的缺點(diǎn),本文我們將給大家總結(jié)了SpringBoot多級(jí)緩存實(shí)現(xiàn)方案,需要的朋友可以參考下2023-08-08SpringBoot應(yīng)用快速部署到K8S的詳細(xì)教程
這篇文章主要介紹了SpringBoot應(yīng)用快速部署到K8S的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12java實(shí)現(xiàn)短地址服務(wù)的方法(附代碼)
大多數(shù)情況下URL太長(zhǎng),字符多,不便于發(fā)布復(fù)制和存儲(chǔ),本文就介紹了通過(guò)java實(shí)現(xiàn)短地址服務(wù),減少了許多使用太長(zhǎng)URL帶來(lái)的不便,需要的朋友可以參考下2015-07-07