Ordered Map and BST Flashcards

1
Q

What is an Ordered Map

A

Similar to a map, but with the keys being ordered.

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

2 Implementations of Ordered Map

A
  1. Array -> very inefficient
  2. Binary Search Tree
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the BST Property

A

Everything to the left of a vertex has a smaller value. Everything to the right of a vertex has a greater or equal value to the vertex. Unlike min / max heap.

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

3 Implementations of BST

A
  1. Sequential Array (inefficient)
  2. Parent Array (inefficient)
  3. Nodes (best)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Time Complexity of Search

A

O(h) -> Not the same as O(logn) because BST is unlikely to be a perfect tree. If current node value too large, search from left child, else search from right child.

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

Time Complexity of Insertion

A

O(h) -> Same logic as in search, insert the node at the right place and recursively assign the node as the appropriate child.

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

Time Complexity of GetMax

A

O(h) -> Recursively visit the right child until you reach a node with no right child, then return the node’s value.

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

Time Complexity of GetMin

A

O(h) -> Recursively visit the left child until you reach a node with no left child, then return that node’s value.

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

Time Complexity of Successor

A

O(h) -> If the node with value x has a right child, the minimum of the right subtree is the successor. If it doesn’t, keep going up until it finds a parent a that is the left child of parent b. Return parent b’s value.

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

Time Complexity of Predecessor

A

O(h) -> If the node with value x has a left child, the maximum of the left subtree is the predecessor. If it doesn’t, keep going up until it finds a parent a that is the right child of parent b. Return parent b’s value.

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

Time Complexity of ListSorted

A

O(3n) = O(n) -> Do in-order traversal: If the current node is null, return. Else visit the left child. Process the current node value. Visit the right child. 3n because you visit each node 3 times.

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

Time Complexity of Deletion

A

Overall O(h)

  1. Vertex v has no children -> O(1): Just remove v
  2. Vertex v has 1 child -> O(1): Connect child to parent.
  3. Vertex v has 2 children -> O(h): Find the successor x of v. Replace v.key with x.key. Recursively delete x in v.right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Time Complexity of Rank

A

O(h) -> Check if value is smaller, larger, equal to value of current node. If smaller, recursively return rank(curr.left). If equal, return curr.left.size + 1. If larger, return curr.left.size + 1 + rank(curr.right).

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

Time Complexity of Select -> given a rank, return the value of the node with that rank

A

O(h) -> Check if rank needed is smaller than rank of current node (curr.left.size + 1), equal or larger. If equal, return the current node’s value. If smaller, recursively call with curr.left and rank k. If larger, recursively call with curr.right and rank (k - rank(curr))What

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

What is the worst case for a BST

A

When height = N. To prevent this, we use AVL trees.

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