Tutorial Solutions Week 08

Exercise 1

Balanced Trees

Explain how we could globally balance a binary search tree using the partition function. We can use partition to move the median element of a given tree to the root of the tree. By having the median element at the root of the tree, we will end up with approximately the same amount of keys in the left subtree and the right subtree. If we repeat this process for every subtree in the tree, we will have a balanced tree.

Exercise 2

Splay Trees

Exercise 3

Hashtables

Draw a hash table of size 13. Use the hash function "k%13" and insert the keys: 5, 29, 32, 0, 31, 20, 23 and 18 into your table (in that order) using the following strategies for handling collisions.

  1. Chaining
  2. Linear Probing
  3. Double Hashing with a second hash function "7 - (k%7)". (Why would a second hash function such as "k%7" not be very useful?)

If there is any remaining time, use it to finish any questions that you did not cover in previous weeks.

  1. Chaining

    Solution

  2. Linear Probing

    Solution

  3. Double Hashing with a second hash function "7 - (k%7)".

    Solution

    (Why would a second hash function such as "k%7" not be very useful?)

    Because you would get values in the range of 0 - 6. Values of 0 would not be very useful as you would just checking indexes 0 away - ie in the same spot the original collision was in.