SlideShare uma empresa Scribd logo
1 de 8
1. Here is an array with exactly 15 elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Suppose that we are doing a serial search for an element. Circle any elements that will be found by
examining two or fewer numbers from the array.
2. Here is an array with exactly 15 elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Suppose that we are doing a binary search for an element. Circle any elements that will be found by
examining two or fewer numbers from the array.
3. Implement the body of the following function using a binary search of the array. You do not need
to check the precondition.
public static boolean has42(int[ ] data, int start, int end)
// Precondition: The elements data[start]...data[end] are sorted from smallest
// to largest. This array segment might be empty (indicated by end being less
// than start).
// Postcondition: A true return value indicates that the number 42 appears in
// data[start]...data[end]. A false return value indicates that 42 doesn’t
// appear.
4. Draw a hash table with open addressing and a size of 9. Use the hash function "k%9". Insert the
keys: 5, 29, 20, 0, 27 and 18 into your table (in that order).
5. Suppose you are building an open address hash table with double hashing. The hash table
capacity is n, so that the valid hash table indexes range from 0 to n. Fill in the blanks:
In order to ensure that every array position is examined, the value returned by the second
hash function must be ________________________ with respect to n.
One way to ensure this good behavior is to make n be _______________, and have the
return value of the second hash function range from _________ to _________ (including the
end points).
6. You are writing code for the remove method of a chained hash table. Fill in the blanks in this
pseudocode with the two missing statements. You may use pseudocode yourself, or write actual
Java code:
public void Table remove(Object key)
{
ChainedHashNode cursor;
int i;
1. i = key.hashCode( );
2. Make cursor refer to the node that contains an item with the given key
(or set it to null if there is no such node).
3. if (cursor != null)
{
3a. _____________________________________________________________
3b. _____________________________________________________________
}
7. Draw a hash table with chaining and a size of 9. Use the hash function "k%9" to insert the keys 5,
29, 20, 0, and 18 into your table.
8. Suppose that I have the following private instance variables for an open-address hash table:
public class Table
{
privateintmanyItems;
9.private Object[ ] keys;
private Object[ ] data;
privateboolean[ ] hasBeenUsed;
...
The hash table uses open addressing with linear probing. A location [i] of the table that has NEVER
been used will have hasBeenUsed[i] set to false. Other locations have hasBeenUsed[i] set to true.
Complete the implementation of the following method of the Table class. There is no need to check
the precondition, but your code must be as efficient as possible.
publicbooleancontainsKey(Object key)
// Postcondition: If key occurs as a key of a record in the table, then
// the method returns true; otherwise the method returns false.
10. Suppose that an open-address hash table has a capacity of 811 and it contains 81 elements.
What is the table's load factor? (An appoximation is fine.)
11. I plan to put 1000 items in a hash table, and I want the average number of accesses in a
successful search to be about 2.0.
A. About how big should the array be if I use open addressing with linear probing? NOTE: For a load
factor of A, the average number of accesses is generally ½(1+1/(1-A)).
12. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from smallest to
largest).
13. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the FIRST iteration of the large loop in an insertion sort (sorting from smallest
to largest). This iteration has shifted at least one item in the array!
14. Suppose that you are writing a program that has this selectionsort static method available:
voidselectionsort(int[ ] data, int first, int n);
Your program also has an integer array called x, with 10 elements. Write two method activations:
The first activation uses selectionsort to sort all of x; the second call uses selectionsort to sort
x[3]..x[9].
15. Describe a case where quicksort will result in quadratic behavior.
16. Here is an array which has just been partitioned by the first step of quicksort:
3, 0, 2, 4, 5, 8, 7, 6, 9
Which of these elements could be the pivot? (There may be more than one possibility!)
17. Give a concise accurate description of a good way for quicksort to choose a pivot element. Your
approach should be better than "use the entry at location [0]".
18. Give a concise accurate description of a good way for quicksort to improve its performance by
using insertionsort.
19. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Suppose we partition this array using quicksort's partition function and using 5 for the pivot. Draw
the resulting array after the partition finishes.
20. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the TWO recursive calls of merge sort are completed, and before the final
merge step has occured.
21. Implement the following static method:
private static void merge(int[ ] data, int first, int n1, int n2);
// Precondition: data has at least n1+n2 components starting at
// data[first]. The first n1 elements (from data[first] to
// data[first+n1-1]) are sorted from smallest to largest, and the last
// n2 elements (from data[first+n1] to data[first+n1+n2-1]) are also
// sorted from smallest to largest.
// Postcondition: Starting at data[first, n1+n2 elements of data have been
// rearranged to be sorted from smallest to largest.
22. Write two or three clear sentences to describe how a heapsort works.
23. Fill in the following table for the times to sort an array of n items. Use only big-O notation, and
do not have any extraneous constants in your expressions.
Worst Case Average Case
Binary search of a sorted array
Insertion sort
Merge sort
Quick sort without "median of three" pivot
selection
Quick sort with "median of three" pivot
selection
Selection sort
Heap sort
24. Here is a small binary tree:
14
/ 
2 11
/  / 
1 3 10 30
/ /
7 40
Circle all the leaves. Put a square box around the root. Draw a star around each ancestor of
the node that contains 10. Put a big X through every descendant of the node the contains 10.
25. Draw a full binary tree with at least 6 nodes.
26. Draw a complete binary tree with exactly six nodes. Put a different value in each node.
Then draw an array with six components and show where each of the six node values would
be placed in the array (using the usual array representation of a complete binary tree).
27. Write the instance variables for a new class that could be used for a node in a tree where:
(1) Each node contains int data, (2) Each node has up to four children, and (3) Each node also
has a reference to its parent. Store the references to the children in an array of four
components.
28. Draw a binary taxonomy tree that can be used for these four animals: Rabbit, Horse,
Whale, Snake.
29. Using the BTNode class from, write a new static method of the BTNode class to meet the
following specification. No recursion is needed.
public static void subswap(BTNode root)
// Precondition: Root is the root reference of a binary tree.
// Postcondition: If root is non-null, then the original left subtree below
// this root has been moved and is now the right subtree, and the original
// right subtree is now the left subtree.
// Example original tree: Example new tree:
// 1 1
// /  / 
// 2 3 3 2
// /  / 
// 4 5 4 5
30. Redo the previous problem as a new non-static BTNode method.
31. Using the BTNode class from, write a new static method of the BTNode class to meet the
following specification.
public static intmanyNodes(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the number of nodes in the tree.
// NOTES: The empty tree has 0 nodes, and a tree with just a root has
// 1 node.
32. Using the BTNode class, write a new static method of the BTNode class to meet the
following specification.
public static inttreeDepth(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the depth of the binary tree.
// NOTES: The empty tree has a depth of -1 and a tree with just a root
// has a depth of 0.
33. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
34. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static int count42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value indicates how many times 42 appears
// in the tree. NOTE: If the tree is empty, the method returns zero.
35. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static boolean has42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value indicates whether 42 appears somewhere
// in the tree. NOTE: If the tree is empty, the method returns false.
36. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static boolean all42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value is true if every node in the tree
// contains 42. NOTE: If the tree is empty, the method returns true.
37. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static int sum(IntBTNode root)
// Precondition: root is the root reference of a binary tree.
// Postcondition: The return value is the sum of all the data in all the
nodes.
// NOTES: The return value for the empty tree is zero.
38. Suppose that we want to create a binary search tree where each node contains information
of some data type. What additional factor is required for the data type?
39. Suppose that a binary search tree contains the number 42 at a node with two children.
Write two or three clear sentences to describe the process required to delete the 42 from the
tree.
40. Suppose IntBTNode is a BTNodewith integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int count42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: The return value indicates how many times 42 appears
// in the tree.
41. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int max(BTNode root)
// Precondition: root is the root reference of a nonempty binary SEARCH
// tree.
// Postcondition: The return value is the largest value in the tree.
42. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
void insert42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: One copy of the number 42 has been added to the binary
// search tree.
43. What is the importance of the stopping case in recursive methods?
44. Write a recursive method that has one parameter which is an int value called x. The method
prints x asterisks, followed by x exclamation points. Do NOT use any loops. Do NOT use any variables
other than x.
45. For the tree shown below, determine
a. Which node is the root
b. Which nodes are leaves
c. The tree’s depth
d. The result of preorder, postorder, inorder, and level-order traversals
46.For each node shown on the tree below:
a. Name the parent node
b. List the children
c. List the siblings
d. Compute the height
e. Compute the depth
f. Compute the size
47.What is the output of the following method for the
tree shown below?
1 public static <AnyType> void mysteryPrint(BinaryNode<AnyType> t )
2 {
3 if( t != null )
4 {
5 System.out.println(t.getElement( ) );
6 mysteryPrint(t.getLeft( ) );
7 System.out.println(t.getElement( ) );
8 mysteryPrint(t.getRight( ) );
9 System.out.println(t.getElement( ) );
10 }
11 }
48.Show the stack operations when an inorder and preorder traversal is
applied to the tree.
611+tutorial

Mais conteúdo relacionado

Mais procurados

Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structuresNiraj Agarwal
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Balwant Gorad
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked listSumathi Kv
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables Nifras Ismail
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms ArraysManishPrajapati78
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queuestech4us
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)EngineerBabu
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2Rajendran
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 

Mais procurados (20)

Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
List
ListList
List
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
 
linked list
linked listlinked list
linked list
 
Pf presntation
Pf presntationPf presntation
Pf presntation
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Open Addressing on Hash Tables
Open Addressing on Hash Tables Open Addressing on Hash Tables
Open Addressing on Hash Tables
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
2D Array
2D Array 2D Array
2D Array
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Recursion Lecture in Java
Recursion Lecture in JavaRecursion Lecture in Java
Recursion Lecture in Java
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Array data structure
Array data structureArray data structure
Array data structure
 

Semelhante a 611+tutorial

Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxmaxinesmith73660
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfinfo309708
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docxajoy21
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework HelpC++ Homework Help
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questionsESWARANM92
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questionsESWARANM92
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdfadinathassociates
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdfalmaniaeyewear
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked listecomputernotes
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsPrabu U
 
Algorithms devised for a google interview
Algorithms devised for a google interviewAlgorithms devised for a google interview
Algorithms devised for a google interviewRussell Childs
 

Semelhante a 611+tutorial (20)

Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx(a) There are three ways to traverse a binary tree pre-order, in-or.docx
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questions
 
Amcat automata questions
Amcat   automata questionsAmcat   automata questions
Amcat automata questions
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
Algorithms devised for a google interview
Algorithms devised for a google interviewAlgorithms devised for a google interview
Algorithms devised for a google interview
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
‏‏Lecture 2.pdf
‏‏Lecture 2.pdf‏‏Lecture 2.pdf
‏‏Lecture 2.pdf
 

611+tutorial

  • 1. 1. Here is an array with exactly 15 elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Suppose that we are doing a serial search for an element. Circle any elements that will be found by examining two or fewer numbers from the array. 2. Here is an array with exactly 15 elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Suppose that we are doing a binary search for an element. Circle any elements that will be found by examining two or fewer numbers from the array. 3. Implement the body of the following function using a binary search of the array. You do not need to check the precondition. public static boolean has42(int[ ] data, int start, int end) // Precondition: The elements data[start]...data[end] are sorted from smallest // to largest. This array segment might be empty (indicated by end being less // than start). // Postcondition: A true return value indicates that the number 42 appears in // data[start]...data[end]. A false return value indicates that 42 doesn’t // appear. 4. Draw a hash table with open addressing and a size of 9. Use the hash function "k%9". Insert the keys: 5, 29, 20, 0, 27 and 18 into your table (in that order). 5. Suppose you are building an open address hash table with double hashing. The hash table capacity is n, so that the valid hash table indexes range from 0 to n. Fill in the blanks: In order to ensure that every array position is examined, the value returned by the second hash function must be ________________________ with respect to n. One way to ensure this good behavior is to make n be _______________, and have the return value of the second hash function range from _________ to _________ (including the end points). 6. You are writing code for the remove method of a chained hash table. Fill in the blanks in this pseudocode with the two missing statements. You may use pseudocode yourself, or write actual Java code: public void Table remove(Object key) {
  • 2. ChainedHashNode cursor; int i; 1. i = key.hashCode( ); 2. Make cursor refer to the node that contains an item with the given key (or set it to null if there is no such node). 3. if (cursor != null) { 3a. _____________________________________________________________ 3b. _____________________________________________________________ } 7. Draw a hash table with chaining and a size of 9. Use the hash function "k%9" to insert the keys 5, 29, 20, 0, and 18 into your table. 8. Suppose that I have the following private instance variables for an open-address hash table: public class Table { privateintmanyItems; 9.private Object[ ] keys; private Object[ ] data; privateboolean[ ] hasBeenUsed; ... The hash table uses open addressing with linear probing. A location [i] of the table that has NEVER been used will have hasBeenUsed[i] set to false. Other locations have hasBeenUsed[i] set to true. Complete the implementation of the following method of the Table class. There is no need to check the precondition, but your code must be as efficient as possible. publicbooleancontainsKey(Object key)
  • 3. // Postcondition: If key occurs as a key of a record in the table, then // the method returns true; otherwise the method returns false. 10. Suppose that an open-address hash table has a capacity of 811 and it contains 81 elements. What is the table's load factor? (An appoximation is fine.) 11. I plan to put 1000 items in a hash table, and I want the average number of accesses in a successful search to be about 2.0. A. About how big should the array be if I use open addressing with linear probing? NOTE: For a load factor of A, the average number of accesses is generally ½(1+1/(1-A)). 12. Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4 Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from smallest to largest). 13. Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4 Draw this array after the FIRST iteration of the large loop in an insertion sort (sorting from smallest to largest). This iteration has shifted at least one item in the array! 14. Suppose that you are writing a program that has this selectionsort static method available: voidselectionsort(int[ ] data, int first, int n); Your program also has an integer array called x, with 10 elements. Write two method activations: The first activation uses selectionsort to sort all of x; the second call uses selectionsort to sort x[3]..x[9]. 15. Describe a case where quicksort will result in quadratic behavior. 16. Here is an array which has just been partitioned by the first step of quicksort: 3, 0, 2, 4, 5, 8, 7, 6, 9 Which of these elements could be the pivot? (There may be more than one possibility!) 17. Give a concise accurate description of a good way for quicksort to choose a pivot element. Your approach should be better than "use the entry at location [0]". 18. Give a concise accurate description of a good way for quicksort to improve its performance by using insertionsort. 19. Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4
  • 4. Suppose we partition this array using quicksort's partition function and using 5 for the pivot. Draw the resulting array after the partition finishes. 20. Here is an array of ten integers: 5 3 8 9 1 7 0 2 6 4 Draw this array after the TWO recursive calls of merge sort are completed, and before the final merge step has occured. 21. Implement the following static method: private static void merge(int[ ] data, int first, int n1, int n2); // Precondition: data has at least n1+n2 components starting at // data[first]. The first n1 elements (from data[first] to // data[first+n1-1]) are sorted from smallest to largest, and the last // n2 elements (from data[first+n1] to data[first+n1+n2-1]) are also // sorted from smallest to largest. // Postcondition: Starting at data[first, n1+n2 elements of data have been // rearranged to be sorted from smallest to largest. 22. Write two or three clear sentences to describe how a heapsort works. 23. Fill in the following table for the times to sort an array of n items. Use only big-O notation, and do not have any extraneous constants in your expressions. Worst Case Average Case Binary search of a sorted array Insertion sort Merge sort Quick sort without "median of three" pivot selection Quick sort with "median of three" pivot selection Selection sort Heap sort
  • 5. 24. Here is a small binary tree: 14 / 2 11 / / 1 3 10 30 / / 7 40 Circle all the leaves. Put a square box around the root. Draw a star around each ancestor of the node that contains 10. Put a big X through every descendant of the node the contains 10. 25. Draw a full binary tree with at least 6 nodes. 26. Draw a complete binary tree with exactly six nodes. Put a different value in each node. Then draw an array with six components and show where each of the six node values would be placed in the array (using the usual array representation of a complete binary tree). 27. Write the instance variables for a new class that could be used for a node in a tree where: (1) Each node contains int data, (2) Each node has up to four children, and (3) Each node also has a reference to its parent. Store the references to the children in an array of four components. 28. Draw a binary taxonomy tree that can be used for these four animals: Rabbit, Horse, Whale, Snake. 29. Using the BTNode class from, write a new static method of the BTNode class to meet the following specification. No recursion is needed. public static void subswap(BTNode root) // Precondition: Root is the root reference of a binary tree. // Postcondition: If root is non-null, then the original left subtree below // this root has been moved and is now the right subtree, and the original // right subtree is now the left subtree. // Example original tree: Example new tree: // 1 1 // / / // 2 3 3 2 // / / // 4 5 4 5 30. Redo the previous problem as a new non-static BTNode method. 31. Using the BTNode class from, write a new static method of the BTNode class to meet the following specification. public static intmanyNodes(BTNode root) // Precondition: root_ptr is the root reference of a binary tree. // Postcondition: The return value is the number of nodes in the tree. // NOTES: The empty tree has 0 nodes, and a tree with just a root has // 1 node. 32. Using the BTNode class, write a new static method of the BTNode class to meet the following specification. public static inttreeDepth(BTNode root) // Precondition: root_ptr is the root reference of a binary tree. // Postcondition: The return value is the depth of the binary tree. // NOTES: The empty tree has a depth of -1 and a tree with just a root // has a depth of 0. 33. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. 34. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. public static int count42(IntBTNode root) // Precondition: root is the root reference of a binary tree (but
  • 6. // NOT NECESSARILY a search tree). // Postcondition: The return value indicates how many times 42 appears // in the tree. NOTE: If the tree is empty, the method returns zero. 35. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. public static boolean has42(IntBTNode root) // Precondition: root is the root reference of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value indicates whether 42 appears somewhere // in the tree. NOTE: If the tree is empty, the method returns false. 36. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. public static boolean all42(IntBTNode root) // Precondition: root is the root reference of a binary tree (but // NOT NECESSARILY a search tree). // Postcondition: The return value is true if every node in the tree // contains 42. NOTE: If the tree is empty, the method returns true. 37. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. public static int sum(IntBTNode root) // Precondition: root is the root reference of a binary tree. // Postcondition: The return value is the sum of all the data in all the nodes. // NOTES: The return value for the empty tree is zero. 38. Suppose that we want to create a binary search tree where each node contains information of some data type. What additional factor is required for the data type? 39. Suppose that a binary search tree contains the number 42 at a node with two children. Write two or three clear sentences to describe the process required to delete the 42 from the tree. 40. Suppose IntBTNode is a BTNodewith integer data. Write a new static method of the IntBTNode class to meet the following specification. Make the method as efficient as possible (do not visit nodes unnecessarily). public static int count42(BTNode root) // Precondition: root is the root reference of a binary SEARCH tree. // Postcondition: The return value indicates how many times 42 appears // in the tree. 41. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. Make the method as efficient as possible (do not visit nodes unnecessarily). public static int max(BTNode root) // Precondition: root is the root reference of a nonempty binary SEARCH // tree. // Postcondition: The return value is the largest value in the tree. 42. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the IntBTNode class to meet the following specification. Make the method as efficient as possible (do not visit nodes unnecessarily). void insert42(BTNode root) // Precondition: root is the root reference of a binary SEARCH tree. // Postcondition: One copy of the number 42 has been added to the binary // search tree. 43. What is the importance of the stopping case in recursive methods?
  • 7. 44. Write a recursive method that has one parameter which is an int value called x. The method prints x asterisks, followed by x exclamation points. Do NOT use any loops. Do NOT use any variables other than x. 45. For the tree shown below, determine a. Which node is the root b. Which nodes are leaves c. The tree’s depth d. The result of preorder, postorder, inorder, and level-order traversals 46.For each node shown on the tree below: a. Name the parent node b. List the children c. List the siblings d. Compute the height e. Compute the depth f. Compute the size 47.What is the output of the following method for the tree shown below? 1 public static <AnyType> void mysteryPrint(BinaryNode<AnyType> t ) 2 { 3 if( t != null ) 4 { 5 System.out.println(t.getElement( ) ); 6 mysteryPrint(t.getLeft( ) ); 7 System.out.println(t.getElement( ) ); 8 mysteryPrint(t.getRight( ) ); 9 System.out.println(t.getElement( ) ); 10 } 11 } 48.Show the stack operations when an inorder and preorder traversal is applied to the tree.