daily log 9.09.20

less than 1 minute read

function sinkDown(array) {
  //     console.log(array)
  let root_idx = 0;
  let left_idx;
  let right_idx;
  while (right_idx < array.length - 1) {
    console.log("getting here");
    left_idx = root_idx + 1;
    right_idx = root_idx + 2;
    console.log(array[root_idx], array[left_idx], array[right_idx]);
    root_idx = root_idx + 3;
  }
}

sinkDown([39, 19, 12, 11, 3, 4]);

//       39
//       |
//    19    12
//    |      |
//  29 3   4

// REMOVE MAX unshift to get max element
// REPLACE WITH LAST pop off back to get newest node
// RESORT THE TREE

//      4
//       |
//    19    12
//    |      |
//  11 3

// look at new "root"
// look to left and right of "root" to see which is bigger
// swap with the bigger one
// repeat forever

//      19
//       |
//    4    12
//    |      |
//  11 3

// do the same as above with current node becoming new "root"
//      19
//       |
//    11    12
//    |      |
//  4 3

// THINGS I NEED
// left, right
// root, swap

// left is root_idx -1
// right is root_idx -2

// swap is null until we check
// root changes over time

Tags:

Updated: