COSC Test3
How can a heap be used to sort an array?
- Step 1: To sort n values in an array, you can use a max-heap. Inserting one entry into a heap requires O(log n) time. Inserting all n array elements into a heap requires O(n x log n) time. - Step 2: Now you can remove the largest entry from the heap (maintaining the heap property) in O(log n) time. Put it at its proper position in the array. Keep doing this until you have removed all n entries (total time is O(n x log n)). - When you add the times from steps 1 and 2, the sum is still O(n x log n). So heap sort, as this algorithm is known, has the same efficiency as merge sort and the average case for quick sort.
How is a heap useful in implementing a priority queue?
- To implement a priority queue, you need to be able to efficiently remove the entry with the highest priority. - If larger values have higher priority, you can use a max-heap. The largest value is always in the root node. You can remove the largest value and maintain the heap property in time O(log n). You can insert a new entry in its proper place in the heap (priority queue) in O(log n) time. - if smaller values have higher priority, you can use a min-heap.
In the header file for the class ArrayDictionary, the text declares which methods as private?
- destroyDictionary(); - findEntryIndex();
What is a balanced binary tree?
A binary tree in which the left and right subtrees if aby node have heights that differ by at most 1.
What is the difference between binary search and a binary search tree?
Binary search refers to searching for a value in a sorted array using the binary search algorithm. A binary search tree is a binary tree that meets certain requirements
You define a hash table so that each location table[i] is itself an array. This array is referred to as a what?
bucket
Heap and semiheap are both
complete binary tree
In an array-based implementation of a heap, the parent of the node in items[ i ] is always stored in ______.
items[(i-1)/2]
When you begin at the hash location and search the dictionary sequentially, this is called what
linear probing
What is the implementation of the ADT dictionary which has efficiency O(1) for addition?
unsorted linked based
If a file contains items in a specified order, how must they be added to put them into a binary search tree in that same order?
use the method add
According to the text, for the ADT dictionary, in which of the following situations would a sorted array-based implementation be appropriate for frequent retrievals?
when you have duplicate search keys
Locating a particular item in a balanced binary search tree of n nodes requires at most ______ comparisons.
⎡log2(n + 1)⎤
Worst-case efficiency of a binary search tree
O(n)
Worst-case efficiency of sequential search of an array
O(n)
When you design an abstract data type, you concentrate on what its operations do and how you will implement them.
false
In a hashing implementation of a Dictionary, what is a collision?
A collision occurs when the hash function maps two or more search keys to the same index. That is, when you are attempting to add an entry and the index that the hash function gives for the key is already occupied by an entry with a different key.
What is a heap?
A heap is a complete binary tree where every node satisfies the heap order property.
In a maxheap, what does the add operation do to add an item to the heap?
A new node containing the item is added at the bottom of the tree. At this point, all nodes in the tree satisfy the heap order property except for the newly added node. The item in the new node must be moved up through the heap until it reaches a node where it satisfies the heap order property in an operation called bubble-up or trickle-up. If the value is larger than the value in its parent, the values are swapped. This process is repeated until the value reaches a node where its parent contains a larger value, or it is placed in the root node.
What is a leaf?
A node that has no children.
What is a subtree?
A subtree is a tree node and all its descendants (and the edges that connect them).
How can an array be efficiently transformed into a maxheap?
A subtree that consists of a single node is a heap (it contains a larger value than any child since it has no children). So every leaf in a tree forms a subtree that is a heap. Consider the array to be a heap where some nodes do not satisfy the heap order property. All leaves satisfy the heap order property. Starting with the last node that is not a leaf and moving back toward the root, perform a trickle-down operation on the value in each node to thurn that subtree into a heap.
What is the worst-case efficiency of a traversal operation on a binary tree?
A traversal operation requires that each node in the tree be visited. The efficiency is O(n).
What is a binary tree?
A tree in which each node has a maximum of two children, and each child is designated as the left or right child and is sorted according to the values in its nodes.
What is a parent? What is a child? What is a root?
A tree is a non-linear, hierarchical data structure that consists of nodes and edges. Each node contains data and links to zero or more successors which we call the children of the node. Every node (except one) has exactly one predecessor which we call the parent of the node. Every tree that contains nodes has exactly one node that has no predecessor which we call the root of the tree. A tree can be empty (contains no nodes).
Briefly describe how hashing is used to implement a Dictionary.
An array is used to store the entries. A special function called a hash function uses the search key of an entry to calculate the index where that entry should be stored.
The lines between the nodes of a tree are called ______.
Edges
(T/F) An ADT dictionary should never allow duplicate search keys
False
(T/F) It is insufficient for hash functions to operate only on integers.
False
(T/F) On average, quadratic probing and double hashing require more comparisons than linear probing.
False
(T/F) Quadratic probing causes no clustering at all.
False
(T/F) The getValue(searchKey) method for an ADT dictionary retrieves the specified search key for a given value.
False
(T/F) The values of the growth-rate function O(log2n) grow faster than the values of the growth-rate function O(n)
False
What kind of function tells you where a dictionary entry is currently or will be placed if it is new?
Hash Function
In a hashing implementation of a Dictionary, what is the time complexity of an add, retrieve or remove an entry?
If the Dictionary uses a good hash function and the entries are evenly distributed and the array is not too full, all these operations can be "instantaneous". That is, in good hash table these operations are O(1).
For a binary search tree implementation of a Dictionary, what is the average case time complexity of the add, retrieve and remove operations?
If the binary search tree is balanced (or almost balanced), the add, retreive and remove operations are O(log n). If the bib=nary search tree is not balanced, these times could become O(n).
In a maxheap, what does the remove operation do to remove the value in the root?
It first replace the value in the root with the value in the last node of the heap and delete the last node. This step likely results in a semiheap - the nodes in the root's left subtree form a heap, and the nodes in the right subtree form a heap, but the value in the root does not satisfy the heap order property. You convert the semiheap to a heap in an operation sometimes called the trickle-down step. The value in the root node is moved down through the heap until it reaches a node where it satisfies the heap order property. This is done by comparing it to the values stored in its children. If one or both children contains a larger value, the value is exchanged with the value in the child containing the largest. This process is then repeated until the value reaches a node where it is larger than the values in any children, or until it reaches a leaf node
Why is it somewhat difficult to implement a binary tree using an array-based structure?
It is difficult because you must represent a non-linear structure (tree) in a linear structure (array).You use an array of nodes where the links are represented using indexes. You must write code to manage the portion of the array that contains nodes that are not currently part of the tree (a free list of nodes). In other words, you have to provide memory management code that simulates what dynamic memory allocation does for you in C++.
NOT ON THE TEST
NONRECURSIVE TRAVERSLAL pg 487 -491
In the class BinaryNode, what value is stored in rootPtr if the tree is empty?
NullPtr
What is the height of a tree as defined in the textbook?
Number of nodes in the longest path of the tree. Including the root.
In an array-based implementation of a heap, the add operation is ______.
O(log n)
In an array-based implementation of a heap, the remove operation is ______.
O(log n)
What is the worst-case efficiency of a retrieval operation on a balanced binary search tree?
O(log n)
Worst-case efficiency of a balanced binary search tree
O(log n)
Worst-case efficiency of binary search of an array
O(log n)
The heapsort is ______ in the worst case
O(n * log n)
In a hashing implementation of a Dictionary, describe 2 general techniques for dealing with collisions.
One approach is to just choose a different (available) index in the Dictionary to store the entry. This is called open addressing. For example, if the index given by the hash function is occupied by another key, just search sequentially starting at that index for an available index. This is called linear probing. A second approach is to restructure the array. Instead of storing a single entry at each index, each location in the array is itself a container that can hold multiple values. For example, each location could be an array (called a bucket). Or each location in the array could be a linked chain (called separate chaining).
What is the difference between a binary search and a search of a binary search tree?
Performing a binary search is always O(log n)
What is the classification of the method getHeightHelper?
Protected
What ADT's is value oriented?
Sorted List
Describe the ADT Dictionary.
The ADT Dictionary is a data structure that manages data by value. An entry in a Dictionary typically consists of several values where one of those values is designated as the search key. The entries are stored in order by search key.
For an array-based sorted list implementation of a Dictionary, what is the worst case time complexity of the add, retrieve and remove operations?
The add and remove operations are O(n) because these operations involve moving entries. The retrieval operation can use a binary search and is O(log n).
Why is it natural to implement a binary tree using a link-based structure?
The edges in the binary tree can naturally be represented using links (pointers). Nodes can be dynamically allocated and de-allocated as necessary.
How are the sorted list and the binary search tree similar?
The entries in a sorted list can be accessed in sorted order by traversing the list. The entries in a binary search tree can be accessed in sorted order using an inorder traversal.
What is the heap order property for a max-heap?
The value in a node is greater than or equal to the value in each of its children. If every node in a heap satisfies the max-heap order property, then the root will contain the largest value.
What is the heap order property for a min-heap?
The value in a node is less than or equal to the value in each of its children. If every node in a heap satisfies the min-heap order property, then the root will contain the smallest value.
What is the worst-case efficiency of a retrieval operation on a binary search tree?
The worst case is when each node in the tree has at most one child. In this case the tree will resemble a linked list and the worst case efficiency is O(n).
(T/F) A binary search is impractical with a link-based implementation of the ADT dictionary
True
(T/F) Dictionary traversal is inefficient when using hashing
True
(T/F) The client code should not be able to modify an entry's search key once that entry is in the dictionary.
True
(T/F) The height of a binary search tree is sensitive to the order in which items are inserted into the tree.
True
(T/F) The in order traversal is only applicable to binary trees.
True
(T/F) The traverse method visits all dictionary entries.
True
What is a complete binary tree?
a binary tree that is full down to level h-1, with level h filled in from left to right.
What is it called when a hash function maps two or more search keys into the same integer?
a collision
Where are descendant nodes of node n located?
a leaf
What do you do if you remove a node from a tree in an array-based implementation?
add it to the free list
In the class ArrayDictionary declared by the text, which of the following methods bears the responsibility for keeping the array items sorted?
add( );
What is a full binary tree?
all nodes that are at a level less than h have two children each.
Since a heap is a complete binary tree, what would be an efficient basis for its implementation?
array based
According to the text, for the ADT dictionary, what implementation should be used when you do not know the maximum size of the dictionary?
binary search tree
What is the implementation of the ADT dictionary which has efficiency O(log n) for addition?
binary search tree
What is the best choice when attempting to add a new entry to a dictionary where the search key already exists?
deny the attempt
In an array-based implementation of a heap, the number of array items that must be swapped to transform a semiheap of n nodes into a heap is ______.
log2 (n + 1)
Locating a particular item in a binary search tree of n nodes requires at most ______ comparisons.
n
Where is a subtree of node n rooted?
node n
To copy a tree, traverse it in __________ and add each data item visited to a new node
preorder
To save a binary search tree in a file and then restore it to its original shape, what type of traversal should be used?
preorder
The process of enlarging a hash table and computing new hash indices for its contents is called
re-hashing
Which of the following is not an ADT dictionary operation?
sort the entries
Which of the following dictionary operations has efficiency O(n) no matter what implementation if the ADT dictionary is chosen?
traversal