Print binary heap (Java implementation)

Source: Internet
Author: User
Tags comparable pow

Print binary heap: Using Hierarchical relationships

I am here to sort the heap first and then execute the print heap 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 of build heaps 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.) {while (Child > 1 && data[child].compareto (DATA[CHILD/2]) > 0)            {Swap (child, CHILD/2);        Child/= 2;  }}/** * @param the lower corner of an element in the data array * @param the lower corner of an element in the B data array * @return which element is large returns the lower corner of the label */private int max (int a, int b) {if (Data[a].compareto (Data[b]) < 0) {//if DATA[B] Big return b;//returns B} el SE {//if DATA[A] big return a;//Returns a}}/** * @param the lower corner of an element in the data array * @param the element in the B data array Subscript * @param the lower corner of an element in the C data array * @return which element returns the lower corner of the index * */private int max (int A, int b, int c) {in        T biggest = Max (A, b);        Biggest = Max (biggest, c);    Return biggest; } public void Shiftdown (int father) {while (true) {intLchild = Father * 2;            int rchild = Father * 2 + 1;                int newfather = father;//Here does not matter, if you change the following return to break, it must be assigned the IF (Lchild > Size) {//If there is no left and right child            Return            } else if (Rchild > Size) {//If there is no right child Newfather = max (father, Lchild);            } else {//If there are left and right children Newfather = max (father, Lchild, rchild);            if (Newfather = = father) {//If the original parent node is the largest of the three, then do not continue to tidy up the heap return;                The else {//parent node is not the largest, then swap the big child up, then continue the heap adjustment until the big root heap is satisfied (Newfather, father); Father = newfather;//is equivalent to continuing Shiftdown (Newfather). If Newfather turned out to be father's left child, it would be 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 (); The public void printspace (int n) {//Prints n spaces (where ' \ t ' is substituted) for (int i = 0; i < n; i++) {SYSTEM.OUT.PR        intf ("%3s", "" "); }} public void Printastree () {int linenum = 1;//First Traverse first line int lines = (int) (Math.log (size)/Math.log        (2) + 1;//lines is the number of layers of the heap int spacenum = (int) (Math.pow (2, lines)-1); for (int i = 1; i <= size;) {//because in [1...size] left closed right closed interval data, data[0] no data//each layer is printed this interval [2^ (Layer-1) ... (2^ layers)-1]. If the number in the heap is not enough (2^ layers)-1, then print to size.            So take min ((2^ layer) -1,size).                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 ("%3s", Data[j]);//print Data System.out.printf ("%3s", "" ");//Picture green Box Printspace (spacenum);//print spacenum a space i++;//1 per element printed            } 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); }}

Execution Result:

Print binary heap (Java implementation)

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.