cs 235 final BYU fall 2017

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

5. You can sometimes substitute ______ for recursion.

You can sometimes substitute iteration (loops) for recursion.

backtracking

a technique that enables you to write programs that can be used to explore different alternative paths in a search for a solution

a. Storage must be reallocated when the queue is full. b. This implementation is normally most efficient in use of storage. c. This is an existing class in the C++ STL. choose: -circular array -single linked list -double linked list -deque

a. Circular array b. Single-linked list c. Deque

Write an if statement that uses a pseudorandom number to assign "heads" or "tails" to a variable coin_flip. The probability of each should be 0.5. Assume the declaration Random my_random;.

if (my_random.next_double() < 0.5) coin_flip = "heads"; else coin_flip = "tails"; fix

Which quadratic sort's performance is least affected by the ordering of the array elements? Which is most affected?

shell sort (or any (n logn) sort)

how does the shell sort work

sort 3 mini arrays insertion sort

For a program that converts infix expressions to postfix: stack or queu

stack

what does #include <utility> include

standard library class pair

how do you access elements in middle of linked list

"walking along" O(n) while(node) if (node->data = data) break node = node->next;

circular queue (cap==6) contains the five characters +, *, -, &, and #, where + is the first character inserted. remove [0], insert '\' and '%' Draw the new queue. What is the value of q.front_index and q.rear_index?

% (q.rear_index) * (q.front_index) - & # \ q.front_index is 1; q.rear_index is 0.

circular queue (cap==6) contains the five characters +, *, -, &, and #, where + is the first character inserted. If a single-linked list were used to implement this queue: head->char = ? tail->char = ?

'*', '%'

define red-black tree

-a balanced tree with red and black nodes. -After an insertion or removal, the following invariants must be maintained for a Red-Black tree. —A node is either red or black. —The root is always black. —A red node always has black children. (A NULL reference is considered to refer to a black node.) —The number of black nodes in any path from the root to a leaf is the same. -To maintain tree balance in a Red-Black tree, it may be necessary to recolor a node and also to rotate around a node. The rebalancing is done inside the insert or remove function, right after returning from a recursive call.

define set fix (ex)

-a collection of objects. -elements contained in the set be unique

avl tree

-balanced binary tree -each node has a balance value that is equal to the difference between the heights of its right and left subtrees (hR - hL) -A node is balanced if it has a balance value of 0; a node is left-(right-)heavy if it has a balance of -1 (+1).

2-3-4 trees balancing fix

-can be balanced on the way down the insertion path by splitting a 4-node into two 2-nodes before inserting a new item. (easier than splitting nodes and rebalancing after returning from an insertion)

3 ways of implementing queue

-double-linked list -single-linked list -a circular array All three implementations support insertion and removal in O(1) time; however, there will be a need for reallocation in the circular array implementation (amortized O(1) time).

define B-tree

-generalization of a 2-3-4 tree -a tree whose nodes can store up to CAP items -The value of CAP is chosen so that each node is as large as it can be and still fit in a disk block -used as indexes to large databases stored on disk . The time to retrieve a block is large compared to the time required to process it in memory, so by making the tree nodes as large as possible, we reduce the number of disk accesses required to find an item in the index.

binary search tree (4 things)

-no duplicate node->data -0-2 children -doesn't need to be imbalanced -generally O(h) for searching

define map

-set of ordered pairs (key, value) -self ordering -keys must be unique, values can be duplicate map<int, string> mymap; mymap[1] = "a";

how does the merge sort work

-split arr in half -sort left half -sort right half -merge left&right mergeSort(first,last) { if (last-first > 1) int aL[last/2] int aR[last/2] for (a->(last/2 (i)) aL[i] = a[i]; aR[i] = a[i+(last/2)] mergeSort(aLL, ALR); mergeSort(aRL, aRR); merge; //funct that combines 2 arrs } (50 60 45 30 90 20 80 15) (50 60 45 30) (90 20 80 15) (50 60)(45 30) (90 20) (80 15) (50)(60)(45)(30)(90)(20)(80)(15) (50 60) (30 45) (20 90)(15 80) (30 45 50 60) (15 20 80 90) (15 20 30 45 50 60 80 90)

when do you rebalance an avl tree

-when a node along the insertion (or removal) path becomes critically out of balance; that is, when the absolute value of the difference of the height of its two subtrees is 2. -The rebalancing is done after returning from a recursive call in the insert or remove function.

hash codes

532 fix

heaps/priority queues

8.5 fix

how does the heapsort work

?

2-3 trees

? fix

b trees

? fix

red-black trees

? fix

define pair

??? fix pg 519

6. Explain how a recursive function might cause a stack overflow exception.

A recursive function that doesn't stop would continue to call itself, eventually pushing so many activation frames onto the stack that a stack overflow exception would occur.

a b c d e f g h i j preorder traversal

ABDGEHCFIJ

4. What three things are stored in an activation frame? Where are the activation frames stored?

An activation frame stores the following information on the stack: the function argument values, the function local variable values, and the address of the return point in the caller of the function

7. If you have a recursive and an iterative function that calculate the same result, which do you think would be more efficient? Explain your answer.

An iterative function would generally be more efficient, because there is more overhead associated with multiple function calls.

B-tree vs B+ tree

B+ tree: stores keys and pointers to data in the leaf nodes and keys and pointers to child nodes in the internal nodes. The parent of a child node is repeated as the first key of the child. Thus each node holds CAP keys and CAP pointers.

8. Binary search is an O(___) algorithm, and linear search is an O(___) algorithm.

Binary search is an O(log2 n) algorithm, and linear search is an O(n) algorithm.

10. Why did you need to provide a wrapper function for recursive functions linear_search and binary_search?

Both search functions should be called with the vector name and target as arguments. However, the recursive linear search function needs the subscript of the element to be compared to the target. The binary search function needs the search boundaries.

Each recursive call of a recursive function must lead to a situation that is ______ to the ______ case.

Each recursive call of a recursive function must lead to a situation that is closer to the base case.

Queue implemented as a single-linked list: the statement ______ would be used inside function push to store a new node whose data field is referenced by item in the queue the statement ______ would be used to disconnect a node after its data was retrieved from the queue.

For insertion: back_of_queue->next = new Node(item, NULL); To disconnect the node to be removed: front_of_queue = front_of_queue->next;

define hash tables

The C++ standard library uses a special type of binary search tree, called a balanced binary search tree, to implement the set and map classes. This provides access to items in O(log n) time. Sets and maps can also be implemented using a data structure known as a hash table, which has some advantages over balanced search trees. The goal behind the hash table is to be able to access an entry based on its key value, not its location. In other words, we want to be able to access an element directly through its key value (associative retrieval), rather than having to determine its location first by searching for the key value in an array or a tree. Using a hash table enables us to retrieve an item in constant time (expected O(1)). We say expected O(1) rather than just O(1), because there will be some cases where the performance will be much worse than O(1) and may even be O(n), but on the average we expect that it will be O(1). Hash Codes and Index Calculation The basis of hashing (and hash tables) is to transform the item's key value to an integer value (its hash code) which will then be transformed into a table index. Figure 9.3 illustrates this process for a table of size n. We discuss how this might be done in the next few examples. fix

The control statement used in a recursive function is the ______ statement.

The control statement used in a recursive function is the if statement.

activiation frames (rec)

The operating system uses activation frames, stored on a stack, to keep track of argument values and return points during recursive function calls. Activation frames can be used to trace the execution of a sequence of recursive function calls.

postorder traversal

Tl, Tr, root post(node) { post(node->left) post(node->right) cout << node->data; }

inorder traversal:

Tl, root, Tr in(node) { in(node->left); cout << node->data; in(node->right); }

9. Towers of Hanoi is an O(___) algorithm. Explain your answer.

Towers of Hanoi is an O(2n) algorithm, because each problem splits into two problems at the next lower level.

2-3 & 2-3-4 trees

Trees whose nodes have more than 0-more than 2 children -A 2-node has two children, a 3-node has three children, and a 4-node has four children. advantage: -keeping the trees balanced is a simpler process - tree may be less deep because a 3-node can have three children and a 4-node can have four children, but they still have O(log n) behavior.

define queue

an abstract data type with a first-in, first-out, (FIFO) structure. -ie reservation lists and waiting lines

What determines whether you should use a quadriatic sort or logarithmic sort

arr size (smaller = quad)

A recursive function has two cases: ______ and ______.

base case, recursive case

define deque

combines stack, queue, vector, and linked list -O(1) insertion/removal

define linked list

data structure consists of a set of nodes (which contain data and a reference to the next node in the list (and prev for double-linked)) insertion/removal == O(1)

what does #include <queue> do?

declares functions: -push: to insert at the back, -pop: to remove from front -front: to access the item at the front.

a b c d e f g h i j inorder traversal

dgbheaifjc

Write a statement that uses a pseudorandom number to assign a value of 1 through 6 to a variable die, where each number is equally likely. Assume the declaration Random my_random;/

die = 1 + 6 * my_random.next_int(); fix

what does #include <list> implement

double-linked list

internal nodes vs external nodes

ex: leaf nodes in: nodes with 1+ children

how does the selection sort work

find smallest element and put it in the first unsorted index (for size) loop (whole arr(i)) loop (find smallest element from i-end) smallest = smallest; swap(smallest, i) 35 65 30 60 20 (20) 65 30 60 35 (20)(30) 65 60 35 (20)(30)(35) 60 65 (20)(30)(35)(60) 65

A queue is a _____-in, _____-out data structure

first, first

1. Show the effect of each of the following operations on queue q. Assume that y (type char) contains the character '&'. What are the final values of x and success (type bool) and the contents of queue q? queue<char> q; bool success = true; char x; q.push('+'); x = q.pop(); x = q.pop(); success = true; q.push('('); q.push(y); x = q.pop(); success = true; 2. Write a new queue function called move_to_rear that moves the element currently at the front of the queue to the rear of the queue. The element that was second in line will be the new front element. Do this using functions push, front, and pop. 3. Answer Question 2 without using functions push or pop for a single-linked list implementation of queue. You will need to manipulate the queue internal data fields directly. 4. Answer Question 2 without using functions push or pop for a circular array implementation of queue. You will need to manipulate the queue internal data fields directly. 5. Write a new queue function called move_to_front that moves the element at the rear of the queue to the front of the queue, while the other queue elements maintain their relative positions behind the old front element. Do this using functions push, front, and pop. 6. Answer Question 5 without using push and pop for a single-linked list implementation of the queue. 7. Answer Question 5 without using functions push or pop for a circular array implementation of the queue.

fix (queue)

Defining the Compare Function

fix 528

open addressing

fix 533

full vs complete trees

full: all internal nodes have 2 children 7 1 11 0 3 9 12 2 5 8 10 4 6 complete: all nodes @ depth (h-2) and up have 2 children; at (h-1), 1 node with 1 child, siblings/cousins to left have 2 & to right have 0 children 7 3 10 1 5 9 11 0 2 4 6 8

a b c d e f g h i j postorder traversal

gdhebifjca

purpose of iterator

give us ability to access items in a container sequentially goes through list

how does the bubble sort work

go through whole arr and swap(i,i+1) if necessary (while !sorted) skip = 1; while (!sorted) { sorted = true; for (first -> (size-skip)) second = first+1 if (a[second] < a[first]) swap(first, second) sorted = false; skip++ } good if array is NEARLY sorted, but should be avoided otherwise a = 60 42 75 83 27 (60 42) 75 83 27 => (42 60) 75 83 27 42 (60 75) 83 27 => 42 60 (75 83) 27 => 42 60 75 (83 27) => 42 60 75 (27 83) (42 60) 75 27 83 => 42 (60 75) 27 83 => 42 60 (75 27) 83 => 42 60 (27 75) 83 42 60 27 (75 83) => (42 60) 27 75 83 42 (60 27) 75 83 => 42 (27 60) 75 83 42 27 (60 75) 83 42 27 60 (75 83) (42 27) 60 75 83 => (27 42) 60 75 83 27 (42 60) 75 83 27 42 (60 75) 83 27 42 60 (75 83) ===> SORTED!!!

how does the quicksort work

if sorted (and piviot is a[0]) -> bad! recursively partition arr QS(first,last) { if (last - first > 1) int pivot = partition(first, last); QS(first, pivot); QS(pivot + 1, last); } partition(first, last) { int up = first + 1; int down = last - 1; do while ((up != last - 1) && !(*first < *up)) { ++up; } while (*first < *down) { --down; } if (up < down) swap(up, down); while (up < down); swap(first, down); return down; } 44 75 23 43 55 12 64 77 33 p^ u^ d^ 44 33 23 43 55 12 64 77 75 p^ u^ d^ 44 33 23 43 12 55 64 77 75 12 33 23 43(44)55 64 77 75 recursively do this^

a recursive function has the following form:

if the problem can be solved for the current value of n Solve it. else Recursively apply the function to one or more problems involving smaller values of n. Combine the solutions to the smaller problems to get the solution to the original.

inserting algorithm for binary search trees

insert(data, node) { if node == null new node() else if data==node->data return //duplicate else if (data < node->data) insert(data, node->left) else insert(data, node->right) }

why is tree balancing necessary

it ensures that a search tree has a O(logn) behavior

2 sorts with nlogn worst-case behavior

merge, heapsort

O(?) for bubble sort (best,avg,worst)

n, n^2, n^2

O(?) for insertion sort (best,avg,worst)

n, n^2, n^2

O(?) for selection sort (best,avg,worst)

n^2, n^2, n^2

O(?) for quicksort (best,avg,worst)

nlogn, nlogn, n^2

O(?) for heapsort (best,avg,worst)

nlogn, nlogn, nlogn

O(?) for merge sort (best,avg,worst)

nlogn, nlogn, nlogn

struct vs class

only diff: struct's data is automatically declared pubic

Which quadratic sort's performance is least affected by the ordering of the array elements? Which is most affected?

selection == least affected bubble == most

3 quadratic sorts

selection, bubble, insertion (work for SMALL arrays [<100]) (insertion == best/3 usually)

circular queue (cap==6) contains the five characters +, *, -, &, and #, where + is the first character inserted. Assume that + is stored in the first position in the array. What is the value of q.front_index? What is the value of q.rear_index?

q.front_index is 0; q.rear_index is 4

Stack or a queue to determine which print job should be handled next?

queue

Explain the use of the stack and activation frames in processing recursive function calls. 2. What is a recursive data structure? Give an example of one. 3. For Towers of Hanoi, show the output lines generated by the function call show_moves(3, 'R', 'M', 'L'). Also, show the sequence of function calls. 4. For the counting cells in a blob problem, show the activation frames in the recursive calls to count_cells following count_cells(grid, 4, 1).

recursion fix

preorder traversal

root, Tl, Tr pre(node) { cout << node->data; pre(node->left) pre(node->right) }

define multimap

same as a map, but there can be duplicate keys

define multiset fix (ex)

same as a set, but can have duplicates

vectors vs arrays

same: -accessed by index diff: vectors have/can -changeable size -insert in middle without shifting -removing without shifting

how does the insertion sort work

think deck of cards -start with a[0] and -> array and put each successive [] in its correct place (probably shifting a bunch of elements) for (int i = first+1 -> last) j = i while (j != first & a[j] < a[j-1] swap (j, j-1) j-- 30) 25 15 20 28 25 30) 15 20 28 15 25 30) 20 28 15 20 25 30) 28 15 20 25 28 30)

When we allocate a new array for an vector because the current capacity is exceeded, we make the new array at least ______. This allows us to ______.

twice the size; spread out the cost of the reallocation so that it is effectively a constant time operation

huffman trees

you did a lab on this fool

4 kinds of imbalace for avl tree (fix: put examples)

—Left-Left (parent balance is -2, left child balance is -1): Rotate right around parent. —Left-Right (parent balance is -2, left child balance is +1): Rotate left around child, then rotate right around parent. —Right-Right (parent balance is +2, right child balance is +1): Rotate left around parent. —Right-Left (parent balance is +2, right child balance is -1): Rotate right around child, then rotate left around parent.

3 things recursive algorithms work

—Verify that the base case is recognized and solved correctly. —Verify that each recursive case makes progress toward the base case. —Verify that if all smaller problems are solved correctly, then the original problem must also be solved correctly.


Kaugnay na mga set ng pag-aaral

Business Skills for Technical Professionals, Chapter 7, Minimizing Stress and Avoiding Burnout, Chapter 9, Solving and Preventing Incidents and Problems - Chapter 6, Handling Difficult Customer Situations - Chapter 5

View Set

ATI Scope and Standards of Practice

View Set

Macro - 3.2 Shifts in Demand and Supply for Goods and Services

View Set

Monopolistic Competition/ Oligopoly

View Set