A method displaying the heap in proper triangle form for debugging: //////////// DISPLAY HEAP BY ROWS public void displayTree() { // These variables are used to keep track when a new row starts int currentRow; int previousRow = 1; // These variables are used for the horizontal alignment. // numRows is needed since the initial indentation depends // on how high the heap is. int numRows = (int) (Math.log(size + 1) / Math.log(2)); int indent = 0; int padding = 0; // THIS OFFSET VARIABLE HAS TO BE THE SAME AS THE AMOUNT OF LEADING ZEROS // IN THE PRINTF STATEMENT BELOW!!! int offset = 2; // Calculate the indent of first row: // - indent doubles by every row. // - offset = length of the actual printed values for (int i = 0; i < numRows; i++) { indent = (indent * 2) + offset; } // Print the indent for the first row for (int i = 0; i < indent; i++) { System.out.print(" "); } // Begin the main loop for (int index = 0; index < size; index++) { // Calculate currentRow based on the current index. // Original formula from professor Rob... // ... division by log(2) makes the logarithm base2 no matter // what original base is.. That's just a mathematical fact // which you should remember without googling! currentRow = 1 + (int) (Math.log(index + 1) / Math.log(2)); // Change row if (currentRow > previousRow) { System.out.println(); previousRow = currentRow; // Calculate new indentation and padding padding = indent; indent = (indent - offset) / 2; // print indentation for (int i = 0; i < indent; i++) { System.out.print(" "); } } // Print the actual value, leading zero helps with alignment. // THE OFFSET VARIABLE ABOVE HAS TO BE THE SAME AS // THE AMOUNT OF LEADING ZEROS IN THE PRINTF STATEMENT. System.out.printf("%02d", array[index]); // Print padding between values, padding is same as indentation of the previous row. // Had to fight the urge to ninja the padding variable out and use (indentation * 2) + offset // as the loop conditional. for (int i = 0; i < padding; i++) { System.out.print(" "); } } System.out.println(" "); }
if we are incrementing the position before adding the object to our array, wouldn't array[parent] == null when using comparator on root node? since there is nothing stored at array[0]? Math.floor(position(1) - 1.0) / 2.0)) gives us 0.
If value of node#1 was 27, it would cause heap violation after 25 "trickles up" to root node. how is that handled? just one more "trickle down" op on the left side?
Amazing thank you Dr.
🙂
At 12:22 its array[parent] not array.parrent
A method displaying the heap in proper triangle form for debugging:
//////////// DISPLAY HEAP BY ROWS
public void displayTree() {
// These variables are used to keep track when a new row starts
int currentRow;
int previousRow = 1;
// These variables are used for the horizontal alignment.
// numRows is needed since the initial indentation depends
// on how high the heap is.
int numRows = (int) (Math.log(size + 1) / Math.log(2));
int indent = 0;
int padding = 0;
// THIS OFFSET VARIABLE HAS TO BE THE SAME AS THE AMOUNT OF LEADING ZEROS
// IN THE PRINTF STATEMENT BELOW!!!
int offset = 2;
// Calculate the indent of first row:
// - indent doubles by every row.
// - offset = length of the actual printed values
for (int i = 0; i < numRows; i++) {
indent = (indent * 2) + offset;
}
// Print the indent for the first row
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
// Begin the main loop
for (int index = 0; index < size; index++) {
// Calculate currentRow based on the current index.
// Original formula from professor Rob...
// ... division by log(2) makes the logarithm base2 no matter
// what original base is.. That's just a mathematical fact
// which you should remember without googling!
currentRow = 1 + (int) (Math.log(index + 1) / Math.log(2));
// Change row
if (currentRow > previousRow) {
System.out.println();
previousRow = currentRow;
// Calculate new indentation and padding
padding = indent;
indent = (indent - offset) / 2;
// print indentation
for (int i = 0; i < indent; i++) {
System.out.print(" ");
}
}
// Print the actual value, leading zero helps with alignment.
// THE OFFSET VARIABLE ABOVE HAS TO BE THE SAME AS
// THE AMOUNT OF LEADING ZEROS IN THE PRINTF STATEMENT.
System.out.printf("%02d", array[index]);
// Print padding between values, padding is same as indentation of the previous row.
// Had to fight the urge to ninja the padding variable out and use (indentation * 2) + offset
// as the loop conditional.
for (int i = 0; i < padding; i++) {
System.out.print(" ");
}
}
System.out.println("
");
}
Works with Integer values 0 - 99
in trickle_up parent is not a method available on the array instance, it is an indice, it's meant to be called with brackets I think
use of clone jutsu in real life. amazing..
doesn't it miss a line for updating the position? especially for the case where we are testing at the root
if we are incrementing the position before adding the object to our array, wouldn't array[parent] == null when using comparator on root node? since there is nothing stored at array[0]? Math.floor(position(1) - 1.0) / 2.0)) gives us 0.
If value of node#1 was 27, it would cause heap violation after 25 "trickles up" to root node. how is that handled? just one more "trickle down" op on the left side?
It would not trickle up if root was 27 because its bigger than 25, it only trickles up if parent is smaller.
Is Math.floor((position - 1)/ 2) the sam as Math.ceil((position/2)-1) ?
You can do cool simultanious videos playing and making one of them wither away but still cant handle those black screens...