java final cards v6
An empty binary tree has height
-1
A binary tree with just one node has height
0
The predecessor of a node in a binary tree is called its
parent
A binary tree traversal method that visits the root first, and then recursively traverses the left and right subtrees is called
preorder traversal
Adding all items from a list to a certain data structure and then removing them one at a time yields a sorted version of the list. The data structure is a
priority queue
O(n*n) is ________.
quadratic time
Postorder traversal of a binary tree
recursively traverses the left subtree, then traverses the right subtree, then visits the root
Insertion Sort
1) Looks at one item at a time finding largest/smallest value 2) Looks at next item to place in sorted list **One item at a time** Separates the list into sorted and unsorted sections (sorted section initially empty). Compares an item in the unsorted section with the items in the sorted section, and places it in the correct position in the sorted section. After each step, the sorted section gets bigger, and the unsorted section gets smaller, until the entire list is sorted. Orders a value by repetitively inserting a particular value into a sorted subset of the list - Consider the first item to be a sorted sublist of length 1 - Insert the second item into the sorted sublist, shifting the first item if needed - Insert the third item into the sorted sublist, shifting the other items as needed - Repeat until all values have been inserted into their proper positions
For a sorted list of 1024 elements, a binary search takes at most ________ comparisons.
11
A complete binary tree with N nodes may be stored in an array A of length N by storing the root at A[0], and then storing in successive array locations the nodes of the tree in increasing order of the level of nodes. If nodes in the same level are stored in left to right order, then the node stored at A[k] will be a leaf if and only if
2k+1 ≥ N
A list of 60 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to find a specified name or confirm that it is not in the list?
6
A database containing 500 sorted integers must be searched using a binary search algorithm. What is the maximum number of iterations of the binary search method that must occur in order to find a specified value or guarantee that it is not in the database?
9
How many times would the while loop execute if you first do int[] arr = {6, 12, 19, 54, 76, 99} and then call binarySearch(arr,76)? (A) 2 (B) 1 (C) 3
A
In which of these cases will an ascending order (from smallest to largest) insertion sort have the fastest run time? I. An array that is in reverse order (from largest to smallest). II. An array that is in sorted order already (from smallest to largest). III. An array that is in random order (not already sorted). (A) II only (B) I only (C) I and II only (D) II and III only (E) III only
A
Under what condition will an insertion sort execute faster? (A) If the data is already sorted in the correct order (B) It will always take the same amount of time to execute
A
Which of the following correctly shows the iterations of an ascending (from left to right) insertion sort on an array with the following elements: {7,3,8,5,2}? (A) {3,7,8,5,2}, {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8} (B) {2,3,8,5,7}, {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8} (C) {3,7,8,5,2}, {3,5,7,8,2}, {2,3,5,7,8} (D) {2,3,8,5,7}, {2,3,5,8,7}, {2,3,5,7,8} (E) {2,7,3,8,5}, {2,3,7,8,5}, {2,3,5,7,8}
A
Which will cause the shortest execution of a sequential search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array
A
array list
A Java class that implements a dynamically growable array of objects
Binary Tree 25.2
A data structure that consists of nodes, with one root node at the base of the tree, and two nodes (left child and right child) extending from the root, and from each child node.
Suppose you want to store students and perform the operations to insert and delete students. Which data structure is best for this application?
A linked list
________ is a data structure to store data in sequential order.
A list
Merge sort 23.4
A list is split into individual lists, these are then combined (2 lists at a time).
Merge Sort
A list is split into individual lists, these are then combined (2 lists at a time). Uses the divide and conquer strategy. Recursively breaks the values to be sorted in half, until there is only one value left to be sorted. Then, it merges the two sorted lists back into one list. Orders values by recursively dividing the list in half until each sub-list has one element, then recombining - divide the list into two roughly equal parts - recursively divide each part in half, continuing until a part contains only one element - merge the two parts into one sorted list - continue to merge parts as the recursion unfolds DIVIDING ITSELF IN HALF REPEATEDLY OR MERGING SORTED ELEMENTS*
treeMap 21.5
A map in which the keys are sorted
inorder traversal 25.6
A node is visited after its left subtree and before its right subtree
Queue 24.5
A queue represents a waiting list. It can be vieed as a
Stacks 24.5
A stack can be viewed as a special type of list whose elements are accessed, inserted, and deleted only from the end (top).
The elements in a long list of integers are roughly sorted in decreasing order. No more than 5% of the elements are out of order. Which of the following is a valid reason for using an insertion sort rather than a selection sort to sort this list into decreasing order 1. There will be fewer comparisons of elements for insertion sort 2. There will be fewer changes of position of elements for insertion sort 3. There will be less space required for insertion sort. (A) I only (B) 2 only (C) 3 only (D) I and 2 only (E) 1, 2 and 3
A. E.g consider the array 10 8 9 6 2 For insertion sort - need 4 passes through this array 1st pass compares 8 and 10 - 1 comparison, no moves 2nd pass compares 9 and 8 and then 9 and 10. The array becomes 10 9 8 6 2 - 2 comparisons, 2 moves The 3rd and 4th pass compares 6 and 8 and 2 and 6 - no moves In summary there are approximately 1 or 2 comparisons per pass and no more then 2 moves per pass For selection sort, there are 4 passes too. The 1st pass finds the biggest element in the array and swaps it into the first position. The array is still 10 8 9 6 2 - four comparisons. There are 2 moves if your algorithm makes the swap in this case, otherwise no moves. The 2nd pass finds the biggest element from a[1] to a[4] and swaps it into the second position: 10 9 8 6 2 - 3 comparisons, 2 moves For the 3rd pass there are 2 comparisons and 1 for the 4th. There are 0 or 2 moves each time. Summary: 4+3+2+1 total comparisons and a possible 2 moves per pass Selection sort makes the same number of comparisons irrespective of the state of the array. Insertion sort does far fewer comparisons if he array is almost sorted. There are roughly the same number of data movements for insertion and selection. Insertion may have more changes depending on how far from their insertion points the unsorted elements are. Insertion and selection sorts have the same space requirements
A complete binary tree with N nodes may be stored in an array A of length N by storing the root at A[0], and then storing in successive array locations the nodes of the tree in increasing order of the level of nodes. If nodes in the same level are stored in left to right order, then the parent of the node stored at A[k] will be stored at
A[(k-1)/2]
A complete binary tree with N nodes may be stored in an array A of length N by storing the root at A[0], and then storing in successive array locations the nodes of the tree in increasing order of the level of nodes. If nodes in the same level are stored in left to right order, then the left child of the node stored at A[k] will be stored at
A[2k+1]
A complete binary tree with N nodes may be stored in an array A of length N by storing the root at A[0], and then storing in successive array locations the nodes of the tree in increasing order of the level of nodes. If nodes in the same level are stored in left to right order, then the right child of the node stored at A[k] will be stored at
A[2k+2]
To add a new element X to a heap
Add X as a leaf, taking care to preserve the completeness of the heap. If X is now the root, or is greater than its parent, stop. Otherwise, repeatedly swap X with its parent until X becomes the root, or becomes greater than its parent.
Array Lists 24.3
An array is a fixed-size data structure. Once an array is created, its size cannot be changed. Nevertheless, you can still use arrays to implement dynamic data structures. The trick is to create a larger new array to replace the current array, if the current array cannot hold new elements in the list.
Map 21.5
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
A database containing 2,000 sorted integers must be searched using a binary search algorithm. What is the maximum number of iterations of the binary search method that must occur in order to find a specified value or guarantee that it is not in the database? (A) 8 (B) 11 (C) 20 (D) 100 (E) 2000
B
A list of 120 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to find a specified name or confirm that it is not in the list? (A) 5 (B) 7 (C) 10 (D) 12 (E) 128
B
How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86} and then call binarySearch(arr,2)? (A) 1 (B) 2 (C) 3
B
Under what condition will an ascending insertion sort execute the slowest? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute
B
Which will cause the shortest execution of a binary search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array
B
A worst case situation for insertion sort would be 1. A list in correct sorted order 2. A list sorted in reverse order 3. A list in random order. (A) I only (B) II only (C) III only (D) I and II only (E) II and III only
B. If the list is sorted in reverse order, each pass through the array will involve the maximum possible number of comparisons and the maximum possible number of element movements if an insertion sort is used
Array unsortedArr contains an unsorted list of integers. Array sortedArr contains a list of integers sorted in increasing order. Which of the following operations is more efficient for sortedArr than unsortedArr? Assume the mist efficient algorithms are used. 1. Inserting a new element 2. Searching for a given element. 3. Computing the mean of the elements A. 1 only B. 2 only C. 3 only D. 1 and 2 only E. 1, 2 and 3
B. Inserting a new element is quick and easy in an unsorted array - just add it to the end of the list Computing the mean involves finding the sum of the elements and dividing by n, the number of elements. The execution is same whether the list is sorted or not. Operation 2, searching for a given element is inefficient for an unsorted list, since a sequential search must be used. In sortedArr, the efficient binary search algorithm, which involves a fewer comparisons could be used. In fact, in a sorted list, even a sequential search would be more efficient than for an unsorted list: If the search item were not in the list, the search could stop as soon as the list elements were greater than the search item.
Big O Notation 22.3
Big O Notation is a way to represent how long an algorithm will take to execute. Measures algorithm time complexity
Which search is a divide and conquer method?
Binary
The ___________________ algorithm sorts values by repeatedly comparing neighbouring elements in the list and swapping their position if they are not in order relative to each other.
Bubble sort
Which of the following algorithms should stop if no swaps of elements have taken place on a pass through the list?
Bubble sort
A list of integers containing [12, 8, 7, 30, 62, 45, 10, 3] is sorted from largest to smallest using a selection sort method. After three passes, what does the list look like? (A) [62, 45, 30, 12, 7, 8, 10, 3] (B) [30, 12, 8, 7, 62, 45, 10, 3] (C) [62, 45, 30, 7, 12, 8, 10, 3] (D) [62, 45, 30, 12, 7, 8, 10, 3] (E) [12, 8, 30, 7, 62, 45, 10, 3]
C
Under what condition will a merge sort execute faster? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute
C
Under what condition will a selection sort execute faster? (A) If the data is already sorted in ascending order (B) If the data is already sorted in descending order (C) It will always take the same amount of time to execute
C
Which of the following conditions must be true in order to search for a value using binary search? I. The values in the array must be integers. II. The values in the array must be in sorted order. III. The array must not contain duplicate values. (A) I only (B) I and II (C) II only (D) II and III
C
Which sort should be the fastest most of the time? (A) selection sort (B) insertion sort (C) merge sort
C
Which of the following is a valid reason why merge sort is a better sorting algorithm than insertion sort for sorting long, randomly ordered lists ? 1. Merge sort requires less code than insertion sort 2. Merge sort requires less storage space than insertion sort. 3. Merge sort runs faster than insertion sort. (A) I only (B) 2 only (C) 3 only (D) I and 2 only (E) 2 and 3 only
C. Merge sort requires both a merge and mergeSort method - more code than the relatively short and simple code for insertion sort. The merge algorithm uses a temporary array, which means more storage space than insertion sort. For long lists, the "divide-and-conquer" approach of merge sort gives it a faster run time than insertion sort
A feature of data that is used for a binary search but not necessarily used for a sequential search is A. Length of the list B. type of data C. order of data D. smallest value in the list E. median value of the data
C. The Binary search algorithm depends on the array being sorted. The sequential search has no ordering requirement. Both depend on choice A, the length of the list, while the other choice are irrelevant to both algorithms
Suppose list1 is an MyArrayList and list2 is a MyLinkedList. Both contains 1 million double values. Analyze the following code: A: for (int i =0; i<100000; i++) list1.add(i); B: for (int i = 0; i< 100000; i++) list2.add(i);
Code fragment A runs as fast as code fragment B.
Suppose list1 is an MyArrayList and list2 is a MyLinkedList. Both contains 1 million double values. Analyze the following code: A: while (list1.size()>0) list1.remove(0); B: while (list2.size()>0) list2remove(0);
Code fragment B runs faster than code fragment A.
Bubble Sort
Compares each item with the next item in the list, and swaps them if needed. Imagine that the largest/smallest item in the array (depending on sorting order) is being raised to the top of the list. Stops when it makes a pass through the entire list without swapping. Orders a list of values by repetitively comparing neighbouring elements and swapping their positions if necessary. - scan the list, exchanging adjacent elements if they are not in relative order; this bubbles the highest value to the top - scan the list again, bubbling up the second highest value repeat until all elements have been placed in their proper order
10 out of 10 points Analyze the following code: public class Test { public static void main(String[] args) { Map<String, String)map = newHashMap<String,String>(); map.put("123", "John Smith"); map.put("111", "George Smith"); map.put("123", "Steve Yao"); map.put("222", Steve Yao"); } }
Correct Answer: Correct After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".
Consider an array of integers that contains [12, 8, 4, 6, 13, 29, 7]. If the array is sorted from smallest to largest using an insertion sort method, what will be the order of the array after the third iteration of the sorting method? (A) [4, 6, 12, 8, 13, 29, 7] (B) [4, 6, 7, 8, 13, 29, 12] (C) [4, 8, 12, 6, 13, 29, 7] (D) [4, 6, 8, 12, 13, 29, 7] (E) [4, 6, 7, 8, 12, 13, 29]
D
The array intArray contains [8, 12, 34, 6, 10, 14, 2, 4]. What are the contents of intArray after 3 passes of insertion sort? (A) [6, 8, 12, 34, 2, 4, 10, 14] (B) [2, 4, 6, 34, 10, 14, 8, 12] (C) [2, 4, 34, 6, 10, 13, 8, 12] (D) [6, 8, 12, 34, 10, 14, 2, 4] (E) [6, 8, 10, 12, 34, 14, 2, 4]
D
Which of the following correctly shows the iterations of an ascending (from left to right) selection sort on an array with the following elements: {10, 6, 3, 2, 8}? (A) {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,10,8}, {2,3,6,8,10} (B) {6,10,3,2,8}, {3,6,10,2,8}, {2,3,6,8,10} (C) {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,8,10} (D) {2,6,3,10,8}, {2,3,6,10,8}, {2,3,6,10,8}, {2,3,6,8,10}
D
Which will cause the longest execution of a sequential search looking for a value in an array of integers? (A) The value is the first one in the array (B) The value is in the middle of the array (C) The value is the last one in the array (D) The value isn't in the array
D
Assume that merge sort will be used to sort an array arr of n integers into increasing order. What is the purpose of the merge method in the merge sort algorithm ? A. Partition arr into 2 parts of roughly equal length, then merge these parts. B. Use a recursive algorithm to sort arr into increasing order. C. Divide arr into n subarrays, each with 1 element. D. Merge 2 sorted parts of arr into a single sorted array. E. Merge 2 sorted arrays into a temporary array that is sorted.
D Recall the merge sort algorithm 1. Divide arr into 2 parts 2. Merge sort the left side 3. Merge sort the right side. 4. Merge the 2 sides into a single sorted array Merge method is used for the last step of the algorithm. It does not do any sorting or partitioning of the array - eliminates choices A,B and C. Choice E is wrong because merge starts with a single array that has 2 sorted parts.
A sorted array of integers containing 2000 elements is to be searched for key using a binary search method. Assuming key is in the array, what is the maximum number of iterations needed to find key? (A) 8 (B) 10 (C) 100 (D) 2000 (E) 11
E
Which of the following is/are true about using insertion sort versus using merge sort? I. Insertion sort requires more storage space than mergesort. II. Insertion sort is only more efficient than mergesort in the case that we have a very small and nearly sorted array. III. Insertion sort is almost always less efficient than mergesort. (A) I only (B) II only (C) III only (D) I and III (E) II and III
E
The decision to choose a particular sorting algorithm should be made based on 1. Run-time efficiency of the sort 2. size of the array 3. space efficiency of the algorithm A. 1 only B. 2 only C. 3 only D. 1 and 2 only E. 1, 2 and 3
E. The time and space requirements of sorting algorithms are affected by all three of the given factors, so all must be considered when choosing a particular sorting algorithm.
Insertion Sort 23.2
Each items is take in turn, compare to the items in a sorted list and placed in the correct position.
A linear search always requires more comparisons than a binary search.
False
Bubble sort is the most efficient searching algorithm
False
Linear Search
Finding a key value within a list, ++More efficient w/ sorted list Linear Search - In linear search, each element of the array is compared with a search key. A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted This approach does not assume the items in the search pool are in any particular order
sequential search algorithm
Finds the index of a targeted value in an array, regardless of if it is sorted. Starts from the beginning of the array, and goes through the entire array until it finds the target value.
Suppose your program frequently tests whether a student is in a soccer team and also need to know the student's information such as phone number, address, and age, what is the best data structure to store the students in a soccer team?
HashMap
Suppose your program frequently tests whether a student is in a soccer team, what is the best data structure to store the students in a soccer team?
HashSet
Three concrete classes of Set 21.2
HashSet , TreeSet , and LinkedHashSet
Which of the following conditions must be true in order to search for a value using binary search? I. The values in the array must be integers. II. The values in the array must be in sorted order. III. The array must not contain duplicate values.
II only
LinkedList is more efficient than ArrayList for which of the following operations?
Insert/delete an element in the middle of the list. Insert/delete an element in the begging of the list.
The __________________ algorithm sorts a list of values by repetitively inserting a particular value into a subset of the list that has already been sorted.
Insertion sort
Quick sort 23.5
It divides the array into two parts so all the elements in the first part are less than or equal to the pivot, and all the elements in the second part are greater than the pivot.
In a binary search, ______________________________.
It is assumed that the search pool is ordered In a binary search, ______________________________.
LinkedHashMap 21.5
Like a regular HashMap, except it can remember the order in which elements (name/value pairs) were inserted, or it can be configured to remember the order in which elements were last accessed.
A __________________ search looks through the search pool one element at a time.
Linear
If you want to store non-duplicated objects in the order in which they are inserted, you should use ________.
LinkedHashSet
Binary Search Algorithm
Loop: 1) Must be sorted first 2) Divide list into two 3) Analyze two lists weather it is in one locationN Finds the index of a targeted value in a SORTED array. Uses a "search space" (initially the entire array) to find the target. Each step, it compares the median value in the search space to the target. Based on the comparison, it can remove half of the search space every step, until the search space consists of only the target value.
Bubble sort 23.3
Moving through a list repeatedly, swapping elements that are in the wrong order.
selection sort algorithm
Nested Loop 1) repeatedly finds the largest/smallest values in the unsorted section 2) Swaps to the correct position, placed in sorted section **Swap** Finds the largest/smallest item in the array (depending on sorting order), and then swaps it with the item in the next position to be filled. For example, the first step finds the largest/smallest item in the array, then swaps it with the first item in the array, and so on. Orders a list of values by repetitively putting a particular value into its final position - find the smallest value in the list - switch it with the value in the first position - find the next smallest value in the list - switch it with the value in the second position - repeat until all values are in their proper places example: 3 6 1 7 --> 1 3 6 7
Constant Complexity 22.3
O(1) Constant time algorithms will always take the same amount of time to be executed. The execution time of these algorithms are independent of the size of the input Quickest algorithm int value = values[1051];
Exponential Complexity 22.3
O(2^n) the number of operations are proportional to the exponent of the size of input if I were trying to hack a password, I would have to examine each record in the password store for each variation of the password -Referred to as a brute force attack
Logarithmic Complexity 22.3
O(log n) An algorithm has a logarithmic time complexity if the time it takes to run the algorithm is proportional to the logarithm of the input size n. for (int i = 1; i < n; i = i * 2) { System.out.println(i); }
The worst-time complexity for binary search is ________.
O(logn)
The heap sort method has a worst case complexity function that is in
O(n log n)
The worst-time complexity for linear search is ________.
O(n)
Linear Complexity 22.3
O(n) An algorithm has a linear time complexity if the time to execute the algorithm is directly proportional to the input size n. Therefore the time it will take to run the algorithm will increase proportionately as the size of input n increases for (int i = 1; i <= n; i++) { // }
Quadratic complexity 22.3
O(n^2) An algorithm has quadratic time complexity if the time to execution it is proportional to the square of the input size. It takes 4 times longer to execute when the number of items increases by a factor of 10 for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { // } }
Cubic Complexity 22.3
O(n^3) An algorithm is said to run in cubic time if the running time of the three loops is proportional to the cube of n. for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++) { for ( k = 0; k < n; k++) { statement; } } }
Quick Sort
Orders values by partitioning the list around one element, then sorting each partition - choose one element in the list to be the partition element - organize the elements so that all elements less than the partition element are to the left and all greater are to the right -apply the quick sort algorithm (recursively) to both partitions
Which data structure is appropriate to store patients in an emergency room?
Priority Queue
postorder traversal 25.6
Process all nodes of a tree by recursively processing the left subtree, then processing the right subtree, and finally the root.
Which data structure is appropriate to store customers in a clinic for taking flu shots?
Queue
Which of the following algorithms is most easily expressed recursively?
Quick Sort
Which of the following operations are supported by a list?
Retrieve an element from this list. Correct Insert a new element to this list. Correct Delete an element from this list. Correct Find how many elements are in this list. Correct Find whether an element is in this list.
ArrayList is more efficient than LinkedList for which of the following operations?
Retrieve an element given the index.
Which of the data types below does not allow duplicates?
Set
Suppose the rule of the party is that the participants who arrive later will leave earlier. Which data structure is appropriate to store the participants?
Stack
TreeSet 21.2.3
The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used
Tree Traversal 25.6
The process of systematically visiting every node in a tree once. The three most common traversals are: pre-order, in-order, and post-order.
Which will cause the shortest execution of a binary search looking for a value in an array of integers?
The value is in the middle of the array
HashSet 21.2.1
There is no particular order for the elements in a hash set
Bucket sort 23.7
These algorithms sort the elements by comparing their keys
Quick sort works by separating a list into two lists, and recursively sorting the two lists using quick sort
True
With each comparison, a binary search eliminates approximately half of the items remaining in the search
True
T/F All the sorting algorithms have greater run times than a sequential search.
True. This is because a sequential search looks at each element once. A sorting algorithm, however, processes other elements in the array for each element it looks at.
Nodes 24.4.1
When a new element is added to the list, a node is created to contain it. Each node is linked to its next neighbor.
When will a sequential search be faster than a binary search?
When it is the first one
Which will cause the longest execution of a sequential search?
When it is the last one
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is ________.
[1, 2, 3, 5, 6]
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is ________.
[1, 5]
priority queue 24.6
a queue in which the highest-priority elements are removed first; within a priority value, the earliest arrival is removed first.
A binary tree stores items that have a natural order in such a way that at each node X, all items stored in the left subtree of X are less than the item stored at X, and all items stored in the right subtree are greater than the item stored at X. Such a binary tree is called
a binary search tree
An AVL tree is
a binary search tree in which the heights of the subtrees at each node differ by at most one
Linked Lists 24.4
a collection of nodes arranged so that each node contains a link to the next node in the sequence
Let X be a node in a binary tree. Any node on the path from X to a leaf is called
a descendant of X
recursion function
a function that calls itself
A node in a binary tree that has no children is called
a leaf
LinkedHashSet 21.3
a map in which elements are stored in certain order (insertion order or access order)
Let X be a node in a binary tree. Any node on the path from X to the root is called
an ancestor of X
set 21.1
any collection of nonduplicate elements
The order condition that characterizes a heap states that
at each node X, any element stored in a child of X must be greater than the element stored at X
An input that results in the shortest execution time is called the ________.
best-case input
The successor of a node in a binary tree is called its
child
O(1) is ________.
constant time
HashSet 21.2.1
data is stored in a table with an associated hash code. Has code is used as an index. Contain NO duplicates, elements kept in order.
A binary tree with depth d is complete if
each level L < d has 2L nodes and all nodes at level d are as far to the left as possible
A binary tree is a collection of nodes in which
each node has at most one predecessor and at most two successors
Abstractset class 21.2
extends AbstractCollection and partially implements Set
You can use index to traverse elements in a set.
false
for-each loop
for (Type variable: collection){ Statement }
Let X be a node in a binary tree. The collection of all descendants of X
form a binary tree called the subtree rooted at X
On an average, linear search searches ________.
half of the list
Breadth First Traversal 25.6
he nodes are visited level by level. First the root is visited, then all the children of the root from left to right, then the grandchildren of the root from left to right, and so on
A sorting algorithm based on a priority queue is
heap sort
To add a new element X to a binary search tree:
if the tree is empty, make X the root of a new tree; otherwise, compare X to the root, if X is less, put it in the left subtree, if it is greater, put in the right subtree
Binary trees have been used
in compilers and interpreters to represent expressions
As the number of items in a search pool grows, the number of comparisons required to search ______________.
increases
A binary tree traversal method that recursively traverses the left subtree, then visits the root, then traverses the right subtree is called
inorder traversal
set 21.2
interface extends the Collection interface,
A priority queue is
is a collection that allows elements to be added, but only allows the minimum element to be removed
Heap Sort 23.6
is a comparison-based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the minimum element and place the minimum element at the beginning. We repeat the same process for the remaining elements.
The depth of a binary tree
is the length of the longest path from the root to a leaf
The level of a node X in a binary tree
is the length of the path from the root to X
Depth First Traversal 25.6
is to visit the root then recursively visit its left subtree and right subtree in an arbitrary order. The preorder traversal can be viewed as a special case of depth-first traversal, which recursively visits its left subtree then its right subtree
O(n) is ________.
linear time
A complete binary tree with N nodes has depth approximately equal to
log N
O(nlong) is ________.
log-linear time
load factor 21.2.1
measures how full the set is allowed to be before its capacity is increased.
A binary tree with no root
must be empty
Preorder Traversal 25.6
root, left subtree, right subtree
2D array
rows and columns, arranged in a grid format, but still stored in contiguous, or side-by-side memory, accessed using two index values.
Which of the following algorithms will have the m smallest items in correct order after the m^th pass has been completed?
selection sort
The length of the longest path from the root of a binary tree to a leaf is called
the height of the tree
Consider the operation of deleting the root of a binary search tree. If the root is a leaf, then
the reference to the root of the tree should be set to null
Consider the operation of deleting the root of a binary search tree. If the root has two children, then
the root node should be removed, then the least node in the right subtree should be deleted and used to replace the root
When a new item is added to an AVL tree
the tree may become unbalanced in four different ways, but because of mirror images, there are really only two fundamentally different imbalances
In a binary tree,
there must be at most one node with no predecessor
Estimating algorithm efficiency is ________.
to estimate their growth function
The keys must be unique for Map.
true
If two objects o1 and o2 are equal, what are the values for o1.equals(o2) and o1.hashCode() == o2.hashCode()?
true true
A binary tree with height 1 must have
two or three nodes
Traversing a binary search tree in inorder
will yield a sorted sequence
To find the minimum element stored in a heap
you should look at the root of the binary tree
To find the minimum node in a binary search tree
you should start at the root, and then keep passing from each node to its left child until you come to a node with no left chid