COMP250 Introduction to Computer Science
Task:
General instructions
• We are provided you with a scaffold to build your code on, i.e. starter code. You should download the scaffold first and then start coding. As with Assignments 1 and 2, you can find this code either your workspace on Ed or on mycourses.
• Search for the keyword updated to find any places where this PDF has been updated.
• T.A. office hours will be posted on mycourses under the “Office Hours” tab. For zoom OH, if there is a problem with the zoom link or it is missing, please email the T.A. and the instructor.
• If you would like a TA to look at your solution code outside of office hours, you may post a private question on the discussion board.
• We have provided you with some examples to test your code. (See TestExposedA3.java on mycourses. The same tests are run on Ed when you submit.) If you pass all these exposed tests, you will get 59/100. Unlike with Assignments 1 and 2, the remaining tests will be private. Therefore, we strongly encourage you to come up with more creative test cases, and to share these test cases on the discussion board. There is a crucial distinction between sharing test cases and sharing solution code. We encourage the former, whereas the latter is a serious academic offense.
• Policy on Academic Integrity: See the Course Outline Sec. 7 for the general policies. You are also expected to read the posted checklist PDF that specifies what is allowed and what is not allowed on the assignment(s).
• Late policy: Late assignments will be accepted up to two days late and will be penalized by 10 points per day. If you submit one minute late, this is equivalent to submitting 23 hours and 59 minutes late, etc. So, make sure you are nowhere near that threshold when you submit.
Tertiary Search Tree (TST)
The purpose of this assignment is to give you some experience with recursion. Recursion is a fundamental method in programming and the sooner you learn it, the better! The topic for this assignment is trees. You will work with a data structure that we will call a tertiary search tree or TST.1 This is like a binary search tree (BST), except that each node in a TST has three children rather than two. We can call the three children left, middle, and right. Each node stores a element whose class type implements Comparable, so you can assume access to a compareTo() method. The subtrees defined by the left, middle, and right children of a node have elements that are less than, equal to, or greater than the element at that node, respectively.
Lecture 25 gave pseudocode for binary search trees (BST) methods. You will implement similar methods for your TST class. Make sure you understand the BST pseudocode before you try to implement a TST version!
You will work with three classes:
TSTNode class (25 points)
A TSTNode has four fields: an element, and three children (left, middle, right). For this class, you will write:
• a constructor TSTNode(T element) (5 points) – assigns the given element to this node; the children are automatically initialized to null.
• height() (10 points) – returns an int which is the height of the subtree, whose root is this TSTNode
• findMin() (5 points)– returns the TSTNode containing the minimum element in the tree; you can use this as a helper method.
• findMax() (5 points)– return the TSTNode containing the maximum element in the tree; you can use this as a helper method.
You may add your own helper methods to the TSTNode class. For example, you may wish to overload the constructor.
TST class (55 points)
A TST has just one field: the root TSTNode of the tree. A TST has several methods that are provided to you:
• height() – returns an int which is the height of the tree; it depends on the height() helper method in the TSTNode class (see above)
• toString()– toString() can be used to visualize the tree structure; recall that you can ’call’ the toString() method using System.out.print(tree) where tree is of type TST;
1This is not to be confused with a “ternary search tree” which is something else. Also, others may have used the term “tertiary search tree” to mean something other than what we mean here. We will ignore these other usages of the term.
• stringify(. . . ) – a helper method used by toString()
• inorderPrintAsList() – prints the elements in order using an enhanced for loop; the enhanced for loop implicitly uses an Iterator, and so you will need to implement the Iterator (see below) before you can use this print method;
Write the following methods for the TST class. Hint: you may find it easier to write some of these methods if you write helper versions which have a TST Node (root) parameter. These helper methods can be either in the TST class or in the TSTNode class.
• insert(T element ) (10 points) – void method; it creates a new node in the tree and assigns the element of that node; the node should be placed in the correct position; duplicates are allowed.
• contains(T element) (5 points) – returns a boolean (true if the tree contains a node with this element, and false otherwise)
• iterator() – returns an TSTIterator for the TST. See the TSTIterator class below. This will be evaluated as part of the tests for TSTIterator.
• rebalance() (20 points) – void method; specifications are as follows.
Just as a BST can become imbalanced as discussed in the lecture, so can a TST. Methods for balancing BST’s are covered in COMP 251. For simplicity, we consider the following simple rebalancing method that uses concepts from COMP 250 only.
We define rebalance() for a TST as follows. Traverse the tree in-order and make an arraylist of the sorted elements. If there are duplicates (multiple instances of the same element), then include them all in the list. Next, recursively partition the list similar to what is done in binary search: consider the element at the middle position size/2; this element will be the one at the root node of the rebalanced TST; all elements that are less than, equal to, or greater than the root element will be stored in the left, middle, or right child subtree, respectively. You may use the List.subList() method here.
Note that this is not necessarily going to give a “balanced” tree in the intuitive sense of balance. For example, if the tree has elements 1, 1, 1, 1, 5, 5, 5, 5, 5, 5 then the “rebalanced” tree would have element 5 at the root node, the root would have no right child, and the root node’s left subtree would have height 3. For TST’s that have relatively few duplicates, the rebalance() method would do a better job of rebalancing.
• remove(T element) (20 points) – returns nothing (void); it finds a node containing this element and removes it; if there are duplicate elements, then exactly one instance must be removed; which one to remove is up to you, and indeed we have no way to distinguish which one you remove without manually examining your code;
To test correctness of the remove method, we need a policy for the case that only one instance of the element to be removed is present and the node containing that element has both left and right children. In principle, either of two policies could be used to replace the element at that node: take the largest element in the left subtree, or take the smallest element in the right subtree. However, for grading, we want a consistent policy for everyone. We require that you take the largest element of the left subtree. Note that this is the opposite remove policy of what was used in the BST lecture slides: we use the opposite policy to make an important point that sometimes two policies are equally good and yet one needs to use just one in the specification.
TSTIterator class (20 points)
This class implements Iterator. Write the following methods:
• a TSTIterator constructor
• hasNext()
• next()
The constructor should create an ordered list of all the elements in the tree. The hasNext() and next() methods then use this list. For grading, we won’t test the constructor directly. Rather we will test that the Iterator works as it should, namely iterating through the list should yield the elements in their proper order.
Note: since TST implements Iterable, you can use the Java enhanced for loop, once you have successfully implemented the TSTIterator class.
Where to start? How to proceed?
We suggest that you implement and test in the following order:
1. TSTNode constructor
2. TST insert() and contains()
3. TSTNode height(), findMin(), findMax()
4. TST iterator()
5. TST rebalance()
6. TST remove()
COMP250 Introduction to Computer Science
Answer in Detail
Solved by qualified expert
Get Access to This Answer
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Amet dictum sit amet justo donec enim diam vulputate ut. Neque convallis a cras semper auctor neque vitae. Elit at imperdiet dui accumsan. Nisl condimentum id venenatis a condimentum vitae sapien pellentesque. Imperdiet massa tincidunt nunc pulvinar sapien et ligula. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere. Et ultrices neque ornare aenean euismod. Suscipit tellus mauris a diam maecenas sed enim. Potenti nullam ac tortor vitae purus faucibus ornare. Morbi tristique senectus et netus et malesuada. Morbi tristique senectus et netus et malesuada. Tellus pellentesque eu tincidunt tortor aliquam. Sit amet purus gravida quis blandit. Nec feugiat in fermentum posuere urna. Vel orci porta non pulvinar neque laoreet suspendisse interdum. Ultricies tristique nulla aliquet enim tortor at auctor urna. Orci sagittis eu volutpat odio facilisis mauris sit amet.
Tellus molestie nunc non blandit massa enim nec dui. Tellus molestie nunc non blandit massa enim nec dui. Ac tortor vitae purus faucibus ornare suspendisse sed nisi. Pharetra et ultrices neque ornare aenean euismod. Pretium viverra suspendisse potenti nullam ac tortor vitae. Morbi quis commodo odio aenean sed. At consectetur lorem donec massa sapien faucibus et. Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. Duis at tellus at urna condimentum mattis pellentesque. Vivamus at augue eget arcu dictum varius duis at. Justo donec enim diam vulputate ut. Blandit libero volutpat sed cras ornare arcu. Ac felis donec et odio pellentesque diam volutpat commodo. Convallis a cras semper auctor neque. Tempus iaculis urna id volutpat lacus. Tortor consequat id porta nibh.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Amet dictum sit amet justo donec enim diam vulputate ut. Neque convallis a cras semper auctor neque vitae. Elit at imperdiet dui accumsan. Nisl condimentum id venenatis a condimentum vitae sapien pellentesque. Imperdiet massa tincidunt nunc pulvinar sapien et ligula. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere. Et ultrices neque ornare aenean euismod. Suscipit tellus mauris a diam maecenas sed enim. Potenti nullam ac tortor vitae purus faucibus ornare. Morbi tristique senectus et netus et malesuada. Morbi tristique senectus et netus et malesuada. Tellus pellentesque eu tincidunt tortor aliquam. Sit amet purus gravida quis blandit. Nec feugiat in fermentum posuere urna. Vel orci porta non pulvinar neque laoreet suspendisse interdum. Ultricies tristique nulla aliquet enim tortor at auctor urna. Orci sagittis eu volutpat odio facilisis mauris sit amet.
Tellus molestie nunc non blandit massa enim nec dui. Tellus molestie nunc non blandit massa enim nec dui. Ac tortor vitae purus faucibus ornare suspendisse sed nisi. Pharetra et ultrices neque ornare aenean euismod. Pretium viverra suspendisse potenti nullam ac tortor vitae. Morbi quis commodo odio aenean sed. At consectetur lorem donec massa sapien faucibus et. Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. Duis at tellus at urna condimentum mattis pellentesque. Vivamus at augue eget arcu dictum varius duis at. Justo donec enim diam vulputate ut. Blandit libero volutpat sed cras ornare arcu. Ac felis donec et odio pellentesque diam volutpat commodo. Convallis a cras semper auctor neque. Tempus iaculis urna id volutpat lacus. Tortor consequat id porta nibh.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Amet dictum sit amet justo donec enim diam vulputate ut. Neque convallis a cras semper auctor neque vitae. Elit at imperdiet dui accumsan. Nisl condimentum id venenatis a condimentum vitae sapien pellentesque. Imperdiet massa tincidunt nunc pulvinar sapien et ligula. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere. Et ultrices neque ornare aenean euismod. Suscipit tellus mauris a diam maecenas sed enim. Potenti nullam ac tortor vitae purus faucibus ornare. Morbi tristique senectus et netus et malesuada. Morbi tristique senectus et netus et malesuada. Tellus pellentesque eu tincidunt tortor aliquam. Sit amet purus gravida quis blandit. Nec feugiat in fermentum posuere urna. Vel orci porta non pulvinar neque laoreet suspendisse interdum. Ultricies tristique nulla aliquet enim tortor at auctor urna. Orci sagittis eu volutpat odio facilisis mauris sit amet.
Tellus molestie nunc non blandit massa enim nec dui. Tellus molestie nunc non blandit massa enim nec dui. Ac tortor vitae purus faucibus ornare suspendisse sed nisi. Pharetra et ultrices neque ornare aenean euismod. Pretium viverra suspendisse potenti nullam ac tortor vitae. Morbi quis commodo odio aenean sed. At consectetur lorem donec massa sapien faucibus et. Nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus. Duis at tellus at urna condimentum mattis pellentesque. Vivamus at augue eget arcu dictum varius duis at. Justo donec enim diam vulputate ut. Blandit libero volutpat sed cras ornare arcu. Ac felis donec et odio pellentesque diam volutpat commodo. Convallis a cras semper auctor neque. Tempus iaculis urna id volutpat lacus. Tortor consequat id porta nibh.
MyAssignmenthelp.com has the largest base of the most talented homework help tutors in Australia. We provide help with homework for various subjects including English, geography and math homework help, etc. So if youre looking for an agency that can do my homework cheap or help me with my homework, connect with us.