Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSE 2320 - Spring 2015 Exam 2 Time: 80 mins Name: . Student ID: . Total exam points: 100. Question 1 2 3 4 5 6 7 Total Points Out of 15 18 10 10 17 15 15 100 If you have the smallest doubt about what a question asks of you, or whether or not you can use certain code, ASK US! (Even if you think that it is a really silly question). For any programming question you are free to write additional helper functions if you want to. Exam questions are on both sides. 1 Question 1 (15 points) We define a function F(N), for integers N >= 0, as follows: F(0) = 2 If N is an integer greater than 0, F(N) = F(N-1) + F(floor(N/2)) + 3*N. a) (5 points) Compute the value for F(N) shown below. SHOW YOUR WORK. Hint: read the whole problem first. F(4) = … F(3) = … F(2) = … F(1) = … 2 b) (10 points) Is it possible to write a function in C that computes F(N) in O(Ν) time? If not, why not? If yes, write the function. 3 Question 2 – 18 points Consider this recursive function foo(N): int foo(int N) { if (N <= 2) return 2; return foo(N-1) * (foo(N-1) + 3) - foo(N-1) + 4; } Use the function definition as is. There is no error. a) (6 points) Draw the tree that shows the function calls performed in order to compute foo(5) (the root will be foo(5) and it will have a child for each recursive call.) b) (6 points) Use that tree to compute the number of function calls (that is, the number of nodes in the tree). SHOW YOUR WORK. (Continue on the next page if needed) 4 c) (6 points) Is it possible to re-implement this function so that it has running time O(N)? If not, why not? If yes, write this O(N) implementation of foo in C. 5 Question 3 – 10 points Questions a) and b) below are independent of each other. a) (5 points) An undirected graph G of 7 vertices contains 10 edges (note: we do NOT count edges from A to B and from B to A as two edges, we count them as a single edge). Does this graph: Definitely contain a cycle (i.e., two vertices connected by multiple paths). Definitely NOT contain a cycle Possibly contain a cycle, possibly not contain one Choose one of these three answers, and justify your choice. 6 b) (5 points) A graph G of 7 vertices contains 10 edges. Does this graph: Definitely contain a pair of vertices NOT connected by any path Definitely NOT contain a pair of vertices NOT connected by any path Possibly contain a pair of vertices NOT connected by any path, or possibly NOT contain such a pair. Choose one of these three answers, and justify your choice. 7 Question 4 – 10 points Write a function int longest(char* S) that takes a string, S, and returns the length of the longest name in that string. You can assume that S will be composed of names concatenated together. A name will start with an uppercase letter. You can assume that all the characters in S are letters (no spaces). E.g. for “JaneBobMarcus” it should return 6 (the length of Marcus), for “TomMatthewGarry” it should return 7 and for “TommatthewGarry” it should return 10 (length of Tommatthew – since matthew starts with lowercase m, it will not be considered a new name) 8 Question 5 – 17 points The tree below has root 9 (the nodes are shown as squares). 9 1 4 3 8 2 a) (3 points) List the nodes in inorder: b) (3 points) List the nodes in preorder: c) (3 points) List the nodes in postorder: 9 d) (8 points) In the following sequence of numbers a 0 means a NULL node and a non-zero number means an actual node in the tree. Build a binary tree from these numbers s.t. if we list the nodes of your tree in preorder and we show a 0 for every NULL node, we will produce the same sequence of numbers. For example 3,0,0 would produce a tree that has root 3 and no children (both children are NULL). For clarity, in your tree, show the 0’s as well. 7, 2, 0, 1, 6, 0, 0, 0, 3, 5, 8, 0, 0, 0, 0 10 Question 6 – 15 points A binary tree structure is defined as follows: struct Tree { int item; struct Tree * left_child; struct Tree * right_child; } If a left child or right child does not exist, the corresponding member variable is set to NULL. Write a recursive function int positive(struct Tree * tree) that returns 0 if there is at least one node that has a negative integer and 1 otherwise (all the nodes have integers that are greater than or equal to 0). (Your program should not crash.) 11 Question 7 – 15 points A binary tree structure is defined as follows: struct Tree { int item; struct Tree * left_child; struct Tree * right_child; } If a left child or right child does not exist, the corresponding member variable is set to NULL. Write a function int print_level(struct Tree * tree) that prints the nodes in LEVEL-ORDER, but for each node, IT ALSO PRINTS THE LEVEL. Your program should not crash. If it is easier, you can assume you already have implementations for lists, stacks and queues of integers that you can use in your function. Partial credit will be given if only printing in level-order (without showing the level numer) E.g. for the tree below it will print, in this order: R–0, E–1, S–1, U–2, L–2, T–3 R S E U L T 12 13 14