Overview
What are the indices in part 3 of the homework?
Numbers from 0 to
n
-1, 1 copy of each.
What are we sorting?
The array of numbers from 0 to
n
-1, 1 copy of each.
What’s that other array?
Something the system uses to determine what note to play. You don’t need to know details, other than that it gets used.
What are the events for?
You’ll have a history of sorting the array of numbers. We will “play back” this history using sound and vision.
What events get added at the end?
compare(0,1), compare(1,2), compre(2,3), …
Why?
Because it plays the notes in ascending order as a final step.
Note: I thought about using the Anya/PM version of the lab, but decided against it, so I wrote a brand-new lab. (It basically said, “Starting from scratch, implement binary search trees.” I like more infrastructure.)
If you are assigned the iterative version of exercise 3, here’s pseudocode
if (root is null)
set the root to a new node
else
set current to root
while (...)
compare key to current.key
case =:
replace the value
return the old value
case <:
if there is a left subtree,
current = left
otherwise
current.left = new node
add 1 to size
return null
case >:
if there is a right subtree,
current = right
otherwise
current.right = new node
add 1 to size
return null
If you are assigned the recursive version of exercise 3, here’s pseudocode.
function set(key, value)
root = setHelper(root, key, value)
return cache
function setHelper(node, key, value)
if node == null
set cache to null
add 1 to size
return a new node
else
compare key to node.key
case =:
set cache to value in node
update value in node
return node
case <:
node.left = setHelper(node.left, key, value);
return node;
else
node.right = setHelper(node.right, key, value);
return node;