CS 249 Data Structures Test #2
16. Fill in the big O values: The speed of the Shellsort is more than O(____) but less than O(____).
O(N log N) less than O(N^2)
23. In a complete binary tree with 20 nodes, and the root considered to be at level 0, how many nodes are there at level 4?
5
36. What does the term weakly ordered mean when applied to heaps?
Both right and left children have keys <= the parent
4. When all references to a link are changed to refer to something else, what happens to the link?
Java garbage collection destroys it. Node is orphaned
40. How many arrays, each big enough to hold all the data, does it take to sort a heap?
ONE, array can be sorted in place by splitting in two parts sorted and unsorted.
19. Quicksort involves partitioning the original array and then _________________________________________.
Partitioning the resulting sub arrays
9. The recursive binary search program discussed in class and in your text, is an example of the _________ approach to solving a problem.
divide and conquer
15. True or false: A good interval sequence for the Shellsort is created by repeatedly dividing the array size in half.
false
2. How many references must you change to insert a link in the middle of a singly linked list?
2
28. If a tree is represented by an array, the right child of a node at index n has an index of __________ .
2n+1
14. If an array has 100 elements, then Knuth's algorithm would start with an interval of _____.
40 h=3*h+1 3(1)+1=4 3(4)+1=13 3(13)+1=40 <-- largest interval possible to start with 3(40)+1=121 !!!!Too Large won't work!!!!!
Sorting Algorithm Worst-Case Running Time Merge sort Quicksort Radix sort Shell sort
5. Complete the table below AND describe how each algorithm works in your own words. Sorting Algorithm Worst-Case Running Time Merge sort O( N log N ) Quicksort O( N2 ) Radix sort O( kN ) Shell sort O( N(logN)2 )
33. A "crossover" node or subtree starts as a ________ and becomes a _______ , or vice versa.
A "crossover" node or subtree starts as a __leftchild______ and becomes a __rightchild_____ , or vice versa.
34. A color flip involves changing the color of ______ and ______ .
A color flip involves changing the color of _a node_____ and _its two children_____ .
6. Assuming a copy takes longer than a comparison, is it faster to delete an item with a certain key from a linked list or from an unsorted array?
A linked list
5. A special case often occurs for insertion and deletion routines when a list is _______.
Empty
29. True or False: The red-black rules rearrange the nodes in a tree to balance it.
False
Heap Sort
Heap Sort - Places all elements into a heap, and then removes the root and places it in its final resting places in the array (last element of array if you are using a descending heap, or first element is you are using an ascending heap). Before taking the root again heapify is called to ensure heap property is maintained.
Describe: Merge sort Quicksort Radix sort Shell sort
MergeSort - recursively divide the array in half until only one item is left, then merge subarrays together. QuickSort - partition array around pivot into two subarrays, everything left of pivot should be less then the pivot, everything right of pivot is greater than pivot. Then recursively quicksort each subarray. RadixSort - divide all data items into groups based on the 1s digits, then based on the 10s digits, then based on the 100s digit, until there are no more digits. Be sure to maintain order throughout each pass. ShellSort - insertion sort widely spaced elements decreasing the spacing between elements until the spacing is 1.
Merge Sort
MergeSort- Recursively breaks the array in half until the base case is reached (# of elements is 1). It then uses the merge method to combine two arrays by comparing the first element of each and placing it into the final array in order.
21. Insertion and deletion in a tree require what big O time?
O(log N)........think binary
Quick Sort
Quick Sort - Uses a pivot to partition the array into values less then pivot and values greater then pivot. The pivot is then place in between these two groups in its final resting place and quick sort is called recursively on each side/group.
Radix Sort
Radix Sort - Puts the elements into "bins" based on the ones digit. Preserving order from the previous pass it then places each element into a bin based on the tens digit, repeating for each digit while maintaining order from the previous step until all digits have used.
32. Newly inserted nodes are always colored ________ .
Red
10. What part of the problem becomes smaller with repeated recursive calls in the towers of hanoi program?
The number of disks to transfer decreases by 1 each time the method calls itself.
18. True or false: In quicksort, the pivot can be an arbitrary element of the array.
True
22. True or False: Not all trees are binary trees.
True
Sorting Algorithm Merge sort Quick sort Radix sort Heap sort
Worst-Case Running Time Merge sort--O( NlogN ) Quick sort--O(N2 ) Radix sort--O(k*N ) Heap sort--O( NlogN )
12. Besides a loop, a _________ can often be used instead of recursion. a. Stack b. Queue c. Array d. None of the above
a. Stack
25. Finding a node in a binary search tree involves going from node to node, asking a. how big the node's key is in relation to the search key. b. how big the node's key is compared to its right or left children. c. what leaf node we want to reach. d. what level we are on.
a. how big the node's key is in relation to the search key.
heap can be represented by an array because a heap a. is complete. b. is weakly ordered. c. is a binary tree. d. satisfies the heap condition.
a. is complete.
1. Access to the links in a linked list is usually through the _______ link. a. the first b. the last c. any d. each
a. the first
37. To "trickle up" a node in a descending heap means a. to repeatedly exchange it with its parent until it's larger than its parent. b. to repeatedly exchange it with its child until it's larger than its child. c. to repeatedly exchange it with its child until it's smaller than its child. d. to repeatedly exchange it with its parent until it's smaller than its parent.
a. to repeatedly exchange it with its parent until it's larger than its parent.
27. Suppose a node A has a successor node S. Then S must have a key that is larger than _____ but smaller than or equal to _______. a. A, A's successor b. A, A's left-child descendant c. A's left-child descendant, A's right-child descendant d. A, A's right-child descendant
b. A, A's left-child descendant
35. What does the term complete mean when applied to binary trees? a. All the necessary data has been inserted. b. All the rows are filled with nodes, except possibly the bottom one. c. All existing nodes contain data. d. The node arrangement satisfies the heap condition.
b. All the rows are filled with nodes, except possibly the bottom one.
31. Which of the following is not a red-black rule? a. Every path from a root to a leaf, or to a null child, must contain the same number of black nodes. b. If a node is black, its children must be red. c. The root is always black. d. All three are valid rules.
b. If a node is black, its children must be red.
8. We've seen that recursion can take the place of a loop. Which of the following is not true? a. Both programs divide the range repeatedly in half. b. If the key is not found, the loop version returns because the range bounds cross, but the recursive version occurs because it reaches the bottom recursion level. c. If the key is found, the loop version returns from the entire method, whereas the recursive version returns from only one level of recursion. d. In the recursive version the range to be searched must be specified in the arguments, while in the loop version it need not be.
b. If the key is not found, the loop version returns because the range bounds cross, but the recursive version occurs because it reaches the bottom recursion level.
11. Which is not true about the merge() method used to implement merge sort a. Its algorithm can handle arrays of different sizes. b. It must search the target array to find where to put the next item. c. It is not recursive. d. It continuously takes the smallest item irrespective of what array it's in.
b. It must search the target array to find where to put the next item.
20. After a partition in a simple version of quicksort, the pivot may be a. used to find the median of the array. b. exchanged with an element of the right subarray. c. used as the starting point of the next partition. d. discarded.
b. exchanged with an element of the right subarray.
7. Which of the following is not true? Iterators would be useful if you wanted to a. do an insertion sort on a linked list. b. insert a new link at the beginning of a list. c. swap two links at arbitrary locations. d. delete all links with a certain key value.
b. insert a new link at the beginning of a list.
39. Heapsort involves a. removing data from a heap and then inserting it again. b. inserting data into a heap and then removing it. c. copying data from one heap to another. d. copying data from the array representing a heap to the heap.
b. inserting data into a heap and then removing it.
b. Write a findSuccessor method that takes as parameter a node in a binary search tree and returns its successor node. Use Java or pseudocode. Remember that there are two subcases: either the input node has a non-empty right subtree, or the right subtree is empty. If the right subtree is empty, then you must start "backtracking" along parent nodes. Node successor(Node n){ Node succ; //case 1 - right subtree isnull if (n.right!=null) { right subtree is not null //go to right subtree and find mind node (all the way down left) succ=node.right while(succ.left!=null){ succ=succ.left; //continue down left subtree until a leaf is reached } } else { //case 2 - right subtree is null, backtrack up tre until you find a node whose // left is the parent of the node whose successor you want succ=n.parent; while(succ!=null && succ.right==n) { //while a successor exists and //the current node is a right child //continue back tracking n.succ; succ=succ.parent } } }
c. What is the worst case running time for your findSuccessor algorithm? The worst case for the successor algorithm would be to traverse the entire height of the tree, which could be O(N) in the worst case.
13. The Shellsort works by a. partitioning the array. b. swapping adjacent elements. c. dealing with widely separated elements. d. starting with the normal insertion sort.
c. dealing with widely separated elements.
24. A subtree of a binary tree always has a. a root that is a child of the main tree's root. b. a root unconnected to the main tree's root. c. fewer nodes than the main tree. d. a sibling with the same number of nodes.
c. fewer nodes than the main tree.
26. An unbalanced tree is one a. in which most of the keys have values greater than the average. b. whose behavior is unpredictable. c. in which the root or some other node has many more left children than right children, or vice versa. d. that is shaped like an umbrella.
c. in which the root or some other node has many more left children than right children, or vice versa.
3. Assuming current points to the next-to-last link in a singly linked list, what statement will delete the last link from the list?
current.next=null;
30. A null child is a. a child that doesn't exist but will be created next. b. a child with no children of its own. c. one of the two potential children of a leaf node where an insertion will be made. d. a non-existent left child of a node with a right child (or vice versa).
d. a non-existent left child of a node with a right child (or vice versa).
17. When partitioning, each array element is compared to the _________.
pivot