Week 7 Flashcards

1
Q

Max Heap

A

Binary tree data structure in which the value of any node is always greater than that of its children (heap property)
Largest value is the root of the tree
COMPLETE binary tree. Each level is filled before the next. filling from left to right.
Add/Remove : log n
Find largest: O(1)

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

Heap underlying implementation

Formulat to find children/parent

A
Heap with max height h will have an array size of 2^h -1
Root of three at index 0
Node at index k:
- left child: index 2k +1
-right child: index 2k+2;
- parent at index (k-1)/2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Adding value to heap, process

A

Place in the rightmost available spot of the deepest level of tree -> Bubble up

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

Removing value from heap

A

Place right most element at deepest level at root position (can only remove elements from root) -> bubble down
When bubbling down it always switches with the larger to the two children nodes

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

Is Heap a BST

A

NO, it is a BT but not a BST
Futhermore, children of a given node must be smaller than node but not necessary that smaller one is left and the other right

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

How do we know we are done with bubbling down

A

Heap has not null leaves. Hence we know we are one if the indices of the left and right child of a given node are outside max capacity of array

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

What is a Priority Queue

A

Java imlementation of Heap. But Min Heap and not Max Heap.

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

Why do we require that heap uses a complete BT

A

Increase efficiency, as we have a interest to minimize height of BT

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

What can be used in Priority Queue

A

Any Java object can be used as key as long as there exists means to compare any two instances in a way that defines a natural order of the keys

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

Trie - Main appplication

A

Information retrieval
Tree like data structure that is efficient at storing strings in order to support fast pattern matching, in which each node holds a single character, an the nodes that are children of a node represent characters that follow that character in string

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

Why Trie?

A

Storing strings in BST inefficient, every node start character by character comparison from beginning of string. would only need to check last added character

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

Is Trie a Binary Tree

A

No

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

Trie Definition

A

Tree-based data structure for storing strings in order to support fast pattern matching.

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

Trie Properties - Structure

A
  1. Each node, except the root is labeled with a character
  2. The children of an non-leaf node have distinct labels
  3. Each leaf is associated with a string. Concatenate characters along path
  4. Height = length of longest string
  5. # leaves = # strings stored
  6. total number of nodes at most = n+1, n = total length of all strings
  7. Use sentinel, e.g. null to mark end of string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Trie - Time complexity

A

o(m), whre m is the length of the string, assuming a nodes stores its child nodes in an o(1) data strucutre.
if children stored e.g. in LL. need to traverse all X children of that node. complexity X*m

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

Trie delete value

A
  1. find word
  2. remove sentinel,
  3. move next node, if no children delte and move on
    else keep it and stop.