CS2 Final Study Guide

CS2 Final Study Guide
- Know how to write an assignment operator, copy constructor and destructor for a
simple class that has dynamically allocated memory.
Trees
1. Define the following terms
o binary search tree
o leaf of a tree
o degree of a tree node
o degree of a tree
o path in a tree
o length of a path in a tree
o descendents of a node in a tree
o ancestors of a node in a tree
o depth of a node in a tree
o height of a node in a tree
o height of a tree
2. Give an ADT for binary trees.
3. Draw the expression tree for the expression a*(b+c)-(d+(e-f)*g)
4. For the tree
1.
2.
3.
4.
5.
6.
What is the height of node A?
What is the height of the tree?
What is the depth of node E?
What is the parent of node F?
Name the descendent(s) of node B.
Name the leaves in the tree.
5. Write a C++ function TreeNode*
copy of binary tree T
6. Write a C++ function
copy_tree(TreeNode* T)
that returns a
TreeNode* expand_leaf(TreeNode* node, ItemType x, ItemType y)
that returns a new binary tree that is identical to the binary tree T except
that every leaf in T now has a left child and a right child whose values
are equal to x and y, respectively. For example, invoking expand_leaf(T,
9, 12) on the tree on the left produces the tree on the right.
7. Write a C++ function int
the binary tree T.
8. Write a C++ function
height(TreeNode* T)
that returns the height of
bool same_tree(TreeNode* T1, TreeNode* T2)
that returns true if binary trees T1 and T2 are exactly the same (same
values, same structure), and returns false otherwise. You may assume
that values of tree_item_type can be compared by means of
the == operator.
9. Write a C++ function
TreeNode* bst_insert(ItemType item, TreeNode* T)
that inserts item into the binary search tree T. After the insertion, T must
remain a binary search tree. You may assume that tree_item_type values
are comparable using the ==, <, and > C operators. Draw the binary
search tree resulting from sequentially calling bst_insert on an initially
empty binary tree with the values 9,12,5,15,7,10,18.
Tree Traversal
1. For the tree
1. Write the node labels in pre-order order
2. Write the node labels in in-order order
3. Write the node labels in post-order order
2.
1. Write C++ code, using the ADT operations for binary tree, to
construct this binary tree of char
2. Write a function void Foo(TreeNode* T) that takes a binary tree, T,
of char and performs some function (your choice) on the tree.
Briefly describe what the function does in your own words.
Search
Functions for DFS and BFS of binary trees from the notes will be given to you
in the exam, if needed.
1. Let tree T be this tree
1. Draw the subtree returned by depth_first_search(T, 'B');
2. Draw the subtree returned by bfs_queue(T, 'B');
2. Given an array containing the
sequence 1,5,29,45,67,76,92,104,187,234 (in that order)
1. state each comparison made in finding the number 234 using
linear search. (For example, 234:1 is a comparison of 234 with 1.)
2. state each comparison made in determining that the number 48 is
not present using linear search.
3. Given an array containing the sequence 1,
5,29,45,67,76,92,104,187,234 (in that order)
1. state each comparison made in finding the number 234 using
binary search. (For example, 234:1 is a comparison of 234 with
1.)
2. state each comparison made in determining that the number 48 is
not present using binary search.
4. Given the following sequence of integers (for example, integers received
from the keyboard)
29,234,1,45,92,76,67,187,104,5
(in that order)
1. draw the binary search tree that would result from inserting the
integers in the order given.
2. state each comparison made in finding the number 234 in the tree
(For example, 234:1 is a comparison of 234 with 1.)
3. state each comparison made in determining that the number 48 is
not present in the tree.
4. write the integers as they would be encountered in an in-order
traversal of the tree.
5. Linear search can be done on data in an array or in a list. Name an
advantage and a disadvantage of the list implementation over the array
implementation.
6. Binary search can be done on data in a sorted array or in a binary search
tree. Name an advantage and a disadvantage of the array implementation
over the BST implementation.
Sorting
1. How many permutations of N distinct items are there?
2. Explain: selection sort is an
3. Explain: quicksort is an
algorithm on average.
algorithm on average, but is
an
algorithm worst case.
4. Given an array containing the integers 12 9 3 6 4 1 (in that order),
1. Show the array as it is sorted by the function selectionsort.
2. Show the array as it is sorted by the function mergesort
3. Show the array as it is sorted by the function insertionsort
4. Show the array as it is sorted by the function bubblesort
5. Write a templated version of both bubble and selection sort.
1. As the numbers of elements to be sorted increases how do the two
sorts compare to one another?
Asymptotic Analysis
1. Fill in the following in increasing order
from the set
2. Prove that the time complexity to build a queue of n items,
using n enqueue operations, is
a simple list.
when the queue is implemented as
3. Linear search can be done on data in an array or in a list. What is the
average time complexity for successful search using an array? Using a
list? Name an advantage of the list implementation. Name an advantage
of the array implementation.
4. Binary search can be done on data in a sorted array or in a binary search
tree. What is the average time complexity for successful search using the
array? Using the BST? What is the worst case time complexity for each?
Name an advantage of the BST implementation. Name an advantage of
the sorted array implementation.
Graphs
1. Is every graph a binary tree? If so why, it not, why not?
2. Draw the adjacency matrix for the following graph. Draw the adjacency
list for the graph. When is one representation better than the other? How
do you represent an undirected graph in an adjacency matrix?
3. Starting at Start Room label nodes in the order visited via a depth first
search
4. Starting at Start Room label nodes (1,2,3,4,..) in the order visited via
breadth first search.
Heaps
1. Be able to identify a heap.
2. Show a maxheap after inserting each of the following numbers:
1,6,4,5,0,8,3,12
3. Remove the two biggest numbers, showing the heap structure after
each deletion.
Previous Exam Topics
1. What will be the output of the program?
#include <iostream>
using namespace std;
int main () {
int nums[] = {2, 4, 6, 1, 2, 5};
int *p = nums + 5;
while (p >= nums) {
if (*p % 3 == 0) {
*p = *p + 1;
p++;
}
else if(*p % 2 == 0) {
*p += 2;
p++;
}
p = p - 2;
}
for (int i=0; i<6; i++) cout << nums[i] << " ";
}
2. Write the code fragment that appropriately deletes the node pointed to by p
(data value 14) in the doubly linked list shown in the diagram. Write the code
fragment to insert a node between 14 and 16.
3. Write code for an abstract base class called SeaCreature. This base class should
have at least 2 member functions, not including any constructors/destructors.
Write a derived concrete subclass called Fish. Remember to use good
programming habits when writing these classes.