CHC5223 Data Structures and Algorithms Assignment 2: Binary search tree
CHC5223 Data Structures and Algorithms
2023–2024 Semester 2
Assignment 2
Value 60% of coursework: Part A is 30% and Part B is 30% Individual work
Learning outcomes
Students will be able to understand
1.1 Data structures
1.2 The applications of data structures
1.3 Object-oriented programming concepts
1.4 Methods for program testing
Students will have acquired skills in:
2.1 Data abstraction
2.2 The use of data structures
2.3 Programming at a more advanced level in a high-level object-oriented language
2.4 Program testing and documentation
Students will have acquired skills in:
3.1 Self-management
3.2 Learning
3.3 Communication
3.4 Problem solving
3.5 Information technology
Submission requirements
The assignment submitted should compressed into a .zip file, the following files should be contained in the compressed file:
-
a report as a Microsoft Word document containing text of all your classes. filename format: student ID+CHC5223_CW2_Report.docx
-
a .zip file containing the project: the runnable jar file (if available) and all the program’s source texts (.java), including those provided
filename format: student ID +CHC5223_ CW2_Files.zipPart A – binary search tree
General requirements
All your programming must conform to “Java Conventions and Programming Guidelines” – see module Moodle site.
You must paste the source code of all your classes into your report, as text or images. Introduction
The topic of this part of the assignment is binary search trees.
The interface IMemberDB describes methods for an abstract data type (ADT) which holds adatabase of Member objects.
You must implement IMemberDB as a binary search tree for Assignment 2.You must not make any changes to these interfaces.
1 of 15
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
Requirements
Basic rules
You must create one executable project after completing all tasks. One Java class should be defined in one .java file respectively.
In the report, the source code of each task, together with the corresponding explanation, should be presented separately.
Failure to comply with these rules will result in zero marks.
Task 1
You must create a Java class called MemberBST that implements the interface IMemberDB. You must use a binary search tree but it does not need to be self-balancing.
You must not encapsulate existing implementations of collections in your submission. For example, you must not create a TreeMap object and call methods on that object from your class. Failure to comply with this will result in zero marks for that part.
Tip: use String.compareTo to compare strings lexicographically. You can treat uppercase and lowercase as different. (Hash codes have no place in this assignment.)
Methods can be implemented either iteratively or recursively. You must not implement the method remove by just building a new tree.
You may make use of the supplied source code for the method remove, based on Object- Oriented Programming in Oberon-2, Hanspeter Mössenböck Springer-Verlag 1993, page 78, transcribed into Java by David Lightfoot (see Appendix).
The constructor for MemberBST must print the string “Binary Search Tree” to System.out. Take care that you have not used a linear search O(n) where you should have used a binary
search tree, aiming towards O(logn).
You must give clear rationales and detailed explanations of your design and implementation in
the report.
Task 2
5 marks
5 marks
You must make appropriate use of assertions (assert statements) to protect preconditions of the operations. Remember to enable assertion checking for your project.
You must give clear rationales and detailed explanations of your design and implementation in the report.
Task 3
2 mark
• for every addition of a Member (put), for every retrieval of a Member(get), for every deletion of a Member(remove):
o theMembername;
You must make your class log monitoring information, either to a text file or by calls of System.out.println.
It must log (at least):
2 of 15
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
o the sequence of nodes of the tree visited. • Paste your log into your report.
We have supplied a main program CHC5223.java for your use with this assignment. The name of the file is set in the static variable filename.
Sample files sampleMembersUK.csv and sampleMembersUS.csv each contain 500 members in this format.
3 marks
You must give clear rationales and detailed explanations of your design and implementation in the report.
3 mark
Task 4
You must devise a test plan for your implementation. Be sure to check (among many other cases).
-
that deleting a leaf node works correctly
-
that deleting a node with one descendant works correctly
-
that deleting a node with two descendants works correctly
2 marks
You must give clear rationales and detailed explanations of your design and implementation in the report.
3 mark
You must give clear evidence and detailed explanations of your test in the report, including your test plan, test data used, expected results and actual results. You must show your actual results and the logging information copied from your log file or the output pane of your IDE. Do not simply state “test passed”, or similar.
5 mark
Task 6
You must comment on the time efficiency and space efficiency of your implementation of the binary search tree.
Task 5
By using the supplied main program, or by other means, you must test your MemberBST.
You must give clear rationales and detailed explanations in the report.
3 of 15
2 mark total 30 marks
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
Part B – graphs and pathfinding
The topic of this part of Assignment 2 is graphs and pathfinding.
General requirements
All your programming must conform to “Java Conventions and Programming Guidelines”. You must paste the source code of all your classes into your report, as text, not images. You must implement all necessary data structures using only arrays.
When programming in Java it is usual to make use of collection classes from the Java class library. However, if you need to program in some other language such classes, or their equivalents, will not necessarily be available.
Task 1
Find or devise a transport network – it need not be real, but it must be realistic. It must be an undirected, connected graph, with no loops. It must have at least eight nodes.
Each node should have a name (with no spaces in it) and x and y positions (0 x < 256 and
0 y < 256) indicating the approximate position of the node on a map with a 256 × 256 grid (y
increasing downwards).
The links must contain information about the distance between the nodes it connects. The distance can be measured in suitable units of length, such as km, or time, such as minutes.
The links between them must be such that there are several pairs of nodes that are linked by more than one route.
You must sketch your network and include it in your report. The sketch must show each node annotated with its name and located on the sketch at its x and y position. The sketch must show each link annotated with its distance. You can make the sketch by hand but if you do so you will need to scan it to include in your report.
Task 2
You must express your network as a text file using the syntax:
“station” name x y
“link” station station distance
Each station must have been defined in a station line before being cited in a link line.
Include the content of the text file in your report.
Task 3
2 marks
You must implement Java classes StackInt, QueueInt, ListInt and SetInt. These will be subclasses respectively of abstract classes AbsStackInt, AbsQueueInt, AbsListInt, AbsSetInt, whose source is provided and shown in the appendixes. These are given as abstract classes (to be ‘sub-classed’) rather than as interfaces (to be ‘implemented’) partly because they are all ‘bounded’, that is, with limited capacity because of being implemented by arrays, and also to allow us to give you hints on how to implement them.
4 of 15
2 marks
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
The required behaviour of the methods of the classes is indicated as pre- and post-conditions in comments.
You must give clear rationales and detailed explanations of your design and implementation in the report.
4 marks
Task 4
You must make appropriate use of assertions (assert statements) to protect preconditions of the operations. Remember to enable assertion checking for your project.
1 mark
Task 5
By using JUnit or otherwise you must test your implementations of StackInt, QueueInt, ListInt and SetInt. To do this, create objects of each class in which capacity is set to a low value, such as 10. You will set it to something larger when deploying the classes later.
You must give clear evidence and detailed explanations of your design and implementation in the report, including the test plan, test data used, expected results, and actual results. Your actual results must be shown either as screenshot images or as text copied from the output pane of the development toolkit.
4 marks
Task 6
You must create a Java class StationInfo to implement the Java interface IStationInfo (supplied). 1 mark
Task 7
You must create a Java class to define an adjacency matrix
final double NO_LINK = Double.MAX_VALUE; // was erroneous double.MAX_VALUE
int numStations; // 0 <= numStations <= capacity
double distance[ ][ ];
where, for all i, j in 0 <= numStations <= capacity
distance [i][j] is the distance from station with number i to station with number j
distance [i][j] is equal to distance [j][i]
distance [i][i] = NO_LINK
Task 8
and also define a list of objects of class StationInfo held in an array. Task 9
You must create a Java method in this class that reads a text file containing the textual description of your network and builds the corresponding list of stations and the matrix of links.
5 of 15
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
You must perform as many validity checks in this as you can and report any errors. Include the text of this class in your report.
You must give clear rationales and detailed explanations of your design and implementation in the report.
2 marks
Task 10
Use print statements to show the contents of the list and the matrix after using your program to build them from the data held in your text file. Copy the results into your report.
You must give clear rationales and detailed explanations of your design and implementation in the report.
2 marks
Task 11
You must write Java methods (as in Appendixes) to perform a depth-first traversal from a given node of your network and a breadth-first traversal from the same node, making use of the algorithms given in the appendix. Include the methods text and the resulting sequence of node names in your report.
You must give clear rationales and detailed explanations of your design and implementation in the report.
4 marks
Task 12
You must implement Dijkstra’s algorithm (as in Appendix) making use of the data structures you have constructed, to find and display the shortest path between two stations in your network. You must also ‘instrument’ your implementation to count the number of iterations of the while loop
You must give clear rationales and detailed explanations of your design and implementation in the report.
4 marks
Task 13
You must give clear rationales and detailed explanations of the difference between Dijkstra’s algorithm and A* algorithm and state how their behavior would differ in the case of your network.
4 marks total 30 marks
6 of 15
CHC5223 Data Structures and Algorithms 2023–2024 Semester 2
Obtaining help
It is encouraged to request further clarification on what is required for this assignment. Please try to do this during normal contact time and avoid asking for such help in the last week before the deadline.
You can discuss the requirements and the material covered in the assignment with others but what you create must be all your own work. Be careful to avoid collusion.
Declare in your report any help you have received other than that from the module teaching team.
Feedback
In addition to the written feedback that we aim to provide within the normal interval, you will be able to obtain fast, brief, verbal formative feedback and help on correcting your work at your practical classes.
7 of 15
CHC5223 Data Structures and Algorithms
2023–2024 Semester 2
Appendix: remove private class Node {
private Member data;
private Node left, right;
public Node(Member s) {data = s; left = null; right = null;}
} // Node
private Node root;
public Member remove(String name){
// based on Object-Oriented Programming in Oberon-2
// Hanspeter Mössenböck Springer-Verlag 1993, page 78
// transcribed into Java by David Lightfoot
// put assert statement for preconditions here
Node parent = null, del, p = null, q = null;
Member result;
del = root;
while (del != null && !del.data.getName().equals(name)) {
parent = del;
if (name.compareTo(del.data.getName()) < 0)
del = del.left; else
del = del.right;
}// del == null || del.data.getName().equals(name))
if(del != null) { // del.data.getName().equals(name)
// find the pointer p to the node to replace del
if (del.right == null) p = del.left;
else if (del.right.left == null) {
p = del.right; p.left = del.left; } else {
p = del.right;
while (p.left != null) {q = p; p = p.left;}
q.left = p.right; p.left = del.left; p.right = del.right; }
if(del == root) root = p;
else if (del.data.getName().compareTo(parent.data.getName()) < 0)
parent.left = p;
else parent.right = p;
// reduce size of tree by one
result = del.data;
}
else result = null;
return result; } // remove