Data Structures - Test 2 Review Flashcards

1
Q

cout &laquo_space;value int sum = 0; if (count < 20) { total = total + current; area = (1.0 / 2.0) * base * height;

A

examples of O(1)

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

“for (int j = 1; j<= n; j++) { cout &laquo_space;"”this prints n times”” &laquo_space;endl; }”

A

example of O(n)

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

“for (int j = 1; j<= n; j++) { for (int k = 1; k<= n; k++) { cout &laquo_space;"”this prints n^2 times”” &laquo_space;endl; } }”

A

example of O(n^2)

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

“for (int j = 1; j<= sqrt(n); j++) { cout &laquo_space;"”this prints sqrt(n) times”” &laquo_space;endl; }”

A

example of O(n^1/2)

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

“for (int j = n; j> 0; j /= 2) { cout &laquo_space;"”this prints lg n times”” &laquo_space;endl; }”

A

example of O(lg n)

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

A hash table is…

A

a fixed size list of records organized to a unique key

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

The key is…

A

usually one of the data fields in the record

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

Each cell in the hash table is called a…

A

bucket

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

Buckets may be empty - wasting memory

A

true

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

The hash function maps the record’s key to an integer called the…

A

hash index(this indicates into which bucket to put the record)

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

If every key maps to a unique hash index, then the hash table operations are fast

A

Search, Insert, and Delete are all O(1)

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

A collision occurs when…

A

two keys map to the same hash index

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

One way to solve collisions?

A

Chaining

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

Let k = max # of records stored in one bucket. If using vectors then: Search and Delete are… Insert is…

A

O(k)O(1)

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

Another category of handling collisions is…

A

Open addressing

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

Open addressing includes…

A

Linear probingQuadratic probingDouble hashing

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

Double hashing uses…

A

a secondary hash function d(k) and handles collisions by placing an item in the first available cell of the seriesh(k,i) = (h(k) + i*d(k)) mod Nfor i = 0, 1, …, N - 1

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

The secondary hash function d(k) cannot have…

A

zero values

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

The table size N must be a…

A

prime to allow probing of all the cells

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

Common choice of compression map for the secondary hash function:…

A

d(k) = q - (k mod q)where:q < Nq is a prime

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

The possible values for d(k) are…

A

1, 2, …, q

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

26 mod 11

A

4

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

21 mod 11

A

10

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

21 mod 5

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Stack operations... Last In, First Out
push, pop, topthese are all O(1)
26
stackADT
+ push(Type Item) : void+ pop() : void+ top() : Type+ size() : int+ empty() : bool
27
Expression: 6 3 + 2 * = Push 6 into stack
| || || || 6 |-------
28
Expression: 6 3 + 2 * = Push 3 into stack
| || || 3 || 6 |-------
29
Expression: 6 3 + 2 * = + Pop stack twice op2 = 3; op1 = 6;
| || || || |-------
30
Expression: 6 3 + 2 * = op1 + op2 = 9 Push 9 into stack
| || || || 9 |-------
31
Expression: 6 3 + 2 * = Push 2 into stack
| || || 2 || 9 |-------
32
Expression: 6 3 + 2 * = * Pop stack twice op2 = 2; op1 = 9;
| || || || |-------
33
Expression: 6 3 + 2 * = op1 + op2 = 18 Push 18 into stack
| || || ||18|-------
34
Expression: 6 3 + 2 * = = Pop stack and print: 18
| || || || |-------
35
Queue operations... First In, First Out
enqueue, dequeue, frontthese are all O(1)
36
queueADT
+ enqueue(Type Item)+ dequeue()+ front() :Type+ rear() :Type
37
A technique in which one system models the behavior of another system is called...
simulation
38
A ________ _____ is a restricted form of a list.
priority queue
39
In a priority queue...
items are arranged according to their priorities (or keys)the keys may or may not be uniqueunlike a queue which is FIFO
40
A priority queue removes items based on...
priority
41
Each item in a PQ is a composition of 2 objects:
- the key (priority)- the datathis gives us a pair (key, data)which we can use to implement using a linked list
42
Two types of PQ's
min PQmax PQ
43
Min PQ
minimum key value is the highest priority
44
Max PQ
maximum key value is the highest priority
45
In either type of PQ, it must hold a...
Total Order Relation(reflexive, antisymmetric, transitive)
46
Implementation: Priority Queue Unsorted list implementation: store the items of the priority queue in a list-based sequence, in arbitrary order
4, 5, 2, 3, 1
47
Implementation: Priority Queue Unsorted list implementation Performance: _______ takes O(1) time since we can insert the item at the beginning or end of the sequence _______, _______ and _______ take O(n) time since we have to traverse the entire sequence to find the smallest key
insertItemremoveItem, min/maxKeymin/maxElement
48
Implementation: Priority Queue Sorted list implementation: store the items of the priority queue in a sequence, sorted by key
1, 2, 3, 4, 5
49
Implementation: Priority Queue Sorted list implementation Performance: _______ takes O(n) time since we have to find the place where to insert the item _______, _______, and _______ take O(1) time since the smallest key is at the beginning of the sequence
insertItemremoveItem, min/maxKeymin/maxElement
50
Queues vs Priority Queues
Queue: structure ensures items processed in the order receivedPriority queues: customers (jobs) with higher priority pushed to the front of the queue
51
Priority Queues are useful for...
sorting
52
Sorting using a priority queue: Given a sequence of N items, a PQ can be used to sort the sequence:
Step 1: Insert N items into the PQ via insertItem (key, data)Step 2: Remove the items by calling removeItem() N timesthe first removes the largest item, the second call the second largest, ..., the last removes the smallest item
53
8 mod 6
2
54
6 mod 6
0
55
12 mod 6
0
56
10 mod 6
4
57
20 mod 6
2
58
16 mod 6
4
59
What's the load factor?
(num items) / (table size)
60
Main deque operation - insertFirst(object o):
inserts element o at the beginning of the deque
61
Main deque operation insertLast(object o):
inserts element o at the end of the deque
62
Main deque operation removeFirst():
removes and returns the element at the front of the deque
63
Main deque operation removeLast():
removes and returns the element at the end of the deque
64
Main deque operation first():
returns the element at the front without removing it
65
Main deque operation last():
returns the element at the end without removing it
66
Main deque operation size():
returns the number of elements stored
67
Main deque operation isEmpty():
returns a Boolean value indicating whether no elements are stored
68
Doubly linked list operation insertFront(e):
inserts an element on the front of the list
69
Doubly linked list operation removeFront():
returns and removes the element at the front of the list
70
Doubly linked list operation insertBack(e):
inserts an element on the back of the list
71
Doubly linked list operation removeBack():
returns and removes the element at the end of the list
72
7 mod 7
0
73
14 mod 7
0