CS3-Project-01

advertisement

Computer Science 3

Project # 01

Instructor: Dr. Hanh Pham

1) Requirements:

Given:

+ a file containing a class for binary tree nodes called “ BinaryTreeNode .java”.

+ a template file called “BinarySearchTree.java” (with description inside, to fill-in with your own codes).

+ a template file called “BinarySearchTree_TEST.java” (its description is below, to fill-in with your own codes).

+ sample input files “treeA.txt” and “treeB.txt” and expected result “treeC.txt”.

+ a sample screen output (expected for given treeA and treeB), in the file “pp01-OUTPUTsample.txt”.

Build:

+ a class for processing binary search trees called BinarySearchTree (see “BinarySearchTree-Description.doc” for more details on some methods). The template is at “BinarySearchTree.java”.

+ a class/program called BinarySearchTree_TEST (the template is at “BinarySearchTree_TEST.java”.) which uses the two above classes to do the following:

1) Input(read) a binary search treeA from a text file called “treeA.txt” using Preorder Traversal method

2) Print treeA 3 times on the screen using: inorderPrint(), preorderPrint(), postorderPrint() traversal methods and display it using the print(10) method in BinaryTreeNode .

3) Input a binary search treeB from a file called “treeB.txt” using Preorder Traversal method

4) Display treeB on the screen (using the print(10) method in BinaryTreeNode ).

5) Add treeB to treeA. The new tree is called C. Display treeC and write it to a file called “treeC.txt”.

6) Let the user enter a node value (an integer) Z. Count how many times this node/value occurs in treeC.

7) Make a copy of treeC. The new tree is called treeD. Display treeD.

8) Calculate the size of treeD and print out its size.

9) Let the user enter a node value (an integer) X. Add this node X to tree C and display the new treeC.

10) Let the user enter a node value (an integer) Y. Remove this node Y from tree D and display the new treeD.

(*) Display means to print out a tree using the print(10) method in BinaryTreeNode class.

2) Submission:

2.1 What to submit:

+ a tar/zip file of your codes called “ YourLastNameFirstInitial-p01.tar/zip”

( VERY IMPORTTANT) for each homework, create a directory/folder called “YourLastNameFirstInitial-N”

( N is the homework number, example: “SmithJ-01”) where you store all the necessary files and where you will work on your project. Pack tar/zip this directory/folder into a file called

“YourLastNameFirstInitial-N.tar/zip”.

Make sure that this directory includes at least the following 3 files:

 “BinaryTreeNode.java”

 “BinarySearchTree.java”

 “BinarySearchTree_TEST.java”

2.2 Where to submit: g o to http://cs.newpaltz.edu/~phamh/ac3/sub/ and select the right project number. Make sure that you fill the form correctly (VERY IMPORTANT). Upload your files.

3) Deadline: please see the SCHEDULE at our website

Submit on the 1 st day after the deadline(exp. the17 th for a deadline on the 16 th ): 50% penalty (example: a project gets 10 but was submitted late 1 day, the final score will be 5)

NO GRADING if submit 2 days or more after the deadline (on the 18 th and after).

DIAGRAMS are in NEXT PAGES

Figure 1.1 Diagram of Class Relationships in Project-01 class BinarySearchTree data private BinaryTreeNode root; methods // Constructor: create an empty tree with an empty root

BinarySearchTree()

1.

// Change or set ROOT of the tree to a given node(newroot)

public void setRoot(BinaryTreeNode newroot)

2.

// Get or return the ROOT of the tree

public BinaryTreeNode getRoot()

3.

// ADD or Insert a new node (element) into this TREE

public void add(int element)

4.

// Add the contents of another tree to this tree.

public void addAll(BinarySearchTree addend)

5.

// Add the contents of another binary search tree to this tree.

private void addTree(BinaryTreeNode addroot)

6.

// Count the number of occurrences of a particular DATA(target) in this tree.

public long countOccurrences(int target)

7.

// REMOVE a node which has its DATA = target in this tree.

public boolean remove(int target)

8.

// Determine the SIZE of the tree USING the size method in BinaryTreeNode

public int size( )

9.

// Create a new tree that contains all the elements from two other trees USING addAll(b)

public static BinarySearchTree union(BinarySearchTree b1, BinarySearchTree b2)

class BinaryTreeNode data private int data;

private BinaryTreeNode left;

private BinaryTreeNode right; methods // Constructor which initializes all three data fields

public BinaryTreeNode(int initialData, BinaryTreeNode initialLeft, BinaryTreeNode initialRight

// Constructor which creates an empty node

public BinaryTreeNode()

1.

// Read DATA of the node

public int getData( )

2.

// Read the LEFT LINK of the node

public BinaryTreeNode getLeft( )

3.

// Read the RIGHT LINK of the node

public BinaryTreeNode getRight( )

4.

// WRITE or change data of the node

public void setData(int newData)

5.

// WRITE or change LEFT LINK of the node

public void setLeft(BinaryTreeNode newLeft)

6.

// WRITE or change RIGHT LINK of the node

public void setRight(BinaryTreeNode newRight)

7.

// Check if the given node IS a LEAF ?

public boolean isLeaf( )

8.

// Read the DATA of the LEFT-MOST node

public int getLeftmostData( )

9.

// Read the DATA of the RIGHT-MOST node

public int getRightmostData( )

10.

// uses an INORDER traversal to PRINT on SCREEN the DATA

// of each node at or below this node of the binary tree.

public void inorderPrint( )

11.

// uses an PREORDER traversal to PRINT on SCREEN the DATA

// of each node at or below this node of the binary tree.

public void preorderPrint( )

12.

// uses an POSTORDER traversal to PRINT on SCREEN the DATA

// of each node at or below this node of the binary tree.

public void postorderPrint( )

13.

// Show DEPTH of nodes

// uses an INORDER traversal to PRINT on the screen the DATA of each node at or below this node

// of the binary tree(with INDENTATION to indicate the DEPTH of the nodes

// a dash "--" is printed at any place where a child has no sibling

// (the indentation of each line of data is four times its depth in the tree)

public void print(int depth)

14.

// REMOVE the LEFTMOST Node of the tree below this node.

// The return value is a reference to the root of the new (smaller) tree.

public BinaryTreeNode removeLeftmost( )

15.

//REMOVE the RIGHTMOST Node of the tree below this node.

// The return value is a reference to the root of the new (smaller) tree.

public BinaryTreeNode removeRightmost( )

16.

// COPY a binary tree. The return value is a reference to the root of the copy.

public static BinaryTreeNode treeCopy(BinaryTreeNode source)

17.

// SIZE of a tree => Count the NUMBER of NODEs in a binary tree.

public static int treeSize(BinaryTreeNode root)

class BinarySearchTree_TEST data methods

1.

// READ the TREE from a TEXT File, return the root

public static BinaryTreeNode ReadTreeFromTextFile(String FileName)

2.

// WRITE a tree NODE(r) to a TEXT file

public static void WriteNodeToTextFile(BinaryTreeNode r, FileWriter output)

3.

// WRITE a TREE (r) to a TEXT file

public static void WriteTreeToTextFile( BinaryTreeNode r, String FileName)

4.

// Generate a BINARY SEARCH tree randomly:

public static BinaryTreeNode GenBinTree()

5.

public static void main(String[] args)

// 1) INPUT/Read TreeA from “TreeA.txt”

// 2) Display TreeA:

// 3) INPUT/Read TreeB from “TreeB.txt”

// 4) Display TreeB:

// 5) Add treeB to treeA

// Writing TreeC to a File

// Display Tree

// 6) SEARCH for a NODE

// 7) Make a COPY of treeC

// Display TreeC

// 8) define the SIZE of treeD

// 9) ADD a NODE

// Display TreeC

// 10) REMOVE a NODE

// Display TreeD

Download