Professional Documents
Culture Documents
Arnaud:
From an empty RB tree, insert manually (by making figures and writing down what you do) the
following nodes: 2, 1, 4, 5, 9, 3, 6, 7.
Solution:
https://docs.google.com/presentation/d/1jLpIgpdnhTpAezaVAzRXyhWvlZK5qfl0MisA9_C8Lz8/e
dit?usp=sharing
Tim:
Given the following nodes and an empty RB tree, insert each node one at a time, and show the state of
the tree after each insertion.
Solution:
https://docs.google.com/presentation/d/1kedRTTs5x_ugO1ZF2bDOt5NPCfztRDq0-
RX2IakQeQg/edit?usp=sharing
Nicole:
Create a RB tree by inserting the following sequence of numbers: 8,18,3,7,6,5,20,9. Clearly show the
tree that results after each insertion (indicating the color of each node).
Solution: https://docs.google.com/document/d/1dhj0zYOgk-VtY6xH0pI4LpXyDpG4LwCE45CVvBQ-
T_E/edit?usp=sharing
Linhui:
Indicate for each of the following statements if it is true or false. Justify your answers.
Solution:
1. FALSE. The root of a red-black must be black by definition. It is possible for the child of the root
of a red-black tree to be red. Therefore, it is possible for the subtree of the root of a red-black
tree to have a red root, meaning that it cannot be a red-black tree. So the statement is false.
2. TRUE. Let x represent the parent of the null reference, and without loss of generality, suppose
x.right is the null reference. Suppose x.left refers to a black node. But then, the number of black
nodes on the path from the root to the x.right null reference must be less than the number of
black nodes from the root to all nodes in the subtree rooted at x.left. This violates the definition
of red-black tree. So, x.left must be a null reference or a red node.
Source: https://cseweb.ucsd.edu/classes/su05/cse100/cse100Midterm1Solutions.pdf
Muhammad:
Show the red-black tree that results after each of the integer keys 21, 32, 64, 75, and 15 are inserted, in
that order, into an initially empty red-black tree. Clearly show the tree that results after each insertion
(indicating the color of each node) and make clear any rotations that must be performed.
Solution:
https://drive.google.com/file/d/1mmw2VQmbujRs8tktNU6Ovm0V2NTO55vx/view?usp=sharing
Source: https://cseweb.ucsd.edu/classes/su05/cse100/cse100Midterm1Solutions.pdf
KC:
Show that the longest simple path from a node x in a red-black tree to a descendant leaf has length at
most twice that of the shortest simple path from node x to a descendant leaf.
Solution:
In the longest path, at least every other node is black. In the shortest path, at most every node is black.
Since the two paths contain equal numbers of black nodes, the length of the longest path is at most
twice the length of the shortest path.
Since every path contains bh(x) black nodes, even the shortest path from x to a descendant leaf has
length at least bh(x). By definition, the longest path from x to a descendant leaf has length height(x).
Since the longest path has bh(x) black nodes and at least half the nodes on the longest path are black (by
property 4), bh(x) >= height(x)/2, so
length of longest path = height(x) <= 2 * bh(x) <= twice length of shortest path.
Source (for the question): Introduction to Algorithms by Cormen et al. (the course textbook), Exercise
13.1-5.
A: No, the red-black tree will not necessarily be the same. Here are two examples: one in which the
tree’s shape changes, and one in which the shape remains the same but the node colors change.
Hashing
Muhammad
Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod
10, which of the following statements are true?
(A) i only
(B) ii only
(D) iii or iv
h(9679) = 9679%10 = 9
h(1989) = 1989%10 = 9
h(4199) = 4199%10 = 9
h(1471) = 1471%10 = 1
h(6171) = 6171%10 = 1
As we can see, 9679, 1989 and 4199 hash to the same value 9. Also, 1471 and 6171 hash to the same
value 1. Therefore, statement (i) and (ii) are correct which match with option (C).
Source: https://www.geeksforgeeks.org/practice-problems-on-hashing/
Nicole:
Answer the following question with T/F:
Solution:
T. If a.equals(b), then when a and b are both the inputs of one same hash function, the results of the
function are the same.
F. The reverse of the statement in (1) is false, could prove by counter example, when the hash function
is x mod 10, 12 and 22 both have output value 2, but 12 and 22 are different input values..
F. Clustering during hashing means when two or more elements try to accomodate in one slot of the
hash table. Best Hash function will always avoid this kind of activity. So the best Hash function has the
least amount of clustering .
Source:
https://www.cs.cornell.edu/courses/cs2110/2017fa/recitations/rec05Hashing/hashingProblemSetSoluti
ons.pdf
Tim:
Suppose you are given the following set of keys to insert into a hash table that holds exactly 11 values:
113 , 117 , 97 , 100 , 114 , 108 , 116 , 105 , 99 Which of the following best demonstrates the contents of
the hash table after all the keys have been inserted using linear probing?
100, ___, ___, 113, 114, 105, 116, 117, 97, 108, 99
99, 100, ___, 113, 114, ___, 116, 117, 105, 97, 108
100, 113, 117, 97, 14, 108, 116, 105, 99, ___, ___
117, 114, 108, 116, 105, 99, ___, ___, 97, 100, 113
99, 100, ___, 113, 114, ___, 105, 116, 117, 97, 108
Solution:
0, 1, 3, 4, 6, 7, 6, 9, 9
Source:
https://runestone.academy/runestone/books/published/pythonds/SortSearch/Hashing.html
KC:
Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple
uniform hashing, what is the expected number of collisions? More precisely, what is the expected
cardinality of {{k,l}: k != l and h(k) = h(l)}?
Solution:
Source (for the question): Introduction to Algorithms by Cormen et al. (course textbook),
Exercise 11.2-1
Linhui:
1. Suppose you use open hashing (chaining) and the following keys are inserted: 5, 28, 19, 15, 20,
33, 12, 17, 10 and m = 9. For simplicity, here we do not distinguish a key from its hashcode, so
we assume h(key)=key. In which slots do collisions occur?
2. Now suppose you use closed hashing with linear probing and the same keys as above are
inserted. More collisions occur than in the previous question. Where do the collisions occur and
where do the keys end up?
Solution:
1. The keys 5, 28, 19, 15, 20, 33, 12, 17, 10 map to slots 5, 1, 1, 6, 2, 6, 3, 8, 1. Collisions are
underlined: when inserting key 19, at slot 1; when inserting key 33, at slot 6; and when inserting
key 10, at slot 1.
2. The key 19 collides with key 28 at slot 1 and 19 goes into slot 2. Key 20 collides with key 19 at
slot 2 and needs to go to slot 3. Key 33 collides with key 15 at slot 6 and needs to go to slot 7.
Key 12 collides with key 20 at slot 3 and needs to go to slot 4. Key 17 goes into position 8. Key 10
collides with key 28 at slot 1, and then collides with 19 at slot 2, etc until it finally finds an open
slot at position 0. The final positions of the keys are [ 10, 28, 19, 20, 12, 5, 15, 33, 17].
Source: http://www.cim.mcgill.ca/~langer/251/E3-hashtable.pdf
Arnaud:
Which one of the following hash functions on integers will distribute keys most uniformly over 10
buckets numbered 0 to 9 for i ranging from 0 to 2020? (GATE-CS-2015)
Solution: In uniform distribution, the function evenly distributes keys into slots of hash table.
For given hash functions, we have calculated hash values for keys 0 to 9 as the folliwing. As we can see
from the table, i^3 mod10 is distributing evenly from indexes 0 to 9. Other functions have not utilized all
indexes. Source: https://www.geeksforgeeks.org/practice-problems-on-hashing/
Mushfique:
Q: Suppose that we use an open-addressed hash table of size m to store n<= m/2 Items.
Assuming uniform hashing, show that for i = 1, 2, …,n, the probability is at most 2^(-k) that the
ith insertion requires strictly more than k probes. (Book Problem)
A: