CSE 2231 Midterm 2 Review

Click Heading Titles to go to Lecture Slides.

Important to remember:
Correspondence is a function that relates concrete state space to abstract state space.
Convention is a statement that must always hold true in this.
Understanding these and reading contracts may be important for this exam.
sortingMachine Kernel Methods
Heaps (log time)
How removeFirst Can Work
To heapify a complete BST, recursively heapify left and right sub-trees, then sift down the root.
siftDown(T[] array, int top, int last) code:
    	
    	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.
Be sure to know what a complete BT is. It is a tree where each node has no more than two subtrees and is filled from left to right.


Note: The term smart node is just a play on the common name: 'Dummy node'. Also, 'Sentinel node' is often used for this type.
In List, there are two smart nodes (one more at the end) in order to enable handling of special case in addRightFront and removeRightFront.

What's new?
TF newInstance() clear() transferFrom()
Refer to slides:

Main ideas:
A tree is a recursive structure representing a collection of nodes.
Size, Height, Labels

Disassemble does the reverse of this.
BugsWorld compiler has:

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:
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.

The lecture uses the following CFG as a representation.

What this means is: These can be represented by what is called a "derivation tree":