Homework #2 CS 2420 solution

Written Assignments (40 points)
1. (20 points) Given the tree shown below:
(a) Identify all the leaf nodes and all the internal nodes.
(b) List the nodes with depth 0, 1, 2, and 3.
(c) What is the height of the tree? What is the height of the subtrees with root B and C?
(d) For the nodes B, C, and D: list the parent, the children, the siblings, all the ancestors, and all the
descendants.
2. (20 points) Answer the following questions:
(a) Suppose you insert 11 and 41 into a stack. Then you remove two items. Then you insert 21 and 31 into the
stack. Then you remove one item. Which one is left?
(b) What are the Big-O complexity of pushing and popping items in an array implementation of a stack? What
is the complexity of enqueue and dequeue for an array implementation of a queue? Explain your answers.
(c) How many times would you need to traverse a singly linked list to delete the item with the largest key
if the items in the list are not sorted? Explain the purpose of each traversal.
Programming Assignment (60 points)
3. (30 points) Given an array A of size N-1 that contains all numbers from 1 to N except one, design and
implement a linear algorithm that finds the missing number. Briefly explain your algorithm in your answer note.
Save the program in a file called findNumber.cpp.
4. (30 points) What data structure is most suitable to determine if a string s is a palindrome, that is, it is equal to
its reverse. For example, “racecar” and “gohangasalamiimalasagnahog” are palindromes. Justify your answer in
your answer note. Use Big-O notation to represent the efficiency of your algorithm. Implement your program
and save it in a file called palindrome.cpp.
All submitted source codes will be passed to Stanford MOSS (http://theory.stanford.edu/~aiken/moss/) for
detecting software plagiarism. You are NOT supposed to copy and paste online solutions nor your collegue's
solutions.
What to turn in:
 Include your name and A number at the top of each file (PDF and two programming files).
 Compress the three files (i) your answer note for questions 1, 2, 3 and 4 in PDF file format (ii) your
findNumber.cpp; and (iii) palindrome.cpp.
 Name the compressed file as hw02_FIRSTNAME_LASTNAME.zip. For example, if your name is
John Smith, name the file as hw02_John_Smith.zip. Submit the compressed file to Canvas.
 Make sure all your programs are fully commented, and compile and run correctly.
 This is an individual assignment, but you may discuss general strategies and approaches with
other members of the class (refer to the syllabus for details of the homework collaboration
policy). If you discussed an assignment with your colleague, explicitly report the colleague's name and
what you discussed in your submission.
 Follow Code of Conduct (refer to the syllabus for details).
sellfy