Week 8 Flashcards

1
Q

What is a heap?

A

A heap is a tree based data structure. Mainly used for priority queues and order statistics.

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

What are the different types of heaps?

A

Binary heaps
Binomial heaps
Fibonnacci heaps

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

What is a binary heap?

A

A complete binary tree, which is a binary tree in which all the levels are completely filled except possibly the lowest one which is filled from the left.

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

How can a binary heap be implemented as a Max heap?

A

The largest element is always the root node.

The value of each nodeis greater than the value of its children.

The tree is complete.

The tree is perfectly balanced.

The leaf nodes in the last level are all pushed to the left.

Height is O(log n)

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

How can we implement a heap as a min heap?

A

Similar to max except the value of each node is less than the value of its children.

The root contains the smallest element.

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

Are heaps perfectly ordered?

A

No, heaps are not perfectly ordered.

Order is only maintained through linear lines of descent between parent and children only.

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

How can heaps be implemented in arrays?

A

Elements are stored from top to bottom and from left to right at each level.

The root node is always at index = 0.

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

In an array heap, at where is the position of the left child and the right child if the node index is i?

What is the position of a parent of a node at index = 1?

A

The position of the left child is 2i+1

The position of the right child is 2i+2

The position of the parent of the node is (i-1)/2

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

How can a heap be implemented in a priority Queue?

A

Enqueue procedure:
add the new element to the end of the heap.

If necessary restore the heap property by swapping the element with its parent until the proper position is found or we are at the root.

Dequeue procedure
Remove the root element (highest priority)
Replace it with the last leaf node.

If necessary restore the heap property by swapping the rooy with its larger child, (reheaping downwards).

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

What is the complexity of top up heap creation in the worst case?

A

O(nlogn)

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

What is the worst case complexity for bottom up heap creation.

A

O(n) is the worst case.

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

What are the steps for top down heap creation.

A

Start with empty heap
Sequentially enqueue new elements

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

What are the steps for bottom up heap creation.

A
  1. Start with the last non-leaf node (n/2)-1
  2. Set index to this position
  3. If necessary restore the heap property by swapping with the largest child.
  4. Repeat until proper place found or becomes a leaf node.
  5. Repeat step 2 after decrementing index. Stop once the root has been processed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is heapsort? What is the procedure?

A

Is an inplace sort of an array.

Procedure:
Reorganize the array into a Max heap
Bottom up method is quickest
for (i = n-1; i>0; iā€“)
Swap root with element i
Restore heap property for elements 0 to i-1

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

In heapsort, what is the worst case, average case and best case complexity.

A

Is O(nlogn) in worst and average cases
Is O(n) in the best case for an array containing identical elements.

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

Why do we like balanced trees?

A

Searches and insertions are most efficient when a binary tree is well balanced. O(log n)

17
Q

What is node balance?

A

Is the height of the right subtree minus theheight of the left subtree:

balance(n) = rightHeight(n)-leftHeight(n)
Where n is some node in the tree.

18
Q

What is an AVL tree?

A

Is an ordered binary tree where every node has a balance of -1 or 0 or +1.

19
Q

What is the general procedure for inserting into an AVL tree?

A
  1. Insert the node into the tree, following the rules for a regular binary search tree.
  2. If necessary, adjust the shape of the tree so that it conforms to the rules of an AVL tree.
  3. Update the balance fields for all nodes affected by the steps above.
20
Q

In an AVL tree, what is a pivot node?

A

Is the ancestor node closest to the inserted node that is not in balance.

21
Q

What are the 3 different cases when inserting in an AVL tree?

A
  1. There is no pivot
  2. The pivot exists and you add the shorter subtree.
  3. the pivot exists and you add the longer subtree.
22
Q

What do you do when you are inserting in an AVL tree with no pivot?

A

You are adding to a subtree with 0 balances.
So you change the balances for all ancestor nodes by + or - 1.

The shape of the tree is not adjusted after the insertion, but the balances must be updated.

Insert the node into its proper place in the tree.

Adjust the balances for all nodes from the inserted node up the root node.

23
Q
A