java - Heap property of this array -


From the programming pearl, it is known that the array [1 ... n] is the HAP property If for everyone, 2 & lt; = I & lt; = Nx [i / 2] & lt; = X [i] .

This is my code:

Import Java.math. *;

  public class heap {public static zero main (string [] args) {int x [] = new int [] {12,20,15,29,23, 17,22,35 , 40,26,51,19}; (Int i = 2; i & lt; x.length; i ++) {if (x [Mathround (i / 2)]  

I used Math here because 4/2 and 5/2 are equal and = 2 when I compile this code, I'm shown in the last line That this is not a heap. Maybe because the index starts at 1 and we do not pay attention to Index 0, yes?

While you are on the right track, there are some key notes:

  • Every time around the loop, the code will print the "pile" or "no pile", as Moron has said.

    • Begin with true

    • starting with Boolean Try to false and break if the position of the stack is not found in repetition

    • then check the value on the variable

    • Start with 0, do not get re-running and back right (or print "found") with your loop (BTW, Java ARA based 0, not 1-based); The position of the stack applies to all nodes.

    • Get rid of that Math.round object

    • You can remove it in another method


Comments