Heaps 3 TrickleUp

Поділитися
Вставка
  • Опубліковано 1 січ 2025

КОМЕНТАРІ •

  • @sharpEarth
    @sharpEarth 3 місяці тому

    Amazing thank you Dr.
    🙂

  • @TZCoder
    @TZCoder 5 років тому +8

    At 12:22 its array[parent] not array.parrent

  • @tohtorizorro
    @tohtorizorro 6 років тому +4

    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("
    ");
    }

    • @tohtorizorro
      @tohtorizorro 6 років тому

      Works with Integer values 0 - 99

  • @davidgeismar6531
    @davidgeismar6531 5 років тому

    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

  • @gyatso_fam
    @gyatso_fam 5 років тому +3

    use of clone jutsu in real life. amazing..

  • @bouzidhamza3494
    @bouzidhamza3494 4 роки тому +1

    doesn't it miss a line for updating the position? especially for the case where we are testing at the root

  • @0Megaman0
    @0Megaman0 Рік тому

    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.

  • @usa-ca
    @usa-ca 6 років тому +1

    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?

    • @TZCoder
      @TZCoder 5 років тому

      It would not trickle up if root was 27 because its bigger than 25, it only trickles up if parent is smaller.

  • @davidgeismar6531
    @davidgeismar6531 5 років тому +1

    Is Math.floor((position - 1)/ 2) the sam as Math.ceil((position/2)-1) ?

  • @yagzyalcntas553
    @yagzyalcntas553 6 років тому

    You can do cool simultanious videos playing and making one of them wither away but still cant handle those black screens...