150 likes | 235 Views
Midterm 2 Review. Please grab a handout. Go ahead and start solving ReallyComplicatedGame. Easy way to check who wins a board winMap.get (new String(board));. The basics. You are allowed 1 sheet of notes
E N D
Midterm 2 Review Please grab a handout
Go ahead and start solving ReallyComplicatedGame • Easy way to check who wins a board winMap.get(new String(board));
The basics • You are allowed 1 sheet of notes • This is a tightly timed exam – be strategic about your time expenditure. Remember that all coding problems are worth an equal number of points. • My personal option: the hardest problems are recursive backtracking and stacks and queues
The Straightforward Problems • Questions 1-3 • When adding nodes to a binary search tree, just add them one at a time and don’t move any existing nodes – that should give you a unique tree • Know how heaps work (handout on website) • Know preorder, postorder and inorder traversals
Recurrence Relations • Recurrance relations – you need to use them not derive them • You don’t need to justify your BigO’s on the exam • I will give you these (you won’t need others) • T(n) = T(n/2) + O(1) → O(log n) • T(n) = T(n-1) + O(1) → O(n) • T(n) = 2T(n/2) + O(1) → O(n) • T(n) = 2T(n/2) + O(n) → O(n log n) • T(n) = T(n-1) + O(n) → O(n2)
Big Os • Adding, removing, or searching a BST depends on its height. The height is O(n) if the tree is not height balanced (worst case), O(log n) if it is. • For priority queues, adding or removing lowest element is O(log n). Doing other stuff tends to be O(n) – priority queues are optimized for those 2 operations
Very Hard Problems • Incomputablity and P/NP are both fair game • But it’s only worth 1 point!
Linked List problems • Doubly linked lists are fair game • Be especially careful with removals. You don’t want to accidentally skip elements. (e.g. repeated 4s in the remove 4s example)
Recursive Backtracking Two kinds of problems • 1st player/2nd player inversion problems • Map traversal problems
1st player/2nd player inversion problems • I can win if I can put the 2nd player in a situation where they CAN’T win • I try all the moves I can currently do and update the game state. Then I let the 2nd player go by making them the first player for this new game • If the second player can win, this is a BAD move for me • So I’ll try a different move • If I’ve tried every possible move and I can’t win, I’m gonna lose. So I return that. • I MUST always return the game state to what it was before I was called
Map traversal problems • Check if you’re off the board • Check if you’re in an illegal position (e.g. an X, a square you’ve visited) • See if you’ve won • IF NOT – mark that this square has been visited (avoid infinite loop) • Then recursively call yourself on neighbors • When you’re finished UNMARK that this square has been visited
Trees • Hopefully very straightforward
Stack and Queue Problems • Always look like • Make a stack or queue • Add starting element to the stack/queue • While the queue is not empty • Grab some element off the stack/queue • Maybe add some more elements to the stack/queue • Sometimes it’s possible to do an end-run around these problems with recursion. Allowed, but can complicate things.
Which structure? • QUEUES – useful when you want to visit some structure in distance order (e.g. everything that’s 1 away, followed by everything that’s 2 away, followed by…) • STACKS – useful for hierarchies where you deal with children before moving on to siblings • PRIORITY QUEUE – deal with certain elements first, based on a sort
ALWAYS • Be calm and strategic. You don’t need to have a solution for every problem to do well on the exam. • Keep in mind the solution structures – that can get you partial points (sometimes) • Make sure you understand the solutions to the sample problems