Exam 3 Review Flashcards
Recursion requires what two cases?
A base case and a recursive case
Recursive method for calculating a factorial
int fact(int n) { int result;
if(n==1) return 1; else { return n * fact(n-1); } }
What is a heap?
- a balanced binary tree
- created using a binary tree
Characteristics of a binary search tree
- only 2 children per node
- left subtree must be less, right subtree must be more
Pre order
root, left subtree, right subtree
In order
left subtree, root, right subtree
IN COMPARABLE ORDER SMALLEST TO LARGEST
Post Order
left subtree, right subtree, root
Breadth-first
based on level… root level, then next level on the left, same level on the right
Difference between binary search tree and binary tree?
The ordering of the values… for a binary tree it doesn’t matter the order as long as each node only has two nodes at most… for a binary search tree the left node must be less than the root and the right must be greater
When adding a value to a BST, what is the first operation? What happens next?
- run find() to determine where it should be
2. add the node at that position
Know how to write the recursive find method
Find an element in the tree
• Compare with root, if less traverse left, else traverse right;
repeat
• Stop when found or at a leaf
.. compexity is log n
2 properties that heaps have?
- Shape Property ( all leaf nodes are either on the bottom level or the second most bottom level)
- Order property (the data value stored at a node is less than or equal to the data values stored at all of its descendants)
(root value is always the smallest value int he heap)
What are heaps good for?
- finding the maximum or minimum value
- sorting values into ascending or descending order
- implementing another data structure (priority queue)
What interface must a class implement to be storable in a heap?
comparable
What must you do when you add nodes to or remove nodes from a heap?
add -> add to the heap then maintain the shape and heap properties (swap around values until the order properties are met)
remove -> remove and return the root then rebuild the heap to SELECT a new root that is the smallest value
Why is it important to keep the heap balanced?
to maintain the properties
What must be there before the CFG is finished?
start symbol
variables
terminal symbols
production symbols