Docsity

Problems for Assignment 4 - Algorithms and Data Structures | CSE 331, Assignments of Computer Science

Toggle sidebar
bg1
CSE 331: Homework 4 Spring 2009
Due by 11:59 PM Friday 13 Feb 2009
This color indicates recent changes since the homework was first released.
Submission
Submit your work via Handin. Name your file <name>Results.pdf, where
<name>is your last name, as in jonesResults.pdf.Please make sure that
your name and your MSU net ID (username) appear at the top of
every page and once at the beginning of any code that you may sub-
mit. Your code file should be named <name>hw4.cpp . Include a makefile
named ’makefile’ or ’Makefile’ that should build your code via the commandline
’make’ utility. The output binary target should be <name>hw4 and it should
take as input <name>hw4 <height>as specified in the last problem. You
are responsible for making sure that your code compiles and runs on
arctic.cse.msu.edu.
Problems:
Do the following problems from Chapter 7 of the text. As always, we do not
want just the ā€œanswersā€, we want the reasoning behind the answers.
1. (20 points) Problem 4.5 of the text: use mathematical induction to do the
proof. Show that the maximum number of nodes, N(h), in a binary tree
of height h is N(h) = 2h+1
āˆ’1.
2. (20 points) Relate this to problem 4.5 of the text.
(a) Compute the average depth of a node in a perfect binary tree of
height h and n = nodes (give a formula). (Give an exact answer for
h = 5.)
(b) What is the average case effort for a successful search in this tree?
(c) What is the worst case effort of a failed search in the tree?
(d) What is the average case effort of a failed search in this tree?
For this problem, we want you to be more precise than Big Oh. Specifi-
cally, we expect exact formulas in terms of h and n that are both simple
and accurate to within 0.5 of the exact answer. Assume that the cost of
finding the root is 1, the root’s children 2, and so on.
3. (20 points) Problem 4.8 of text. Give the prefix, infix, and postfix expres-
sions corresponding to the tree in Figure 4.74.
1
pf2

Partial preview of the text

Download Problems for Assignment 4 - Algorithms and Data Structures | CSE 331 and more Assignments Computer Science in PDF only on Docsity!

CSE 331: Homework 4 Spring 2009

Due by 11:59 PM Friday 13 Feb 2009

This color indicates recent changes since the homework was first released.

Submission

Submit your work via Handin. Name your file Results.pdf, where is your last name, as in jonesResults.pdf. Please make sure that your name and your MSU net ID (username) appear at the top of every page and once at the beginning of any code that you may sub- mit. Your code file should be named hw4.cpp. Include a makefile named ’makefile’ or ’Makefile’ that should build your code via the commandline ’make’ utility. The output binary target should be hw4 and it should take as input hw4 as specified in the last problem. You are responsible for making sure that your code compiles and runs on arctic.cse.msu.edu.

Problems:

Do the following problems from Chapter 7 of the text. As always, we do not want just the ā€œanswersā€, we want the reasoning behind the answers.

  1. (20 points) Problem 4.5 of the text: use mathematical induction to do the proof. Show that the maximum number of nodes, N(h), in a binary tree of height h is N(h) = 2h+1^ āˆ’ 1.
  2. (20 points) Relate this to problem 4.5 of the text.

(a) Compute the average depth of a node in a perfect binary tree of height h and n = nodes (give a formula). (Give an exact answer for h = 5.) (b) What is the average case effort for a successful search in this tree? (c) What is the worst case effort of a failed search in the tree? (d) What is the average case effort of a failed search in this tree?

For this problem, we want you to be more precise than Big Oh. Specifi- cally, we expect exact formulas in terms of h and n that are both simple and accurate to within 0.5 of the exact answer. Assume that the cost of finding the root is 1, the root’s children 2, and so on.

  1. (20 points) Problem 4.8 of text. Give the prefix, infix, and postfix expres- sions corresponding to the tree in Figure 4.74.
  1. (20 points) This problem was initially unclear and asked you to do Problem 4.19 of the text and then provided another set of numbers. You may do either 4.19 OR show the AVL tree after inserting 8, 6, 7, 5, 1, 2, 4, 5, 6 into an initially empty AVL tree. Either way, show the tree after each insertion (and any ensuing rotations).
  2. (20 points) Write a program to test the performance of the STL set con- tainer and report on your results as described below. See Section 4.8.1 of the text and also course directory /Examples/Set/ for code examples using the set container.

(a) Your program should perform these steps. (i) It should input an integer h, which is the height of a perfect binary tree. (ii) It should generate in order the integers 1 to 2h+1^ āˆ’ 1 and insert these into a set, reporting on the total runtime to do this. (iii) Next, it should loop over these same integers and check if each is in the set, reporting on the total runtime. (iv) The program should next create a vector of the integers used above and randomly shuffle them. After clearing the previous set, it should rebuild it by inserting all of the integers, now in random order, reporting the total runtime. (b) When tested to be correct, run your program for h = 7, 10, 13, 16, 19 (if runtimes are reasonable) and create a 3 x 5 table of the runtimes from the above tests. (c) Discuss your results. Do you think that the set container could be implemented via a vector, a hash table, a binary search tree without balancing, or a binary search tree with balancing? Give reasons.