Binary Search Trees Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the time for operations on a BST in the worst case?

A

Proportional to the height of the tree

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

What is the operation for floor and ceiling?

A

Given key < root - go left
Given key > root - floor could be right if and only if there is a key smaller than or equal to the key in the right subtree. If not, then root.
Ceiling follows the same logic, less is exchanged with greater

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

How does selection work in a BST?

A

If we seek key of rank k:
* If number of keys in left tree is larger than k, look for key in left tree
* If num keys N = k, return root
* If num keys < k, look for the key of rank k - (num of keys) - 1 in right

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

How does rank work in a BST?

A
  • if given key is the key at the root, return size(left)
  • If given key is less than root, return rank(key, left)
  • If given key is larger, return 1 + size(left) + rank(key, x.right)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does deleting the min/max work in BSTs?

A

Min: Go left until finding node with null left link and replace with right link
Max: Go right until node with null right and replace with left

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

How do we delete a node with one child?

A

Link replacement from parent to child

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

How do we delete any node with 2 children?

A
  • Save link to t
  • Set x to point to successor min(t.right)
  • Set right link of x to deleteMin(t.right)
  • Set left link to t.left.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly