Data Structures Final

¡Supera tus tareas y exámenes ahora con Quizwiz!

Ch 8 - What is height?

The maximum level.

CH 12 - Show how the values in the array in Exercise 1 would be arranged immediately before the first recursive call to QuickSort.

If the pivot used in QuickSort is the first element in the array, then it will look like this: [14, 7, 10, 23, 18, 4, 19, 5, 43, 66]

CH 12 - for count going from 0 through numValues - 1 InsertItem(values, 0, count)

InsertionSort

CH 12 - What is the best, average and worst case Big O notation for HeapSort?

O(N log2 N) for all

CH 12 - What is the best, average and worst case Big O notation for MergeSort?

O(N log2 N) for all

Ch 8 - What is the Big-O notation for the destructor function in a binary search tree?

O(N)

CH 12 - What is the best, average and worst case Big O notation for SelectionSort?

O(N^2) for all

CH 9 - What is the Big O notation for Enqueue and Dequeue for a heap?

O(log2N)

CH 9 - What is the Big O notation for a balanced binary search tree?

O(log2N)

Ch 8 - What is the Big-O notation for the DeleteItem function in a binary search tree?

O(log2N)

CH 9 - What is the Big O notation for Dequeue for a linked list?

O(1)

CH 9 - A priority queue is implemented as a linked list, sorted from largest to smallest element. a) How would the definition of PQType change?

The definition for the PQType would change by since a priority queue is different from a normal queue since instead of using a FIFO data structure, values come in and out by order to priority.

Ch 8 - What is a level?

The distance of a node from the root; the root is level 0

Ch 7 - 9. a - Given the following function: int Func(int num) { if (num == 0) return 0; else return num + Fun(num + 1); } Is there a constraint on the values that can be passed as a parameter for this function to answer the Smaller-Caller Question?

The function does not end if you pass a positive number as an argument.

CH 12 - Cut the array in half MergeSort the left half MergeSort the right half Merge the two sorted halves into one sorted array

MergeSort—Recursive

Ch 7 - 2. f - True or false? If false, correct the statement. Recursive functions: must always contain a path that does not contain a recursive call.

True

Ch 7 - 32. True or False? Tail recursion often indicates that the problem could be solved more efficiently using iteration.

True

Ch7 - 2. a - True or false? If false, correct the statement. Recursive functions: often have fewer local variables than the equivalent nonrecursive routines.

True

CH 12 - Show the contents of the array after the fourth iteration of BubbleSort

BubbleSort: [-7, 10, 23, 18, 43, 4, 19, 5, 66, 14]

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 What is the height of the tree?

4

CH 9 - A priority queue containing characters is implemented as a heap stored in an array. The precondition states that this priority queue cannot contain duplicate elements. Currently, the priority queue holds 10 elements, as shown below. What values might be stored in array positions 7-9 so that the properties of a heap are satisfied? [z, f, j, e, b, g, h, ?, ?, ?]

7: D, 8:C, 9:A

Ch 8 - What is a leaf node?

A tree node that has no children.

Ch7 - The case for which the solution can be stated nonrecursively

Base case

CH 12 - What is the best, average and worst case Big O notation for Insertion Sort?

Best: O(N)(*), Average: O(N), Worst: O(N)

Ch7 - 1. d) Explain what is meant by the following: binding time

Binding time is the point in time where the variables of a program are assigned to physical memory addresses.

CH 12 - In what cases, if any, is the bubble sort O(N)?

Bubble sort is O(N) when the input list is already sorted.

CH 12 - How many comparisons would be needed to sort an array containing 100 elements using SelectionSort if the original array values were already sorted? a)10,000 b)9,900 c)4,950 d)99 e)None of the above

C, 4950. 1 + 2 + 3 + 4...(n-1) (100*99)/2 = 4950

Ch 7 - 22. a - True or false? If false, correct the statement. A recursive solution should be used when: computing time is critical.

False, the recursive solution is not always the fastest.

CH 9 - A binary tree is stored in an array called treeNodes, which is indexed from 0 to 99, as described in the chapter. The tree contains 85 elements. Mark each of the following statements as True or False, and correct any false statements. e) The tree has seven levels that are full, and one additional level that contains some elements.

False

CH 9 - A binary tree in which all of the leaves are located on the same level and every nonleaf node has two children.

Full binary tree

Ch7 - The case for which the solution is expressed in terms of a smaller version of itself

General (recursive) case

CH 9 - A complete binary tree, each of whose elements contains a value that is is greater than or equal to the value of each of its children

Heap

Ch7 - When a chain of two or more function calls returns to the function that originated the chain

Indirect recursion

CH 12 - Computation in which multiple operations are performed simultaneously.

Parallel processing

CH 12 - In what cases, if any, is quick sort O(N2)?

QuickSort is O(n^2) when it's the worst case runtime.

CH 12 - The number of possibilities for each position; the digits in a number system

Radix

Ch7 - A solution that is expressed in terms of (1) smaller instances of itself and (2) a base case

Recursive algorithm

Ch7 - A function in which the function being called is the same as the one making the call

Recursive call

Ch7 - A definition in which something is defined in terms of a smaller version of itself

Recursive definition

CH 12 - Set current to the index of first item in the array while more items in unsorted part of array Find the index of the smallest unsorted item Swap the current item with the smallest unsorted one Shrink the unsorted part of the array by incrementing current

SelectionSort

CH 12 - Given the array [26, 24, 3, 17, 25, 24, 13, 60, 47, 1] tell which sorting algorithm would produce the following results after four iterations: [1, 3, 13, 17, 25, 24, 24, 60, 47, 26]

SelectionSort

CH 12 - In what cases, if any, is the selection sort O(log2N)?

SelectionSort is O(n2) in all cases, so it isn't O(log2N) in any cases.

CH 12 - Show the contents of the array after the fourth iteration of SelectionSort

SelectionSort: [-4, 5, 7, 10, 18, 43, 19, 23, 66, 14]

CH 12 - Computation that proceeds one step at a time.

Serial (sequential) processing

Ch7 - 1. a) Explain what is meant by the following: base case

The base case is the section of a recursive algorithm that is not defined in terms of itself. It's the part of the function where recursion stops.

Ch 7 - Use the following function when answering Exercises 7 and 8: int Puzzle(int base, int limit) { if (base > limit) return -1; else if (base == limit) return 1; else return base*Puzzle(base+1, limit); } 7 b) Identify the following: the general case or cases of the function Puzzle

The general case(s) of the function puzzle is: If (base < limit) { return base * Puzzle(base + 1, limit); }

Ch7 - 1. b) Explain what is meant by the following: general (or recursive) case

The general or recursive case is the part of a recursive algorithm that is defined in terms of itself. It's the part of the algorithm that calls itself.

Ch7 - Assuming that the recursive calls work correctly, does the entire function work correctly?

The general-case question

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 What nodes are on Level 3?

The nodes on level 3 are 11, 29 and 62

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Trace the path that would be followed in searching for a node containing 28.

The root node is 56, therefore we should recursively search in the left subtree. 28 is less than 69, so we will search the left subtree. 28 is greater than 22, so we will search the right subtree. 28 is less than 29, so we will search the left subtree. 28 is greater than 23, so we will search in the right subtree. The subtree is NULL, so the node isn't found.

Ch 8 - What is a root?

The top node of a tree structure; a node with no parent

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 What is the maximum height of a binary search tree containing these nodes?

There are 13 nodes so the maximum height of the tree is 12

Ch 7 - 9. b - Given the following function: int Func(int num) { if (num == 0) return 0; else return num + Fun(num + 1); } Is Func(7) a good call? If so, what is returned from the function?

This is not a good call since it leads to infinite recursive calls to the function.

CH 9 - A priority queue of strings is implemented using a heap. The heap contains the following elements: Show how this priority queue is affected by adding the string "interviewing."

When adding the word "interviewing", it's added to the end of the tree but it does not fit well there. So we move the new node up to its proper position.

Ch 7 - 9. c - Given the following function: int Func(int num) { if (num == 0) return 0; else return num + Fun(num + 1); } Is Func(0) a good call? If so, what is returned from the function?

Yes it is a good call, the output is 0.

CH 12 - Implement the SelectionSort sorting algorithm

template<class ItemType> void SelectionSort(ItemType values[], int numValues) { int endIndex = numValues-1; for (int current = 0; current < endIndex; current++) Swap(values[current], values[MinIndex(values, current, endIndex)]); }

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Which levels have the maximum number of nodes that they could contain?

0 and 1 have the maximum number of nodes they can have

Ch 8 - Binary search tree

A binary tree in which the key value in any node is greater than the key value in its left child and any of its children(the node in the left subtree) and less than the key value in its right child and any of its children (the nodes in the right subtrees).

CH 12 - Cut the array in half Sort the left half Sort the right half Merge the two sorted halves into one sorted array

MergeSort

Ch7 - 1. c) Explain what is meant by the following: run-time stack

A run-time stack is a data structure that contains information about all of the functions that are currently active. It stores the stack frame of a function every time a call to them occurs.

Ch 8 - What is a binary tree?

A structure with a unique starting node (the root), in which each node is capable of having two child nodes, and in which a unique path exists from the root to every other node

CH 12 - Identify one or more correct answers: a)Reordering an array of pointers to list elements, rather than sorting the elements themselves, is a good idea when the number of elements is very large. b)the individual elements are large in size. c)the sort is recursive. d)there are multiple keys on which to sort the elements.

A, when the number of elements is very large, because if the data structures we are sorting are bigger and you know the pointer is in a different location, then its better to sort the list instead of the pointers.

CH 12 - Which of the following is true about QuickSort? a)A recursive version executes faster than a nonrecursive version. b)A recursive version has fewer lines of code than a nonrecursive version. c)A nonrecursive version takes more space on the run-time stack than a recursive version. d)It can be programmed only as a recursive function.

B, since shorter recursive code takes a longer time to execute.

CH 12 - Show how the values in the array in Exercise 1 would be arranged immediately before the execution of the function Merge in the original (nonrecursive) call to MergeSort.

Before the Merge function is called, the right and left sub-arrays have to be sorted, so it will look like this: [7, 10, 18, 23, 43, 4, 5, 14, 19, 66]

CH 12 - Set current to the index of first item in the array while more items in unsorted part of array "Bubble up" the smallest item in the unsorted part, causing intermediate swaps as needed Shrink the unsorted part of the array by incrementing current

BubbleSort

CH 12 - Given the array [26, 24, 3, 17, 25, 24, 13, 60, 47, 1] tell which sorting algorithm would produce the following results after four iterations: [1, 3, 13, 17, 26, 24, 24, 25, 47, 60]

BubbleSort

Ch 7 - 15. b - A sequential search member function of SortedType has the following prototype: void SortedType::Search(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation.

Class SortedTypeArray { Private: Int data[SIZE]; Int index, size; Public: SortedTypeArray() { Index = 0; Size = 0; } Void Search(int value, bool &found) { If (index == SIZE) { Found = false; Return; } else { If (data[index] == value) { Found = true; Return; } else { Index = index + 1; Search(value,found); } } } Void setIndex(int a) { Index = a; } Void add(int a) { If (size < SIZE) } Data[size] = a; Size = size + 1; } else { Cout << "The array is full." << endl; } } Void display() { For (int i = 0; i < size; i++) { Cout << data[i] << " " << endl; } }

CH 9 - A binary tree that is either full or full through the next-to-last level, with the leaves on the last level located as far to the left as possible

Complete binary tree

CH 12 - A merge sort is used to sort an array of 1,000 test scores in descending order. Which of the following statements is true? a) The sort is fastest if the original test scores are sorted from smallest to largest. b)The sort is fastest if the original test scores are in completely random order. c)The sort is fastest if the original test scores are sorted from largest to smallest. d)The sort is the same, no matter what the order of the original elements.

D, the sort will be the same, no matter the order of the elements.

Ch7 - When a function directly calls itself

Direct recursion

CH 9 - A priority queue is implemented as a linked list, sorted from largest to smallest element. d) Compare the Enqueue and Dequeue operations to those for the heap implementation, in terms of Big-O notation.

Enqueue: O(n * log(n)) Dequeue: O(n * lon(n))

CH 12 - What is the best, average and worst case Big O notation for BubbleSort?

O(N^2) for all

Ch7 - 2. d - True or false? If false, correct the statement. Recursive functions: should be used whenever execution speed is critical. are always shorter and clearer than the equivalent nonrecursive routines.

False, It is possible that recursion can be more expensive.

Ch7 - 2. e - True or false? If false, correct the statement. Recursive functions: are always shorter and clearer than the equivalent nonrecursive routines.

False, non-recursive functions are sometimes shorter and clearer.

Ch7 - 2. c - True or false? If false, correct the statement. Recursive functions: are possible only in languages with static storage allocation.

False, recursion cannot be implemented with static storage allocation.

CH 9 - A binary tree is stored in an array called treeNodes, which is indexed from 0 to 99, as described in the chapter. The tree contains 85 elements. Mark each of the following statements as True or False, and correct any false statements. b) treeNodes[41] has only one child.

False, since each node has either 2 or 0 children

Ch 7 - 2. g - True or false? If false, correct the statement. Recursive functions: are always less efficient, in terms of Big-O complexity.

False, sometimes they are more efficient in terms of big-O complexity.

CH 9 - A binary tree is stored in an array called treeNodes, which is indexed from 0 to 99, as described in the chapter. The tree contains 85 elements. Mark each of the following statements as True or False, and correct any false statements. d) The subtree rooted at treeNodes[7] is a full binary tree with four levels.

False, the level of a complete tree of index2.gif nodes is log(n+1) log(85+1) = 6.4 = 6

Ch 7 - 22. c - True or false? If false, correct the statement. A recursive solution should be used when: computing space is critical.

False, the recursive solution does not always take up the least amount of space.

CH 9 - A binary tree is stored in an array called treeNodes, which is indexed from 0 to 99, as described in the chapter. The tree contains 85 elements. Mark each of the following statements as True or False, and correct any false statements. c) The right child of treeNodes[12] is treeNodes[25].

False, the right child of 12 is 2 x 12 + 2 = 26

Ch7 - 2. b - True or false? If false, correct the statement. Recursive functions: generally use while or for statements as their main control structure.

False, they use if-else statements.

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Show the order in which the nodes in the tree are processed by a preorder traversal of the tree.

Get to the root, traverse the left subtree, traverse the right subtree.

CH 12 - Given the array [26, 24, 3, 17, 25, 24, 13, 60, 47, 1] tell which sorting algorithm would produce the following results after four iterations: [3, 17, 24, 26, 25, 24, 13, 60, 47, 1]

InsertionSort

CH 12 - Show the contents of the array after the fourth iteration of InsertionSort

InsertionSort: [-7, 10, 23, 18, 43, 4, 19, 5, 66, 14]

Ch 8 - What is the Big-O notation for the IsEmpty function in a binary search tree?

O(1)

Ch 8 - What is the Big-O notation for the IsFull function in a binary search tree?

O(1)

Ch 8 - What is the Big-O notation for the class constructor function in a binary search tree?

O(1)

CH 12 - What is the best, average and worst case Big O notation for QuickSort?

O(N log2 N) for all

CH 9 - What is the Big O notation for Enqueue for a linked list?

O(N)

CH 9 - What is the Big O notation for a skewed binary search tree?

O(N)

Ch 8 - What is the Big-O notation for the GetLength function in a binary search tree?

O(N)

Ch 8 - What is the Big-O notation for the MakeEmpty function in a binary search tree?

O(N)

Ch 8 - What is the Big-O notation for the GetItem function in a binary search tree?

O(log2N)

Ch 8 - What is the Big-O notation for the PutItem function in a binary search tree?

O(log2N)

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Trace the path that would be followed in searching for a node containing 61.

The root node is 56, therefore we should recursively search in the right subtree. 61 is less than 69, so we will search the left subtree. 61 is greater than 59, so we will search the right subtree. 61 is less than 62, so we will search the left subtree. 61 is equal to 61, therefore the node is found.

CH 9 - A priority queue is implemented as a linked list, sorted from largest to smallest element. b) Write the Enqueue operation using this implementation.

Public boolean enqeue(object info) { If (((end+1)%queue.length) == start) Return false; End = (end + 1)%queue.length; Queue[end] = info; If (start == -1) Start = 0; Return true;

CH 9 - A priority queue is implemented as a linked list, sorted from largest to smallest element. c) Write the Dequeue operation using this implementation.

Public object dequeue() { If (start == -1) { Return null; } Object temp = queue[start]; If (start == end) { Start = end -=1; } else { Start = (start + 1)%queue.length; Return temp;

Ch7 - Print out the second through last elements om the list in reverse order, print the first element in the list

RevPrint

Ch7 - Does each recursive call to the function involve a smaller case of the original problem, leading inescapably to the base case?

The smaller-caller question

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 What is the minimum height of a binary search tree containing these nodes?

There are 13 nodes so the minimum height of the tree is 3

Ch 7 - Use the following function when answering Exercises 7 and 8: int Puzzle(int base, int limit) { if (base > limit) return -1; else if (base == limit) return 1; else return base*Puzzle(base+1, limit); } 8. a -Show what would be written by the following calls to the recursive function Puzzle: cout << Puzzle(14, 10);

Since 14 is greater than 10, it returns - 1.

Ch 7 - Use the following function when answering Exercises 7 and 8: int Puzzle(int base, int limit) { if (base > limit) return -1; else if (base == limit) return 1; else return base*Puzzle(base+1, limit); } 8. c - Show what would be written by the following calls to the recursive function Puzzle: cout << Puzzle(0, 0);

Since the base is equal to limit(0 == 0), it returns 1.

Ch 7 - Use the following function when answering Exercises 7 and 8: int Puzzle(int base, int limit) { if (base > limit) return -1; else if (base == limit) return 1; else return base*Puzzle(base+1, limit); } 8. b - Show what would be written by the following calls to the recursive function Puzzle: cout << Puzzle(4, 7);

Since the function recursively calls 3 times, it returns 120.

CH 12 - A sorting algorithm that preserves the order of duplicates

Stable sort

CH 9 - A binary tree is stored in an array called treeNodes, which is indexed from 0 to 99, as described in the chapter. The tree contains 85 elements. Mark each of the following statements as True or False, and correct any false statements. a) treeNodes[42] is a leaf node.

True

Ch 7 - 15. a - A sequential search member function of SortedType has the following prototype: void SortedType::Search(int value, bool& found); Write the function definition as a recursive search, assuming a linked list implementation.

Struct node { Int data; Node *next }; Class SortedTypeList { Private: Node *start; Node *current_ptr; Public: SortedTypeList() { Start = NULL; Current_ptr = NULL; } If (current_ptr == NULL) { Found = false; Return; } else { If (current_ptr-> data = value) { Found = true; Return; } else { Current_ptr = currentPtr->next; Search(value,found); } } } Void add(int a) { Node *temp = new Node(); temp->data = a; temp->next = NULL; Node *ptr; Ptr = start; If (start == null) { Start = temp; Current_ptr = start; } else { While (ptr->next != NULL) { Ptr = ptr->next; ptr->next = temp; } } Void setPtr() { Current_ptr = start; } Void show() { Current_ptr = start; While (current_ptr != NULL) { Cout << current_ptr->data << " "; Current_ptr = current_ptr->next; } Cout << endl; } };

Ch7 - 1. e) Explain what is meant by the following: tail recursion

Tail recursion is when a recursive call to a function is the last line in the function, the last thing that the function does is to call itself.

Ch 7 - 3. Use the Three-Question Method to verify the ValueInList function described in this chapter.

The base case question: There is a non-recursive way out of the function, when list.info[startIndex] == value. The smaller-caller question:The recursive call to the function does involve a smaller case of the original problem, since startIndex increments each time, which shrinks the size of the list. The general case question: The entire function works correctly because eventually startIndex will reach the end of the list and it will either find the value and terminate, or terminate because it reached the end.

Ch 7 - Use the following function when answering Exercises 7 and 8: int Puzzle(int base, int limit) { if (base > limit) return -1; else if (base == limit) return 1; else return base*Puzzle(base+1, limit); } 7. a) Identify the following: the base case or cases of the function

The base case(s) of the function puzzle are: If (base > limit) { return - 1; } and else if (base == limit) { return 1; }

Ch7 - Is there a non-recursive way out of the function, and does the routine work correctly for this base case?

The base-case question

CH 9 - A priority queue of strings is implemented using a heap. The heap contains the following elements: What feature of these strings is used to determine their priority in the priority queue?

The length of the string.

Ch 7 - 12. d - You must assign the grades for a programming class. The class is studying recursion, and students have been given this simple assignment: Write a recursive function SumSquares that takes a pointer to a linked list of integer elements and returns the sum of the squares of the elements. Example: SumSquares(listPtr) yields (5 * 5) + (2 * 2) + (3 * 3) + (1 * 1) = 39 Assume that the list is not empty. You have received quite a variety of solutions. Grade the functions that follow, marking errors where you see them. int SumSquares(NodeType* list) { if (list->next == NULL) return list->info*list->info; else return list->info*list->info + SumSquares(list->next); }

This function returns the correct answer, but there can be a problem is the initial list is empty.

Ch 7 - 12. c - You must assign the grades for a programming class. The class is studying recursion, and students have been given this simple assignment: Write a recursive function SumSquares that takes a pointer to a linked list of integer elements and returns the sum of the squares of the elements. Example: SumSquares(listPtr) yields (5 * 5) + (2 * 2) + (3 * 3) + (1 * 1) = 39 Assume that the list is not empty. You have received quite a variety of solutions. Grade the functions that follow, marking errors where you see them. int SumSquares(NodeType* list) { if (list == NULL) return 0; else return list->info*list->info + SumSquares(list->next); }

This function returns the correct answer, there are no errors.

Ch 7 - 12. a - You must assign the grades for a programming class. The class is studying recursion, and students have been given this simple assignment: Write a recursive function SumSquares that takes a pointer to a linked list of integer elements and returns the sum of the squares of the elements. Example: SumSquares(listPtr) yields (5 * 5) + (2 * 2) + (3 * 3) + (1 * 1) = 39 Assume that the list is not empty. You have received quite a variety of solutions. Grade the functions that follow, marking errors where you see them. int SumSquares(NodeType* list) { return 0; if (list != NULL) return (list->info*list->info) + SumSquares(list->next)); }

This function will always return 0 since it has a bad base case. It will never go to the recursive call no matter what the input is.

Ch 7 - 12. b - You must assign the grades for a programming class. The class is studying recursion, and students have been given this simple assignment: Write a recursive function SumSquares that takes a pointer to a linked list of integer elements and returns the sum of the squares of the elements. Example: SumSquares(listPtr) yields (5 * 5) + (2 * 2) + (3 * 3) + (1 * 1) = 39 Assume that the list is not empty. You have received quite a variety of solutions. Grade the functions that follow, marking errors where you see them. int SumSquares(NodeType* list) { int sum = 0; while (list != NULL) { sum = list->info + sum; list = list->next; } return sum; }

This returns the sum of the scores, but it doesn't return the sum of the squares of the scores. It is an iterative solution, not a recursive solution.

Ch 7 - 12. e - You must assign the grades for a programming class. The class is studying recursion, and students have been given this simple assignment: Write a recursive function SumSquares that takes a pointer to a linked list of integer elements and returns the sum of the squares of the elements. Example: SumSquares(listPtr) yields (5 * 5) + (2 * 2) + (3 * 3) + (1 * 1) = 39 Assume that the list is not empty. You have received quite a variety of solutions. Grade the functions that follow, marking errors where you see them. int SumSquares(NodeType* list) { if (list == NULL) return 0; else return (SumSquares(list->next) * SumSquares(list->next)); }

This returns zero no matter what the input is, but the base case is good even though the recursion calls are wrong.

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Show the order in which the nodes in the tree are processed by an inorder traversal of the tree.

Traverse the left subtree, get to the root, traverse the right subtree.

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Show the order in which the nodes in the tree are processed by a postorder traversal of the tree.

Traverse the left subtree, traverse the right subtree, get to the root.

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Show how the tree would look after the deletion of 29, 59, and 47.

Tree -> 56 49 69 22 61 11 30 62 23 64

CH 9 - The elements in a binary tree are to be stored in an array, as described in the chapter. Each element is a nonnegative int value. What value can you use as the dummy value, if the binary tree is not complete?

Tree-> 26 14 38 1 null 33 50 Null 7 null null null 35 44 60

Ch 8 - 56 47 69 22 49 59 11 29 62 23 30 61 64 Show how the (original) tree would look after the insertion of nodes containing 63, 77, 76, 48, 9, and 10 (in that order).

Tree-> 56 47 69 22 49 59 77 11 29 48 62 76 9 23 30 61 64 10 63

Ch 7 - 22. b - True or false? If false, correct the statement. A recursive solution should be used when: the nonrecursive solution would be longer and more difficult to write.

True, it can save time if you write a recursive solution in this case.

Ch 7 - 22. d - True or false? If false, correct the statement. A recursive solution should be used when: your instructor says to use recursion.

True, you should use a recursive solution if your instructor says to.

CH 9 - Implement the PQType class constructor as a priority queue

class FullPQ() { }; class EmptyPQ() { }; template<class ItemType> class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType newItem); void Dequeue(ItemType& item); private: int length; HeapType<ItemType> items; int maxItems; };

Ch 7 - 9. d - Given the following function: int Func(int num) { if (num == 0) return 0; else return num + Fun(num + 1); } Is Func(-5) a good call? If so, what is returned from the function?

Yes, it is a good call, the output is -15.

CH 9 - The elements in a binary tree are to be stored in an array, as described in the chapter. Each element is a nonnegative int value. Show the contents of the array, given the following tree. 26 14 38 1 33 50 7 35 44 60

[26, 14, 38, 1, null, 33, 50, null, 7, null, null, null, 35, 44, 60]

Ch 8 - Implement the IsEmpty function as a binary search tree

bool TreeType::IsEmpty() const { return root == NULL; }

Ch 8 - Implement the IsFull function as a binary search tree

bool TreeType::IsFull() const { TreeNode* location; try { location = new TreeNode; delete location; return false; } catch(std::bad_alloc exception) { return true; } }

Ch7 - Implement the ValueInList function as a recursive function

bool ValueInList(ListType list, int value, int startIndex) { if (list.info[startIndex] == value) return true; //Base case 1 else if (startIndex == list.length - 1) return false; //Base case 2 else return ValueInList(list, value, startIndex + 1); }

Ch7 - Implement the combinations function as a recursive function

int Combinations(int group, int members) // Pre: group and members are positive. // Post: Function value = number of combinations of members size. //That can be constructed from the total group size. { if (members == 1) return group; //Base case 1 else if (members == group) return 1; //Base case 2 else return (Combinations(group-1, members-1) + Combinations(group-1, members)); }

Ch 8 - Implement the CountNodes function as a binary search tree

int CountNodes(TreeNode* tree); int TreeType::GetLength() const { return CountNodes(root); } int CountNodes(TreeNode* tree) { if (tree == NULL) return 0; else return CountNodes(tree->left) + CountNodes(tree->right) + 1; }

Ch7 - Implement the factorial function as a recursive function

int Factorial(int number) // Pre: number is nonnegative. // Post: Function value = factorial of number. { if (number == 0) // Line 1 return 1; //Line 2 else return number * Factorial(number - 1); //Line 3 }

CH 9 - Implement the reheapDown function

template<class ItemType> void HeapType<ItemType>::ReheapDown(int root, int bottom) { int maxChild; int rightChild; int leftChild; leftChild = root*2+1; rightChild = root*2+2; if (leftChild <= bottom) { if (leftChild == bottom) maxChild = leftChild; else { if (elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if (elements[root] < elements[maxChild]) { Swap(elements[root], elements[maxChild]); ReheapDown(maxChild, bottom); } } }

Ch7 - Implement the insert function as a recursive linked list function

template<class ItemType> void Insert(NodeType<ItemType>*& listPtr, ItemType item) { if (listPtr == NULL || item < listPtr->info) { //Save current pointer NodeType<ItemType>* tempPtr = listPtr; //Get a new node listPtr = new NodeType<ItemType>; listPtr->info = item; listPtr->next = tempPtr; } else Insert(listPtr->next, item); }

Ch7 - Implement the BinarySearch function as a recursive function

template<class ItemType> bool BinarySearch(ItemType info[], ItemType item, int fromLocation, int toLocation) { if (fromLocation > toLocation) // Base case 1 return false; else { int midPoint; midPoint = (fromLocation + toLocation) / 2; if (item < info[midPoint]) return BinarySearch(info, item, fromLocation, midPoint - 1); else if (item == info[midPoint]) return true; else //Base case 2 return Binary Search(info, item, midPoint + 1, toLocation); } }

CH 9 - Implement the HeapType struct

template<class ItemType> struct HeapType { void ReheapDown(int root, int bottom); void ReheapUp(int root, int bottom); ItemType* elements; int numElements; };

Ch7 - Implement the delete function as a recursive linked list function

template<class ItemType> void Delete(NodeType<ItemType>*& listPtr, ItemType item) { if (item == listPtr->info) { NodeType<ItemType>* tempPtr = listPtr; listPtr = listPtr->next; delete tempPtr; } else Delete(listPtr->next, item) }

CH 9 - Implement the HeapSort function

template<class ItemType> void HeapSort(ItemType values[], int numValues) { int index; for (index = numValues/2 - 1; index >= 0; index--) ReheapDown(values, index, numValues-1); for (index = numValues-1; index >=1; index--) { Swap(values[0], values[index]); ReheapDown(values, 0, index-1); } }

CH 9 - Implement the reheapUp function

template<class ItemType> void HeapType<ItemType>::ReheapUp(int root, int bottom) { int parent; if (bottom > root) { parent = (bottom-1) / 2; if (elements[parent] < elements[bottom]) { Swap(elements[parent], elements[bottom]); ReheapUp(root, parent); } } }

CH 9 - Implement the Dequeue function as a priority queue

template<class ItemType> void PQType<ItemType>::Dequeue(ItemType& item) { if (length == 0) throw EmptyPQ(); else { item = items.elements[0]; items.elements[0] = items.elements[length-1]; length--; items.ReheapDown(0, length-1); } }

CH 9 - Implement the Enqueue function as a priority queue

template<class ItemType> void PQType<ItemType>::Enqueue(ItemType newItem) { if (length == maxItems) throw FullPQ(); else { length++; items.elements[length-1] = newItem; items.ReheapUp(0, length-1); } }

CH 9 - Implement the PQType class function as a priority queue

template<class ItemType> PQType<ItemType>::PQType(int max) { maxItems = max; items.elements = new ItemType[max]; length = 0; } template<class ItemType> void PQType<ItemType>::MakeEmpty() { length = 0; } template<class ItemType> PQType<ItemType>::~PQType() { delete [] items.elements; }

CH 9 - Implement the IsEmpty function as a priority queue

template<class ItemType> bool PQType<ItemType>::IsEmpty() const { return length == 0; }

CH 9 - Implement the IsFull function as a priority Queue

template<class ItemType> bool PQType<ItemType>::IsFull() { return length == maxItems; }

CH 12 - Implement the Swap sorting algorithm

template<class ItemType> inline void Swap(ItemType& item1, ItemType& item2) { ItemType tempItem; tempItem = item1; item1 = item2; item2 = tempItem; }

CH 12 - Implement the MinIndex sorting algorithm

template<class ItemType> int MinIndex(ItemType values[], int startIndex, int endIndex) { int indexOfMin = startIndex; for (int index = startIndex + 1; index <= endIndex; index++) if (values[index] < values[indexOfMin]) indexOfMin = index; return indexOfMin; }

CH 12 - Implement the BubbleSort sorting algorithm

template<class ItemType> void BubbleSort(ItemType values[], int numValues) { int current = 0; while (current < numValues - 1) { BubbleUp(values, current, numValues-1); current++; } }

CH 12 - Implement the BubbleUp sorting algorithm

template<class ItemType> void BubbleUp(ItemType values[], int startIndex, int endIndex) { for (int index = endIndex; index > startIndex; index--) if (values[index] < values[index-1]) Swap(values[index], values[index-1]); }

CH 12 - Implement the BubbleUp2 sorting algorithm

template<class ItemType> void BubbleUp2(ItemType values[], int startIndex, int endIndex, bool& sorted) { sorted = true; for (int index = endIndex; index > startIndex; index--) if (values[index] < values[index-1]) { Swap(values[index], values[index-1]); sorted = false; } }

CH 12 - Implement the InsertItem function as a sorting algorithm

template<class ItemType> void InsertItem(ItemType values[], int startIndex, int endIndex) { bool finished = false; int current = endIndex; bool moreToSearch = (current != startIndex); while (moreToSearch && !finished) { if (values[current] < values[current-1]) { Swap(values[current], values[current-1]); current--; moreToSearch = (current != startIndex); } else finished = true; } }

CH 12 - Implement the InsertionSort function as a sorting algorithm

template<class ItemType> void InsertionSort(ItemType values[], int numValues) { for (int count = 0; count < numValues; count++) InsertItem(values, 0, count); }

CH 12 - Implement the Merge function as a sorting algorithm

template<class ItemType> void Merge(ItemType values[], int leftFirst, int leftLast, int rightFirst, int rightLast, ItemType tempArray[]) { int index = leftFirst; int saveFirst = leftFirst; while ((leftFirst <= leftLast) && (rightFirst <= rightLast)) { if (values[leftFirst] < values[rightFirst]) { tempArray[index] = values[leftFirst]; leftFirst++; } else { tempArray[index] = values[rightFirst]; rightFirst++; } index++; } while (leftFirst <= leftLast) { tempArray[index] = values[leftFirst]; leftFirst++; index++; } while (rightFirst <= rightLast) { tempArray[index] = values[rightFirst]; rightFirst++; index++; } for (index = saveFirst; index <= rightLast; index++) values[index] = tempArray[index]; }

CH 12 - Implement the MergeSort function as a sorting algorithm

template<class ItemType> void MergeSort(ItemType values[], int first, int last, ItemType tempArray[]) { if (first < last) { int middle = (first + last) / 2; thread left (MergeSort<ItemType>, values, first, middle, tempArray); thread right (MergeSort<ItemType>, values, middle + 1, last, tempArray); left.join(); right.join(); Merge<ItemType>(values, first, middle, middle + 1, last, tempArray); } }

CH 12 - Implement the MergeSort function as a sorting algorithm

template<class ItemType> void MergeSort(ItemType values[], int first, int last. ItemType tempArray) { if (first < last) { int middle = (first + last) / 2; MergeSort<ItemType>(values, first, middle, tempArray); MergeSort<ItemType>(values, middle + 1, last, tempArray); Merge<ItemType>(values, first, middle, middle + 1, last, tempArray); } }

CH 12 - Implement the RadixSort function as a sorting algorithm

template<class ItemType> void RadixSort(ItemType values[], int numValues, int numPositions, int radix) { QueType<ItemType> queues[radix]; int whichQueue; for (int position = 1; position <= numPositions; position++) { for (int counter = 0; counter < length; counter++) { whichQueue = values[counter].SubKey(position); queues[whichQueue].Enqueue(values[counter]); } CollectQueues(values, queues, radix); } }

CH 12 - Implement the ShortBubble function as a sorting algorithm

template<class ItemType> void ShortBubble(ItemType values[], int numValues) { int current = 0; bool sorted = false; while (current < numValues - 1 && !sorted) { BubbleUp2(values, current, numValues-1, sorted); current++; } }

Ch 8 - Implement the delete function as a binary search tree

void DeleteNode(TreeNode*& tree); void Delete(TreeNode*& tree, ItemType item); void TreeType::DeleteItem(ItemType item) { Delete(root, item); } void Delete(TreeNode*& tree, ItemType item) { if (item < tree->info) Delete(tree->left, item); else if (item > tree->info) Delete (tree->right, item); else DeleteNode(tree); }

Ch 8 - Implement the Insert function as a binary search tree

void Insert(TreeNode*& tree, ItemType item); void TreeType::PutItem(ItemType item) { Insert(root, item); } void Insert(TreeNode*& tree, ItemType item) { if (tree == NULL) { tree = new TreeNode; tree->right = NULL; tree->left = NULL; tree->info = item; } else if (item < tree->info) Insert(tree->left, item); else Insert(tree->right, item); }

Ch 8 - Implement the retrieve function as a binary search tree

void Retrieve(TreeNode* tree, ItemType& item, bool& found); ItemType TreeType::GetItem(ItemType item, bool& found) const { Retrieve(root, item, found); return item; } void Retrieve(TreeNode* tree, ItemType& item, bool& found) { if (tree == NULL) found = false; else if (item < tree->info) Retrieve(tree->left, item, found); else if (item > tree->info) Retrieve(tree->right, item, found); else { item = tree->info; found = true; } }

Ch7 - Implement the RevPrint function as a recursive linked list function

void RevPrint(NodeType* listPtr) { if (listPtr != NULL) { RevPrint(listPtr->next); std::cout << listPtr->info << std::endl; } }

Ch 8 - Implement the PutItem function as a binary search tree

void TreeType::PutItem(ItemType item) { TreeNode* newNode; TreeNode* nodePtr; TreeNode* parentPtr; newNode = new TreeNode; newNode->info = item; newNode->left = NULL; newNode->right = NULL; FindNode(root, item, nodePtr, parentPtr); if (parentPtr == NULL) root = newNode; else if (item < parentPtr->info) parentPtr->left = newNode; else parentPtr->right = newNode; }


Conjuntos de estudio relacionados

CH 6: Inventory and Cost of Goods Sold

View Set

HTML and CSS Final Study Guide version 3

View Set

Communicating at Work Ch 1 LS/SB

View Set

Chapter 4: Equilibrium: Where Supply Meets Demand

View Set

Ectoderm I: Neural Tube Formation

View Set

Electrical Machines Midterm-Theory

View Set