Practice Exam 1 Version A - Algorithms / Data Structures | CP SC 212, Exams of Algorithms and Programming

Material Type: Exam; Class: ALGS/DATA STRUCTURES; Subject: COMPUTER SCIENCE; University: Clemson University; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-pkc
koofers-user-pkc 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPSC 212-301 Name: _____________________________
Test #1A September 21, 2005
Honor Pledge:
I pledge that I have neither given nor received unauthorized assistance on this test.
Signed: _______________________ Date: _____________
20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web page. You are to
develop a class called My1DTree with one constructor:
public My1DTree (int x[]) {
// ... your code ...
} // My1DTree
which receives an int array which is a one-dimensional representation of a binary tree (recall the root
element is in element 1 of of the array), and four public methods:
public String preorder () { public String inorder () {
// ... your code ... // ... your code ...
} // preorder } // inorder
public String postorder () { public String levelorder () {
// ... your code ... // ... your code ...
} // postorder } // levelorder
which return Strings containing the tree’s preorder, inorder, postorder, and levelorder traversals,
respectively.
For example, the following test driver (you may download it now from the CPSC 212 Schedule page,
September 21):
public static void main(String args[]) {
// -1 indicates that an array element is empty
int node[] = { -1, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1 };
System.out.print("Array contents: ");
for (int i=0; i<node.length; i++)
System.out.print(node[i] + ", ");
System.out.println("");;
My1DTree t = new My1DTree(node);
System.out.println("Preorder: " + t.preorder());
System.out.println("Inorder: " + t.inorder());
System.out.println("Postorder: " + t.postorder());
System.out.println("Levelorder: " + t.levelorder());
} // end main
produces the following output:
Array contents: -1, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1,
Preorder: 1 2 4 5 3 6 7
Inorder: 4 2 5 1 6 3 7
Postorder: 4 5 2 6 7 3 1
Levelorder: 1 2 3 4 5 6 7
pf2

Partial preview of the text

Download Practice Exam 1 Version A - Algorithms / Data Structures | CP SC 212 and more Exams Algorithms and Programming in PDF only on Docsity!

CPSC 212-301 Name: _____________________________

Test #1A September 21, 2005

Honor Pledge:

I pledge that I have neither given nor received unauthorized assistance on this test.

Signed: _______________________ Date: _____________

20 points. 50 minutes. Open Java API. Open cucs API. You may not visit any other web page. You are to

develop a class called My1DTree with one constructor:

public My1DTree (int x[]) { // ... your code ... } // My1DTree

which receives an int array which is a one-dimensional representation of a binary tree (recall the root

element is in element 1 of of the array), and four public methods:

public String preorder () { public String inorder () { // ... your code ... // ... your code ... } // preorder } // inorder public String postorder () { public String levelorder () { // ... your code ... // ... your code ... } // postorder } // levelorder

which return Strings containing the tree’s preorder, inorder, postorder, and levelorder traversals,

respectively.

For example, the following test driver (you may download it now from the CPSC 212 Schedule page,

September 21):

public static void main(String args[]) { // -1 indicates that an array element is empty int node[] = { -1, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1 }; System.out.print("Array contents: "); for (int i=0; i<node.length; i++) System.out.print(node[i] + ", "); System.out.println("");; My1DTree t = new My1DTree(node); System.out.println("Preorder: " + t.preorder()); System.out.println("Inorder: " + t.inorder()); System.out.println("Postorder: " + t.postorder()); System.out.println("Levelorder: " + t.levelorder()); } // end main

produces the following output:

Array contents: -1, 1, 2, 3, 4, 5, 6, 7, -1, -1, -1, -1, -1, Preorder: 1 2 4 5 3 6 7 Inorder: 4 2 5 1 6 3 7 Postorder: 4 5 2 6 7 3 1 Levelorder: 1 2 3 4 5 6 7

Note that a value of -1 indicates that an array element is empty. This test driver is provided only as an

example. You should test your code with a more rigorous test driver.

Submission:

Submit your work using: handin.212.301 1 My1DTree.java

Also submit any helper methods and/or classes you developed during this test and required by your class.

Important:

If you cannot complete one or more of the traversal methods, be sure to include at least a skeleton of the

method which returns the String:

“Method ___________ not completed”

so that my test driver will be able to compile your program.

Evaluation:

Your program will be evaluated on: (a) correctness of algorithm, and (b) efficiency of algorithm.