Maps, Hash Tables & Binary Search Trees Flashcards

1
Q

What is a Map ADT?

A

A map models a collection of key-value entries that is searchable ‘by the key’
The main operations of a map are for searching, inserting and deleting items
Multiple entries with the same key are not allowed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a Map and Priority queue?

A

Map - access any key
Priority Queue - access only the minimum key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does a map work as a doubly linked list?

A

Store the items of the map in a list S, in arbitrary order, as well as a size counter, so that getSize is O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the performance of a list-based Map?

A

put() - takes O(n) as you have to check if the key occurs in the map already
get and remove take O(n) time since in the worst case, you traverse the whole list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Hash Table and what are its time complexities for looking up keys, inserting and deleting them?

A

A CDT which is suitable for implementing maps
Convert each key into an index into a (big) array
Look-up, insertion and deletion of keys usually runs in O(1) time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a hash function?

A

Maps keys of a given type to integers in a fixed interval [0, N-1]
Example - H(k) = k mod N
The integer h(k) is called the hash value of key k

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does a hash table consist of?

A

A hash function (h)
Array (called table) of size N

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a compression function?

A

A compression function is the 2nd part of a hash function. The goal is to be applied directly on top of the hash function, to ensure the keys gets ‘dispersed’ in an ‘apparently random’ way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Separate Chaining?

A

Let each cell in the table point to a linked list of entries that map there e.g. if something collides with an entry that is already there, then it just gets added to the linked list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the problems with Separate Chaining?

A

Requires additional memory outside of the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is open addressing and what are its problems?

A

The colliding item is placed in a different cell of the table
Linear probing handles collisions by placing the next colliding item in the next available table cell
Each table cell inspected is referred to as a ‘probe’
Disadvantage - colliding items lump together, causing future collisions to cause a longer sequence of probes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you delete an element from a hash table safely?

A

Use ‘lazy deletion’ - don’t mark the entry as a blank, but as a ‘deleted’ cell and fix the entries later

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is double hashing?

A

Double hashing uses a secondary hash function d(k), which handles collisions by placing an item in the first available cell of the series.
For reference, linear probing is d(k) = 1 i.e. take the next available spot. If it doesn’t exist, then move yet 1 more space forward.
The table size N must be a prime to allow probing of all the cells

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the performance of hashing?

A

In the worst case, searches, insertion and removals on a hash table take O(n) time. Otherwise, normally O(1)
Reference - worst case occurs only when all keys collide

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does ‘load factor’ mean?

A

This affects the performance of a hash table. It is a measure of how ‘full’ the array for the hash table is. In Java, the highest this is allowed to go is 75%, and then it gets re-hashed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is re-hashing?

A

When the table gets too full, then ‘re-hash’. This creates a new, larger tables and new hash function, followed by transferring all the elements to the new table. This can be done in two ways:
All at once - O(n) amortised time
A few at a time - O(1) amortised time

17
Q

What are the differences between Hash Maps and Priority Queues?

A

Hash map does not use the ordering of keys e.g. does not implement min(). If you wish to do that, then it would take O(n) time
Priority Queue does not allow direct access to a key e.g. there is no easy way to do get(k), would have to walk through all the entries

18
Q

What is a binary search tree?

A

It is a binary tree storing key-value entries at its internal nodes and satisfying the following ‘search tree’ property:
Let u, v and w be any three nodes such that u is in the left subtree of v and w is in the right subtree of v. Then we must have key(u) < key(v) < key(w) (assuming no duplicate entries)

19
Q

How do you search within a binary search tree?

A

To search for key k, trace a downward path starting at the root. The next node visited depends on the outcome of the comparison of k with the key of the current node
If we reach a leaf, the key is not found and we return null

20
Q

How do you access the minimum key of a binary search tree?

A

Always go left. If sorted correctly, then that should be the smallest node

21
Q

How do you insert into a binary search tree?

A

Use a get(k) function. If the key has not been found, then insert the new key as a leaf node to the current node selected. If it is found, then replace the value found.

22
Q

How do you delete from a binary search tree a node with one child?

A

Remove the node, and then connect the child node to the parent of the removed node.

23
Q

How do you delete from a binary search tree a node with two children?

A

Find the internal node (w) that follows n in an inorder traversal (typically the next highest value)
Copy the key (w) into the node to be deleted.
Remove the original node w and its left child z (which if null, is fine, but if not, then follow procedure for deletion of one node)

24
Q

What is a balanced search tree?

A

A balanced search tree is when all depths have been filled.

25
Q

What is the time complexity of search, insertion and deletion for a balanced binary search tree?

A

All of the methods are O(log n)

26
Q

What is the performance of a binary search tree?

A

Space used is O(n)
Methods find, insert and remove take O(height)
Height h is O(n) in worst case, and O(log n) in best case

27
Q

What is self-balancing?

A

Constantly re-structure the trees
Keep the trees height balanced so that the height is logarithmic in the size
Performance of self-balancing is always logarithmic

28
Q

What is the issue with self-balancing?

A

Could make trees balanced using a ‘total rebuild’ but that would result in O(n)
Counter this issue by looking at the path to some recently changed node, not the entire tree, and fixing that.