AVL Trees Flashcards

1
Q

In terms of balance factor, when would you do a double rotation?

A

When parent and child are heavy in opposite directions, thus have opposite signed balance factors.
A right left rotation is required when the child is left heavy (bf < 0) and parent is right heavy (bf > 0)
A left right rotation is required when the child is right heavy (bf > 0) and the parent is left heavy (bf < 0)

Left-right imbalance – N is left-heavy and N’s left child C is right-heavy
Right-left imbalance – N is right-heavy and N’s right child C is left-heavy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A node that is left heavy has a positive or negative balance factor?

A

negative. balanceFactor(N) < 0

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

Rotations are always done with respect to x nodes

A

3

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

When we say that a node is left heavy, what does that tell us about the height of the subtrees rooted at that node

A

Balance factor is less than 0 and the left subtree of the node has greater height than the right

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

A node that is right heavy has a positive or negative balance factor?

A

positive. balanceFactor(N) > 0

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

A right rotation moves nodes in a _______ direction, with the center moving ________ and nodes to its left moving ________.

A

clockwise, downwards, upwards

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

The shape of a non-balancing BST depends to a great degree on the…

A

order of which values/keys are added into the tree

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

What is the balance factor of a BST node and how do you calculate it?

A

The balance factor of a BST node tells us how balanced the subtree at that node is.
The balance factor of a subtree rooted at some node N is calculated by:
balanceFactor(N) = height(N.right) - height(N.left)

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

Time complexity of AVL insert and remove operations?

A

O(logn) This is because every time an insertion and removal occurs, the tree is rebalanced to keep the tree height within constant factor of logn. The maximum number of times the O(1) rebalance function can be called is h, the height of the tree when upwards traversal starts at the very bottom of the tree.

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

A _______ is a simple operation that restructures an isolated region of the tree by performing a limited number of pointer updates that result in one node moving ______ in the tree and another node moving ______

A

rotation, upwards, downwards

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

A tree is balanced when all nodes have _____ approximately _____ or less

A

when we talk about balance in the context of BSTs, we’re referring to trees in which all nodes have depth approximately log n or less.

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

Whats the purpose of a double rotation?

A

A double rotation aligns imbalances on the same side to create a left-left imbalance or a right-right imbalance.

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

When would you do a double rotation as opposed to just one?

A

When you insert a node to the left and then right, or when you insert a node to the right and then left. LR-Insert causes LR-imbalance

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

When is a BST considered height balanced?

A

A BST is height balanced if, at every node in the tree, the subtree heights of the node’s left and right subtrees differ by at most 1.

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

Time complexity of the rebalance function alone?

A

O(1) because at most there will be 2 rotations per node.

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

Inserting into an AVL tree with n nodes requires O(logn) rotations, true or false?

A

False, we need at most 2 rotations

17
Q

When does an AVL tree check a tree’s balance?

A

After insertion or removal operations

18
Q

A rotation (i.e., single or double) will be needed any time an insertion into or removal from an AVL tree leaves the tree (temporarily) with a node whose balance factor is either __ or __

A

-2 or 2

19
Q

What is the height of a NULL node (or empty subtree)?

A

-1

20
Q

Time complexity of a single rotation?

A

O(1) because there is a constant number of pointers updated and updating the height of two nodes is constant because height is a property of a node

21
Q

Whats the relationship between height balance and balance factor (hint: height balance means the balance of the entire tree)

A

We can say that a BST is height balanced when the balance factor of all nodes is between -1, 0 or 1

22
Q

When we say that a node is right heavy, what does that tell us about the height of the subtrees rooted at that node

A

Balance factor is greater than 0 and the node’s right subtree has greater height than its left subtree.

23
Q

What quantifier is used to measure if a tree is balanced or not?

A

height

24
Q

When we’re working with a BST that’s more-or-less balanced, then O(h) operations will be fast, since in a balanced BST, h will be approximately _____. However, in a very unbalanced BST, h will be closer to ____, in which case operations on that BST could run much more slowly.

A

log n , n

25
Q

Inserting into an AVL tree with n nodes requires looking at O(logn) nodes, true or false?

A

True because AVL trees are balanced

26
Q

An AVL tree’s operations include mechanisms to ensure that the tree always exhibits ____________

A

height balance

27
Q

If all nodes in a BST are height balanced, we can guarantee that…

A

Height balance is an important concept because it has been shown that a BST exhibiting height balance (i.e., one in which no node has subtrees that differ in height by more than 1) is guaranteed to have an overall height that’s within a constant factor of log(n).

28
Q

Why is balance important in a binary search tree?

A

Because the primary operations on BSTs all have O(h) runtime complexity, where h is the height of the tree. If a tree is unbalanced, then we get closer to O(n) time complexity

29
Q

Whenever height balance is lost, an AVL tree should ______

A

rotate/rebalance

30
Q

A left rotation moves nodes in a ________ direction, with the center moving _______ and nodes to its right moving _______.

A

counterclockwise, downwards, upwards