CSCI 211 Chapter 11-14 Questions

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

For a well-designed hash table, searching requires _____ on average.

0(1)

MD5 always produces a

128-bit hash value.

A new BST is built by inserting 255 nodes in sorted order. What is the tree height?

254

Given a max-heap with levels 0, 1, 2, and 3, with the last level not full, after inserting a new node, what is the maximum possible swaps needed?

3

What is the worst case (largest) number of nodes visited when searching for a key?

3. In the worst case, the node does not exist or is a leaf. In such a worst case, the search will visit a node in level 0 (the root 300), then a node in level 1 (100 or 900), then a node in level 2 (25, 145, 750, or 925).

What is the maximum loop iterations for a perfect binary tree with 7 nodes, if no node matches?

3. The tree has 3 levels. Iteration 1 checks the first level, 2 the second, 3 the third (which assigns current with null because a leaf has no children). Iteration 4 never occurs because the current node is null.

Assume a perfect 7-node BST. How many algorithm loop iterations will occur for an insert?

3. The tree has 3 levels. Iteration 1 visits the first level. Iteration 2 visits the second level. Iteration 3 visits the third level, whose node has no children, and inserts the new node as that node's child.

What is the maximum loop iterations for a perfect binary tree with 7 nodes, if a node matches?

3. The tree has 3 levels. The worst case is that the matching node is a leaf. Iteration 1 checks the first level, 2 the second, 3 the third (and returns).

A new BST is built by inserting 255 nodes in random order. What is the minimum possible tree height?

7. A tree with all levels full (except perhaps the last level) has height h=⌊log2255⌋=⌊7.994⌋=7. The minimum height occurs if all levels are full. 255 nodes would have levels filled as: 1, 2, 4, 8, 16, 32, 64, and 128, summing to 255. Although one sees 8 levels, the height is 7 because the root is at height 0 (hence the floor operation).

Assume a perfect 255-node BST. How many algorithm loop iterations will occur for an insert?

8. The tree has 8 levels. Each iteration visits a level, descending left or right depending on the new and current nodes' keys. A main benefit of a BST is that inserts require only O(logN) iterations to find the proper insert location in a nearly-full N-node tree.

What is the maximum loop iterations for a perfect binary tree with 255 nodes?

8. The tree has 8 levels. Each iteration visits one level.

Which set operation is not commutative? Union Intersection Difference

Difference

Is the tree a binary tree?

Each binary tree node may each have up to two children; no ordering is required.

Suppose a treap is built by inserting nodes with main keys in this order: A, B, C, D, E, F, G. The treap will have 7 levels, with each level having one node with a right child.

False A BST would have such a structure. But upon each insert into a treap, the random priority may cause a rotate up, which forces some nodes to become left children. The tree is thus likely to be balanced.

The Pop and Peek operations both return the value in the root, and therefore have the same worst-case runtime complexity.

False. Both functions return the value in the root, but the Pop function removes the value and the Peek function does not. Pop is worst-case O(logN) and Peek is worst-case O(1).

A treap's nodes have random main keys. True False

False. A node being inserted already has a main key. The key is used to form a BST.

Heapsort uses recursion. True False

False. The only function Heapsort calls is MaxHeapPercolateDown, which is not recursive. So, the Heapsort algorithm is not recursive.

An array sorted in ascending order is already a valid max-heap.

False. A max-heap can be used to sort an array, but a sorted array may not be valid max-heap

BSTRemoveKey will not properly update parent pointers when a non-root node is being removed.

False. All cases properly update parent pointers. When removing a non-root node, BSTReplaceChild properly updates parent pointers.

MaxHeapPercolateDown checks the node's left child first, and immediately swaps the nodes if the left child has a greater key.

False. All children are checked before any child key is moved into the parent. If greater than the parent's key, the maximum child key is moved up into the parent.

When a hash table is initialized, all entries must be empty-after-removal.

False. All entries must be initialized to empty-since-start.

Calling SetMap on set X always produces a set with the same number of elements as X.

False. Although SetMap adds X items to the result set, the map function may produce the same result for different elements

Encryption and decryption are synonymous. True False

False. Although the encryption and decryption processes can be similar, the two terms exist to distinguish between the process of altering the data into a 'hidden' form (encryption) and reconstructing back into a 'readable' form (decryption).

A hash value can be used to reconstruct the original data. True False

False. Although true for some hashing functions, MD5 and many others produce a hash value that cannot be used to reconstruct the original data.

The loop in SetIsSubset always performs N iterations, where N is the number of elements in subsetCandidate.

False. As soon as an element in subsetCandidate is found to be missing from set, false is returned, even if many elements from subsetCandidate have not yet been checked.

BSTInsert will not work if the tree's root is null.

False. At the beginning of BSTInsert, a null root node is handled as a special case. The node will be inserted as the tree's root.

The worst-case time complexity for BSTGetHeight is O(log N), where N is the number of nodes in the tree.

False. BSTGetHeight is called for every node in the tree, so the time complexity is O(N), not O(log N).

BSTGetParent always returns a non-null node when searching for a null node.

False. BSTGetParentRecursive cannot be used to search for a null node. The node's key is accessed in the function, so the function may fail if the node is null.

BSTRemoveNode uses BSTRemoveKey. True False

False. BSTRemoveNode never calls BSTRemoveKey.

The search algorithm stops only when encountering a bucket containing the key being searched for.

False. Encountering a bucket with the key being searched for is not the only case that stops the search algorithm. Probing N buckets or encountering an empty-since-start bucket will also stop the search.

BSTRemoveKey will not work if the key is not in the tree.

False. For a key not in the tree, BSTSearch returns null. The null node is then passed to BSTRemoveNode, which returns after the first if statement. The tree is not altered.

A hash table implementation must use only one criteria for resizing.

False. Multiple criteria can be used together. Ex: Resizing could occur when either the load factor exceeds 0.5 or the number of items in a bucket exceeds 11.

When the removal algorithm finds the bucket containing the key to be removed, the bucket is marked as empty-since-start.

False. Removal must mark the bucket as empty-after-removal.

Calling SetFilter on set X always produces a set with the same number of elements as X.

False. SetFilter doesn't include any elements that do not satisfy the predicate, so the resulting set may have fewer than X elements.

The Caeser cipher is an encryption algorithm that works well to secure data for modern digital communications.

False. The Caeser cipher is very easy to decrypt, even by hand. Modern encryption algorithms are far more advanced and secure than the Caeser cipher.

The insertion algorithm can only insert into empty-since-start buckets.

False. The insertion algorithm can insert into empty-since-start and empty-after-removal buckets. Whichever bucket is encountered first in the probing sequence will be used for the insertion.

BSTRemoveNode will not work to remove the last node in a tree.

False. The last node in a tree is the root, and the case (node == tree⇢root) is encountered. The tree's root pointer is set to the root's right child, which is null, thus clearing the tree.

In a hash table using chaining, the load factor cannot exceed 1.0.

False. The load factor is the number of items divided by the number of buckets. Multiple items can be placed in one bucket when using chaining. So the hash table could have more items than buckets, making the load factor > 1.0.

If the current node matches the key, when does the algorithm return the node?

Immediately. No additional searching is needed; the algorithm returns the node immediately.

Removing a node from an N-node nearly-full BST has what computational complexity?

O(logN). The computation is dominated by searching for the node, which is O(logN). The actual removal is just a few pointer updates.

Which is not an advantage of storing password hash values, instead of actual passwords, in a database?

Password hash values do not compress passwords, so storage space is not saved when storing hash values.

Double hashing would never resolve collisions if the second hash function always returned 0.

The index formula becomes (h1(key)+i∗0)mod(tablesize) when the second hashing function returns 0. Regardless of the value of i, the formula would always yield the same index.

Heapsort's worst-case runtime is O(N log N). True False

True

In MaxHeapPercolateUp, the while loop's condition nodeIndex > 0 guarantees that parentIndex is >= 0.

True

MaxHeapPercolateDown has a precondition that nodeIndex is < arraySize.

True

When implementing a priority queue with a heap, no operation will have a runtime complexity worse than O(logN).

True Push and pop operate have runtime O(logN). All other operations happen in constant time.

A treap's nodes have random priorities. True False

True Upon an insert, the treap insert operation assigns the node with a random priority, then percolates the node up until the heap property is not violated.

BSTGetHeight would also work if the recursive call on the right child was made before the recursive call on the left child.

True The order of the two recursive calls does not matter. As long as the 2 recursive calls are made and the greater of the 2 returned heights is used, BSTGetHeight will properly determine the tree height.

Every set is a subset of itself. True False

True. A set will contain every element of itself, and therefore is a subset of itself.

BSTReplaceChild will not work if the parent pointer is null.

True. At the beginning of BSTReplaceChild, the parent's left child is accessed. The parent parameter must be non-null.

BSTRemoveKey uses BSTRemoveNode. True False

True. BSTRemoveKey finds the node with the key, then calls BSTRemoveNode to remove the node.

All calls to BSTRemoveNode to remove a non-root node will result in a call to BSTReplaceChild.

True. Cases 3 and 4 in BSTRemoveNode call BSTReplaceChild directly. Case 1 recursively calls BSTRemoveNode on the successor node, which is guaranteed to be an internal node with 1 child or a leaf node, thus encountering case 3 or 4.

Both SetFilter and SetMap will call the function passed as the second argument for every element in the set.

True. Every element is tested with the predicate when filtering, and every element transformed with the map function when mapping.

For X to be a subset of Y, the number of elements in Y must be greater than or equal to the number of elements in X.

True. For all elements in set X to also be in set Y, Y must have at least the same number of elements in X.

The base case for BSTGetParentRecursive is when subtreeRoot is null or is node's parent.

True. If subtreeRoot is null or node's parent, the function returns without making a recursive call.

A filter predicate must return true for elements that are to be added to the resulting set, and false for elements that are not to be added.

True. The filter predicate, by definition, has this behavior.

BSTGetParent returns null when the node argument is the tree's root.

True. The first call to BSTGetParentRecursive compares the root against the root's children, not finding a match. Recursive calls are made to search the remaining tree levels, ultimately not finding a parent and returning null.

When resizing to a larger size, the load factor is guaranteed to decrease.

True. The load factor is computed as X / Y, where X is the number of items and Y the number of buckets. Resizing will not change X. When Y increases, the fraction X / Y decreases. Therefore, the load factor always decreases when resizing to a larger size.

The removal algorithm searches for the bucket containing the key to remove. If found, the bucket is marked as empty-after-removal.

True. The removal algorithm searches for the bucket containing the key to remove. If found, the bucket is marked as empty-after-removal.

Cryptography is used heavily in internet communications. True False

True. Virtually all logins into internet accounts use encryption to securely transmit a username and password to a web server.

BSTRemoveNode may use recursion. True False

True. When an internal node with 2 children is being removed, BSTRemoveNode uses recursion to remove the successor node.

In a hash table using open addressing, the load factor cannot exceed 1.0.

True. With open addressing, one bucket holds at most one item. So a table with N buckets can have at most N items, making the maximum possible load factor N / N = 1.0.

If the child to be visited is null, when does the algorithm return null?

Upon exiting the loop. To avoid having checks for null within the last two branches of the algorithm, the algorithm only checks for null in the loop condition.

When X and Y do not have any elements in common, which is always true?

X \ Y = X Because Y does not contain any elements that also exist in X, X \ Y is equivalent to X.

Which is true for any set X? X ∪ X = X ∩ X X ∪ X = X \ X X \ X = X ∩ X

X ∪ X = X ∩ X

Perfect BST with N = 7

[log2N] + 1

A 100 element hash table has 100 _____. items buckets

buckets

A hash function computes a bucket index from an item's _____.

key

Using left, current, and right, what ordering will print the BST from largest to smallest? Ex: An inorder traversal uses left current right.

right current left. A reverse inorder traversal visits nodes from largest to smallest. The resulting order would be: 925 775 750 300 201 100 25 11.

BSTGetHeight returns 0 for a tree with a single node.

true

MaxHeapPercolateUp works for a node index of 0. True False

true

The base case for BSTGetHeight is when the node argument is null.

true. When the node argument is null, BSTGetHeight returns -1 and does not make any recursive calls.

Given N nodes, what is the height of a max-heap?

⌊logN⌋


Kaugnay na mga set ng pag-aaral

NR 206 Analyzing Data to Make Accurate Clinical Judgments

View Set

Intro. to Java Programming, Ninth Edition - Ch.4

View Set