Data Structures - Test 2 Review Flashcards
cout «_space;value int sum = 0; if (count < 20) { total = total + current; area = (1.0 / 2.0) * base * height;
examples of O(1)
“for (int j = 1; j<= n; j++) { cout «_space;"”this prints n times”” «_space;endl; }”
example of O(n)
“for (int j = 1; j<= n; j++) { for (int k = 1; k<= n; k++) { cout «_space;"”this prints n^2 times”” «_space;endl; } }”
example of O(n^2)
“for (int j = 1; j<= sqrt(n); j++) { cout «_space;"”this prints sqrt(n) times”” «_space;endl; }”
example of O(n^1/2)
“for (int j = n; j> 0; j /= 2) { cout «_space;"”this prints lg n times”” «_space;endl; }”
example of O(lg n)
A hash table is…
a fixed size list of records organized to a unique key
The key is…
usually one of the data fields in the record
Each cell in the hash table is called a…
bucket
Buckets may be empty - wasting memory
true
The hash function maps the record’s key to an integer called the…
hash index(this indicates into which bucket to put the record)
If every key maps to a unique hash index, then the hash table operations are fast
Search, Insert, and Delete are all O(1)
A collision occurs when…
two keys map to the same hash index
One way to solve collisions?
Chaining
Let k = max # of records stored in one bucket. If using vectors then: Search and Delete are… Insert is…
O(k)O(1)
Another category of handling collisions is…
Open addressing
Open addressing includes…
Linear probingQuadratic probingDouble hashing
Double hashing uses…
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
The secondary hash function d(k) cannot have…
zero values
The table size N must be a…
prime to allow probing of all the cells
Common choice of compression map for the secondary hash function:…
d(k) = q - (k mod q)where:q < Nq is a prime
The possible values for d(k) are…
1, 2, …, q
26 mod 11
4
21 mod 11
10
21 mod 5
1
Stack operations… Last In, First Out
push, pop, topthese are all O(1)
stackADT
+ push(Type Item) : void+ pop() : void+ top() : Type+ size() : int+ empty() : bool
Expression: 6 3 + 2 * = Push 6 into stack
|| || || 6 |——-
Expression: 6 3 + 2 * = Push 3 into stack
|| || 3 || 6 |——-
Expression: 6 3 + 2 * = + Pop stack twice op2 = 3; op1 = 6;
|| || || |——-