Advertisement
Guest User

Trickle Down for Heap

a guest
Oct 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. private void trickleDown(Node<T> trickle) {
  2.         boolean go = true;
  3.  
  4.         while (go) {
  5.             if (trickle.getLeftChild() == null && trickle.getRightChild() == null) {
  6.                 System.out.println("Stop please");
  7.                 go = false;
  8.             } else if (trickle.getLeftChild() != null && trickle.getRightChild() != null) {
  9.                 if (trickle.getLeftChild().getPriority() > trickle.getRightChild().getPriority()) {
  10.                     if (trickle.getPriority() > trickle.getLeftChild().getPriority()) {
  11.                         go = false;
  12.                     } else {
  13.                     // swap the left child with current
  14.                     // create temp values of trickle
  15.                     T value = trickle.getValue();
  16.                     int priority = trickle.getPriority();
  17.                     // set trickle values to leftchilds
  18.                     trickle.setPriority(trickle.getLeftChild().getPriority());
  19.                     trickle.setValue(trickle.getLeftChild().getValue());
  20.                     // set trickle's child to temp vals
  21.                     trickle.getLeftChild().setValue(value);
  22.                     trickle.getLeftChild().setPriority(priority);
  23.                    
  24.                     trickle = trickle.getLeftChild();
  25.                     }
  26.                    
  27.                 } else if (trickle.getLeftChild().getPriority() < trickle.getRightChild().getPriority()) {
  28.                     if (trickle.getPriority() > trickle.getRightChild().getPriority()) {
  29.                         go = false;
  30.                     } else {
  31.                     // swap the left child with current
  32.                     // create temp values of trickle
  33.                     T value = trickle.getValue();
  34.                     int priority = trickle.getPriority();
  35.                     // set trickle values to Rightchilds
  36.                     trickle.setPriority(trickle.getRightChild().getPriority());
  37.                     trickle.setValue(trickle.getRightChild().getValue());
  38.                     // set trickle's child to temp vals
  39.                     trickle.getRightChild().setValue(value);
  40.                     trickle.getRightChild().setPriority(priority);
  41.                     trickle = trickle.getRightChild();
  42.                     }
  43.                    
  44.                 }
  45.             } else if (trickle.getRightChild() == null && trickle.getLeftChild() != null) {
  46.                 if (trickle.getPriority() > trickle.getLeftChild().getPriority()) {
  47.                     go = false;
  48.                 } else {
  49.                 // swap the left child with current
  50.                 // create temp values of trickle
  51.                 T value = trickle.getValue();
  52.                 int priority = trickle.getPriority();
  53.                 // set trickle values to leftchilds
  54.                 trickle.setPriority(trickle.getLeftChild().getPriority());
  55.                 trickle.setValue(trickle.getLeftChild().getValue());
  56.                 // set trickle's child to temp vals
  57.                 trickle.getLeftChild().setValue(value);
  58.                 trickle.getLeftChild().setPriority(priority);
  59.                 trickle = trickle.getLeftChild();
  60.                 }
  61.             }
  62.         }
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement