Chapter 29 - AVL Trees Flashcards

1
Q

If a tree is perfectly balanced – i.e., a complete binary tree – its height is log n. Can we maintain a perfectly balanced tree?

A

Yes, but doing so will be costly. The compromise is to maintain a well-balanced tree – that is, the height of every node’s two subtree are about the same.

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

What is an AVL tree?

A

An AVL tree is a well balanced binary search tree.

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

For AVL trees, what does “AVL” stand for?

A

AVL trees were invented in 1962 by two Russion computer scientists, Adelson-Velsky and Landis – hence the name AVL.

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

What is the difference between the heights of every node’s subtrees in an AVL tree?

A

The difference in height is either 0 or 1. Therefore the maximum height of an AVL tree is O(logn)

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

What is the balance factor of a node in an AVL tree?

A

The balance factor of a node is the height of its right subtree minus the height of its left subtree. A node is said to be balanced if its balance factor is -1, 0, or 1. A node is considered left-heavy if its balance factor is -1, and right-heavy if its balance factor is +1.

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

What does it mean to rebalance an AVL tree?

A

After inserting or deleting an element from an AVL tree, if the tree becomes unbalanced, perform a rotation operation to rebalance the tree.

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

Describe the LL rotation.

A

An LL imbalance occurs at a node A, such that A has a balance factor of -2 and a left child B with a balance factor -1 or 0. This type of imbalance can be fixed by performing a single right rotation at A.

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

Describe the RR rotation.

A

An RR imbalance occurs at a node A, such that A has a balance factor of +2 and a right child B with a balance factor of +1 or 0. This type of imbalance can be fixed by performing a single left rotation at A.

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

Describe the LR rotation.

A

An LR imbalance occurs at a node A, such that A has a balance factor of -2 and a left child B with a balance factor of +1. Assume B’s right child is C. This type of imbalance can be fixed by performing a double rotation at A (first a single left rotation at B and then a single right rotation at A).

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

Describe the RL rotation.

A

An RL imbalance occurs at a node A, such that A has a balance factor of +2 and a right child B with a balance factor of -1. Assume B’s left child is C. This type of imbalance can be fixed by performing a double rotation at A (first a single right rotation at B and then a single left rotation at A).

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

“In order to balance the tree, you need to know each node’s height.” Where can the height of each node be stored?

A

AVLTreeNode inherits from TreeNode. The height is a new data field defined in AVLTreeNode. The data fields in TreeNode are left and right, pointing to the left and right subtree.

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

Whens is the updateHeight method invoked? When is the balancFactor method invoked? When is the balancePath method invoked?

A

updateHeight(AVLTreeNode) is invoked to update the height of a node. It is invoked to rebalance the tree. balanceFactor is invoked to check the balance factor of a node. It is invoked when a path is rebalanced. balancePath is invoked along the path where a new node is inserted or a node is deleted.

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

In the insert and delete methods, once you have performed a rotation to balance a node in the tree, is it possible that there are still unbalanced nodes?

A

No

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

Can you traverse the elements in a AVL tree using a do-while loop?

A

Sure you can, by using the pointers of the nodes.

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

What is the time complexity of the search method in AVLTree?

A

O(log n)

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

What is the time complexity of the insert method in AVLTree?

A

O(log n)

17
Q

What is the time complexity of the delete method in AVLTree?

A

O(log n)

18
Q

The ____ of a node is the height of its right subtree minus the height of its left subtree.

A. balance factor
B. depth
C. length
D. degree

A

A. balance factor

19
Q

The balance factor of every node in an AVL tree may be ___

A. 0
B. 1
C. -1
D. 2

A

All but D

20
Q

A ___ (with no duplicate elements) has the property that for every node in the tree the value of any node in its left subtree is less than the value of the node and the value of any node in its right subtree is greater than the value of the node.

A. binary tree
B. binary search tree
C. AVL tree
D. binary heap

A

A and B are correct

21
Q

The time complexity for insertion, deletion, and search is O(logn) for a ____

A. binary tree
B. binary search tree
C. AVL tree
D. binary heap

A

C. AVL tree

22
Q

In a __, the element j to be removed is always at the root.

A. binary tree
B. binary search tree
C. AVL tree
D. binary heap

A

D. binary heap

23
Q

In a ____, the element just inserted is always at the leaf.

A. binary search tree
B. AVL tree
C. binary heap

A

A. binary search tree

24
Q

The average time-complexity for insertion, deletion, and search in a ___ is O(logn).

A. binary search tree
B. AVL tree
C. binary heap

A

B. AVL tree