CSSE 230
Data Structures and Algorithm Analysis
Homework 7
- 46 points
To Be Turned In
Submit to the WA7 drop box.
-
Don’t forget: You should be keeping an individual log about your team project work so that you can write a performance evaluation for each of your teammates. You do not need to turn in this log, but I wanted to remind you while I had your attention.
- (15 points) On paper, start with this red-black tree:
(If it is hard to see, the two children of the root are red.)
Insert the following nodes using the top-down insertion algorithm and show what happens (both rotations and re-colorings): 20, 10, 25, 27, 40.
Please denote the red nodes very clearly, using red (best) or a key (acceptable).
Work carefully - it will be hard to give partial credit if your answer is incorrect.
-
(15 points) Weiss exercise 20.5 [20.5]. In this problem you will check your understanding of
collision resolution techniques in hash tables.
- ( 6 points) Weiss Exercise 8.10 [8.10]. Logarithmic form
of Stirling's approximation.
- (10 points; 5 for each part) Checkout the Hash Set Exercise from your personal repository - this is a modified version of Weiss' hash table that implements probing. Answer the following:
- Look at the findPos() method. It currently implements quadratic
probing. Which computation done in findPos() shows you that it is
quadratic probing? What change would you make to that method to change it to
linear probing?
- Examine the code for deleting a value. Note that it doesn't actually delete it, but changes a boolean flag to false. Why must this be the case?
In other words, if you really deleted it, how could it break the hash table? Recall our discussion of probing; also see the contains() method.
Optional practice problem
- not to be turned in.
This problem has been assigned in the past. Since you are actually implementing height-balanced trees, it seems redundant. But it could still be a very good practice problem for the next exam, so I did not remove it.
(xx points) Start with the following Binary Search Tree:
See the hint at the end!
-
Is this an AVL tree? ____ If not, rearrange it so that it is height-balanced.
-
Draw the tree after insertion of a node containing 11, using the usual BST insertion algorithm.
-
Is the new tree AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
-
Delete the element 5 from the original tree (not the one from part c), using the BST deletion algorithm described in class and in the textbook. Draw the new tree.
-
Is the new tree AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
-
Is your new tree (if any) AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
-
Add the element 5 to the tree from part (f) using the BST algorithm. Draw the new tree.
-
Is the new tree AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
-
Add the element 12 to the tree from part (h). Draw the new tree.
-
Is the new tree AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
-
Add the element 7 to the tree from part (j). Draw the new tree.
-
Is the new tree AVL? ______ If not, name the node where the rotation should be done, according to the algorithm from class. ______ Single or double rotation? ____________ If you need to do a rotation, draw the resulting tree.
Hint: In my solution, for all the sub-parts together, I did three single rotations and no double rotations. The first four leaf nodes in my final tree were 3, 5, 7, and 9. (In my first attempt, I misread part d) and deleted from the tree in part c) rather than the original tree. With that mistake, for all the sub-parts together, I did two single rotations and one double rotation and the first four leaf nodes in my final tree were also 3, 5, 7, and 9.)