algorithm - When building heap, is heap unique? -


i'm studying heap & heap sorting.
there array : arr[8] = {6,9,3,1,8,7,2,11}
when i'm trying build heap, using code , pencil, faced 2 kinds of heap.

when using code, maxheap : 11 9 7 6 8 3 2 1

when using insertion theory, maxheap : 11 9 7 8 6 3 2 1


code i'm using :

int[] doheapsort(int[] value) {     int length = value.length;      (int = length / 2; > 0; i--) {         maxheapify(value, i, length);     }      //print heap     for(int = 0 ; i<value.length; i++)         system.out.println(value[i]);      return (value); }   void maxheapify(int[] array, int index, int heapsize) {     int left = index * 2;     int right = left + 1;     int max = index;      if (left <= heapsize && array[left - 1] > array[index - 1]) {         max = left;     }      if (right <= heapsize && array[right - 1] > array[max - 1]) {         max = right;     }      if (max != index) {         swap(array, index - 1, max - 1);         maxheapify(array, max, heapsize);     } } 

theory, in case, create array heap , insert 6 11 in order. (on other hand, code in-place heap)

both maxheap results satisfied heap definition. heap not unique ? thanks

that's correct. heap constraint (which children not greater parents) not specify heap, there more 1 possible arrangement.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -