Lecture 4 - Binary Search, AVL trees & sort Flashcards

1
Q

Describe the structure of a Binary Search Tree.

A

X is root,
All nodes on left of X are <= X
All nodes on right of X are >= X

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

How would you measure the height of a BST recursively?

A

Height(x) = 1 + max( height(left(x)), height(right(x)) )

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

What’s the average run time of Search, Insert, Delete in a BST?

A

Omega(h)

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

Why is it easier to determine height of tree from the depth of the lowest node?

A

All we have to do is count the nodes traversing from deepest node to the root.

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

Define a balanced BST.

A

A BST where the nodes are well distributed between left and right

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

Compare the runtime of Height(BST,n) on a completely balanced vs completely unbalanced BST.

A

h = Omega(logn) vs Omega(n)

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

Define an AVL tree.

A

BST such that the heights of the two child subtrees of any node differ by at most one. They are self-balanced BSTs.

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

What’s the runtime of Insert, Delete & Search in average and worst cases?

A

O(logn)

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

Let Nh be the minimum # nodes in an AVL tree of height h. Prove that h = O(logn)

A

Nh = 1 + Nh-1 + Nh-2
Nh > Omega(2^(h/2))
h = O (logn)

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

What does a Rotation do in an AVL

A

They allow you to change the tree structure while preserving the BST property. This allows you to restore an AVL tree after inserting a node that breaks it.

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

Why do zigzag patterns cause problems when rotating BST trees to form AVLs?

A

Because they don’t restore the property immediately, we have to remove the zig-zag pattern and put all nodes on one side of the root we’re trying to rotate.

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

What’s the running time AVL insertion?

A

O(h) insertion + 2* O(1) rotation = O(logn) insertion in AVL trees.

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

Compare the worst case runtime of BST sort and AVL sort.

A

BST sort: O(n^2)

AVL soft: O(n*logn)

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

How does an inorder traversal print a tree like A / x \ B?

A

A x B

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

Describe BST sort on a list of keys.

A
  1. Build a BST tree from the list of keys

2. print in-order traversal

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

What’s the best case and worst case running time of BST sort?

A

Omega(nlogn)

O(n^2)