if (array.length > 1) { // Set the index of left and right in relation to the root. int leftIndex = 2 * top + 1; int rightIndex = 2 * top + 2; // I declared root for simplicity T root = array[top]; // Make sure the right node actually exists. if (rightIndex <= last) { // Set smallerIndex to index of whichever child is smallest. int smallerIndex = leftIndex; int largerIndex = rightIndex; if (order.compare(array[leftIndex], array[rightIndex]) > 0) { smallerIndex = rightIndex; largerIndex = leftIndex; } // Move smaller node up if it is smaller than root. if (order.compare(root, array[smallerIndex]) > 0) { exchangeEntries(array, top, smallerIndex); } // SiftDown the left and right nodes. siftDown(array, leftIndex, last, order); siftDown(array, rightIndex, last, order); } //We might still need to sift if right node doesn't exist. else if (leftIndex <= last) { T leftHeap = array[leftIndex]; // Swap leftHeap with root if root is greater. if (order.compare(root, leftHeap) > 0) { exchangeEntries(array, top, leftIndex); } } } Notes: rightIndex = 2 * top + 1 leftIndex = 2 * top + 2 Check the root of the smaller child and swap that with the parent root. Then recursively siftDown children.
Note that Statement can be represented as a Tree of StatementLabel. Where StatementLabel has a Kind, a Condition, and an instruction (String). The Kinds are: BLOCK IF IF_ELSE WHILE CALL Be sure to see slides for enum information (slide 14).
Program represents a complete BL program. The program is represented by name (String) , context (Map) , and body (Statement). name is required to be an IDENTIFIER. context maps instruction names with the content of those instructions. body represents the main part of the program after BEGIN.
A CFG is a set of specific formation rules for a language. These rules can be used to determine whether or not sun supposed use of that language is syntactically correct.
These can be represented by what is called a "derivation tree":