Print the binary heap (implemented in Java) and print the java

Source: Internet
Author: User

Print the binary heap (implemented in Java) and print the java
Print binary heap: using hierarchical relationships

 

I sorted the heap first, and then executed the print heap method in sort.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, number set heap 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 array the cursor mark of an element * @ param B data array * @ Return indicates the maximum value of an element. */private int max (int a, int B) {if (data [a]. compareTo (data [B]) <0) {// If data [B] Big return B; // return B} else {// If data [a] Big return; // return a}/*** @ param a data array. The cursor mark of an element * @ param B data array * @ param c data array * @ return indicates the maximum value of an element. */private int max (int, int B, int c) {int biggest = max (a, B); biggest = max (biggest, c); return biggest;} publ Ic void shiftDown (int father) {while (true) {int lchild = father * 2; int rchild = father * 2 + 1; int newFather = father; // It doesn't matter if the value is assigned here. if you change the return value to break, you must assign the value if (lchild> size) {// if there is no left or right child return ;} else if (rchild> size) {// if there is no right child newFather = max (father, lchild);} else {// if there is left or right child newFather = max (father, lchild, rchild);} if (newFather = father) {// if the original parent node is the maximum of the three nodes, you do not need to refresh the heap return;} el Se {// if the parent node is not the largest, swap the big child up and continue the heap adjustment until swap (newFather, father) is satisfied; father = newFather; // continue shiftDown (newFather ). If newFather is a left child of father, it is equivalent to 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) {// print n spaces (replace '\ t' here) for (int I = 0; I <n; I ++) {System. out. printf ("% 3 s", "") ;}} public void printAsTree () {int lineNum = 1; // first traverse the first line of int lines = (int) (Math. log (size)/Math. log (2) + 1; // lines is the number of heap layers int spaceNum = (int) (Math. pow (2, lines)-1); for (int I = 1; I <= size;) {// because in [1... size] data is stored in the left closed right closed interval, and data [0] does not store data // This interval is printed on each layer [2 ^ (layers-1 )... (2 ^ layers)-1]. If the number in the heap is not enough (2 ^ layers)-1, print it to the size. Therefore, min (2 ^ layers)-1, size) is used ). for (int j = (int) Math. pow (2, lineNum-1); j <= Math. min (size, (int) Math. pow (2, lineNum)-1); j ++) {printSpace (spaceNum); // print spaceNum space System. out. printf ("% 3 s", data [j]); // print the data System. out. printf ("% 3 s", ""); // The Green Box printSpace (spaceNum) in the image; // print spaceNum space I ++; // + 1} lineNum ++ for each printed element; 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 );}}

Execution result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.