avl-flashcards

1
Q

If node A has balance factor 2 and its left child B has balance factor 1, what case is this and what rotation do we perform?

A

This is a Left-Left (LL) case. Perform a single right rotation at node A.

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

If node A has balance factor -2 and its right child B has balance factor -1, what case is this and what rotation do we perform?

A

This is a Right-Right (RR) case. Perform a single left rotation at node A.

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

If node A has balance factor 2 and its left child B has balance factor -1, what case is this and what rotation do we perform?

A

This is a Left-Right (LR) case. Perform two rotations: 1) single left rotation at B, 2) single right rotation at A.

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

If node A has balance factor -2 and its right child B has balance factor 1, what case is this and what rotation do we perform?

A

This is a Right-Left (RL) case. Perform two rotations: 1) single right rotation at B, 2) single left rotation at A.

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

How do you calculate the balance factor of a node?

A

Balance Factor = height(left subtree) - height(right subtree). Valid values are -1, 0, and 1.

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

When do we need to rebalance an AVL tree?

A

When the balance factor of any node becomes less than -1 or greater than 1 (i.e., when the heights of a node’s two subtrees differ by more than 1).

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

After removing a node from an AVL tree, how many rotations might be needed to rebalance?

A

Several rotations might be needed. Always choose rotation based on balance factor of the node. If child’s balance factor is 0, either single or double rotation works (single rotation will always rebalance).

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

When performing rotations, what do we do with orphaned children (subtrees)?

A

If a node C is rotated to replace its parent N, and C already has a child in the direction of rotation, that child becomes the child of N in the opposite direction of rotation.

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

How do we identify which rotation case we’re dealing with?

A

1) Find smallest unbalanced subtree
2) Let a be root of this subtree
3) Let b be root of a’s taller subtree
4) Let c be root of b’s taller subtree
5) Based on positions of b and c relative to a, determine case (LL, RR, LR, or RL).

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

What is the time complexity of rebalancing operations (put and remove) in an AVL tree?

A

Both put and remove operations are O(log n), which includes the time to perform the actual operation and any necessary rotations.

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