Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in the drawing to indicate what must change in the linked list for each change described. (6 points) (a) Add integer 41 (b) Add integer 61 16 16 25 25 37 37 45 45 58 0 58 0 (c) Add integer 11 16 25 37 45 58 0 1
2. What does the acronym LIFO stand for? (2 points) 3. Which data structure is LIFO, stack or queue? (2 points) 4. What does the acronym FIFO stand for? (2 points) 5. Which data structure is FIFO, stack or queue? (2 points) 6. Enter the values that will be in the linked list of integers, and draw the links (arrows), of a stack after the following operations are executed. (6 points) push 12 push 23 push 34 push 45 pop pop push 56 7. Enter the values that will be in the linked list of integers, and draw the links (arrows), of a queue after the following operations are executed. (6 points) enqueue 12 enqueue 23 enqueue 34 enqueue 45 dequeue dequeue enqueue 56 2
8. Answer the following questions regarding this tree: 42 36 65 19 40 56 78 12 22 38 58 71 82 31 Is this a binary search tree? (yes or no) (2 points) What is the root? (2 points) How many nodes are in the tree? (2 points) List the leaves in the tree. (2 points) Name the children of node 36: (2 points) Name the parent of node 36: (2 points) List the nodes as they would be processed in a breadth- search: (2 points) List the nodes as they would be processed in a preorder traversal: (2 points) List the nodes as they would be processed in an inorder traversal: (2 points) List the nodes as they would be processed in a postorder traversal: (2 points) 3
Name 9. Use code stack.h as a reference. Write the following two new member functions: MakeEven takes no parameters and changes every occurrence of an odd number in the stack to an even number by adding 1 to it. It is to return the number of nodes that were changed. For example, if the stack mystack contains 1 8 9 4 and we call mystack.makeeven( ), the stack would be modified to contain 2 8 10 4 and a 2 would be returned. It therefore has no parameters, and it is to return an integer. Demote takes one integer parameter. If that integer is not in the stack, return false. If it is the integer in the stack, return false. Otherwise, move this node down one position in the stack and return true. For example, if the stack mystack contains 1 2 3 4 and we call mystack.demote(2), the stack would be modified to contain 1 3 2 4 and it would return true. The method will have one parameter and return a boolean. (a) Begin by indicating the changes that need to be made to the class declaration: (6 points) class Stack { public: // see function def's for pre and post Stack() { top = 0;} ~Stack(); bool IsEmpty() {return (top == 0);} bool IsFull(); bool Peek(int & x); void Push(int x); bool Pop(int & x); void Display(); }; private: Node * top; // points to the Node in the stack Use the following two pages to code the functions. 4
(b) Write the member function definition for MakeEven: (12 points) (repeated for your convenience) MakeEven takes no parameters and changes every occurrence of an odd number in the stack to an even number by adding 1 to it. It is to return the number of nodes that were changed. For example, if the stack mystack contains 1 8 9 4 and we call mystack.makeeven( ), the stack would be modified to contain 2 8 10 4 and a 2 would be returned. It therefore has no parameters, and it is to return an integer. 5
(c) Write the member function definition for Demote: (12 points) (repeated for your convenience) Demote takes one integer parameter. If that integer is not in the stack, return false. If it is the integer in the stack, return false. Otherwise, move this node down one position in the stack and return true. For example, if the stack mystack contains 1 2 3 4 and we call mystack.demote(2), the stack would be modified to contain 1 3 2 4 and it would return true. The method will have one parameter and return a boolean. 6
10. Use code tree.h as a reference. Write the following two new member functions: (12 points each) HaveOneChild (recursive) will return the number of nodes in the tree that have exactly one child. If the tree is empty, it should return 0. Therefore, it will have no parameters and return an integer.. TheParentOf (non-recursive and assuming that the tree is a binary search tree; that is, all nodes in the left subtree have values less than the current node and all nodes in the right subtree have values greater than or equal to the current node) takes an integer as a parameter and returns the value of its parent. If the value is not in the tree, which includes the case where the tree is empty, return -1. If the value is at the root, return -2. Therefore, it will have one parameter and return an integer. (Code HaveOneChild on this page and TheParentOf on the following page.) 7
(repeated for your convenience) TheParentOf (non-recursive and assuming that the tree is a binary search tree; that is, all nodes in the left subtree have values less than the current node and all nodes in the right subtree have values greater than or equal to the current node) takes an integer as a parameter and returns the value of its parent. If the value is not in the tree, which includes the case where the tree is empty, return -1. If the value is at the root, return -2. Therefore, it will have one parameter and return an integer. 8