COP3530 Data structures

Purpose: This assignment has 4 main problems. These problems relate to some of the important topics covered in Modules 3, 4, and 5. Submitting Your Assignment: • Assignments must be turned in via Canvas. • Please follow these steps for every assignment:

1. You are allowed to upload only a single ZIP file and no other kinds of compressed files will be accepted 2. Please name your submission as 2_XXXXXXX.ZIP, where XXXXXXX is your seven-digit Panther ID number

3. Inside ZIP folder, there should be a separate folder for each question (i.e. Problem 1, Problem 2, Problem 3, etc)

4. For questions that require Java implementation: • The .java file must be inside the corresponding problem folder. DO NOT MIX ANSWERS. • ONLY SUBMIT .JAVA FILES. DO NOT SUBMIT YOUR WHOLE PROJETC’S FOLDER!!! • If is required, each .java file should contain ITS OWN main method at the bottom of the file. Please add only one main method for EACH .java file.

5. For written questions: • Submit these files INSIDE the specific problem folder. • Each answer MUST be identified. It should be easy to tell which question and subsection you are answering! • Written questions must be only in PDF format

. 6. Please include the following header for each Java program: /************************************************************** Purpose/Description: Author’s Panther ID: Certification: I hereby certify that this work is my own and none of it is the work of any other person. **************************************************************/

7. Submissions turned in after the due date and/or which don’t meet the established formatting rules will not be accepted.

 Failure to follow these simple directions may result in a loss of credit for the assignment. Problem #1: In this problem, you will write some Java code for simple operations on binary search trees where keys are integers.

Assume you already have the following code and assume that the method bodies, even though not shown, are correct and implement the operations as we defined them in class. public class BinarySearchTreeNode { public int key; public BinarySearchTreeNode left; public BinarySearchTreeNode right; } public class BinarySearchTree { private BinarySearchTreeNode root; public void insert(int key) { … } public void delete(int key) { … } public boolean find(int key) { … } }

(a) Add a method public int positiveKeySum() to the BinarySearchTree class that returns the sum of all non-negative keys in the tree. You will need an assistant/helper method.

(b) Add method public void deleteMax() to the BinarySearchTree class that deletes the maximum element in the tree (or does nothing if the tree has no elements).

(c) Add method public void printTree() to the BinarySearchTree class that iterates over the nodes to print then in decreasing order. So the tree… Produces the output “5 4 3 2 1”. Note: You will need an assistant/helper method.

(d) Add method public void printPostorder() to the BinarySearchTree class that prints out the nodes of the tree according to a “postorder” traversal. So the tree… Produces the output “1 3 2 5 4”.

Note: You will need an assistant/helper method.

Important Notes: • For this problem, you only need to submit the implementation of four methods in Java (positiveKeySum, deleteMin, printTree, and printPostorder). It is not required that you implement the main method.

Problem #2: (a) Show the result of every step of inserting the following sequence of elements into an initially empty AVL-tree: 10, 20, 15, 25, 30, 16, 18, 19.

(b) Show the resulting AVL-tree, after physical deletion (NOT a lazy deletion) of the record with the key 30 from the AVL tree that you got in the previous exercise.

(c) Show the result when an initially empty AVL-tree has keys 1 through 7 inserted in order (1,2,3,4,5,6,7).

(d) Draw an AVL-tree of height 4 that contains the minimum possible number of nodes.

(e) The following items are inserted into an AVL tree: 1, 2, 3, 8, 6. How many and what type of rotations are performed? Justify.

Note: We assume that double rotations count as one rotation. Important Notes: • For this problem, you don’t need to submit any implementation in Java. • For all parts of this problem, you must draw the AVL-trees using the appropriate graphics tools at your convenience. AVL-trees that were drawn by hand will be not accepted.

Powered by WordPress