You are on page 1of 2

  Home Study tools  My courses  My books Career Life 

home / study / engineering / computer science / computer science questions and answers...
Find solutions for your homework

Question: HERE IS SSet.java and DefaultComparator.java


: https://www.chegg.com/homework-help/questions...

HERE IS SSet.java and DefaultComparator.java :


https://www.chegg.com/homework-help/questions-and-
answers/question-nothing-java-file-defaultcomparatorjava-package-comp2402a4-import-
javautilcompara-q71137381

SOME OF THE CODE OF BINARYSEARCHTREE.JAVA IS MISSING


BECAUSE I COULDN'T UPLOAD IT ALL. I
DON'T THINK YOU WILL NEED BUT
IF YOU THINK YOU ARE MISSING SOME IMPORTANT CODE LET ME
KNOW.

============================================================

Please only do where it says //TODO

==============================================================

BinarySearchTree.java

==============================================================

package comp2402a4;

// Thanks to Pat Morin for the skeleton of this file!

import java.util.Comparator;

import java.util.Iterator;

import java.util.Random;

public class BinarySearchTree, T> extends

       BinaryTree implements SSet {

   protected Comparator c;

public static class BSTNode,T>

       extends BinaryTree.BTNode {

// Usually these would not be public, but for server tests it's
necessary.

       public T x; // Holds the data value


of this node.

public int s; // The size of the tree rooted at this node.

   }

/*

* Return the value that follows x (i.e. the minimum value

* y such that y > x. If no such element exists, return


null

* @return the successor value of x.

*/

public T succ(T x) {

// TODO: Implement this method. It should match slowSucc, but

// should be faster :-)

return null;

/*

* Return the value that follows x (i.e. the minimum value

* y such that y > x. If no such element exists, return


null

* @return the successor value of x.

*/

public T slowSucc(T x) {

// Does an inorder traversal in O(n) time.

Iterator it = iterator();

while( it.hasNext() ) {

T curr = (T)(it.next());

if( c.compare(curr, x) > 0 ) { // we have our first >


elt

return curr; // Return this element

return null; // never found anything

/*

* Return the value that precedes x (i.e. the maximum value

* y such that y < x. If no such element exists, return


null

* @return the predecessor value of x.

*/

public T pred(T x) {

// TODO: Implement this method. It should match slowPred, but

// should be faster :-)

return null;

/*

* Return the value that precedes x (i.e. the maximum value

* y such that y < x. If no such element exists, return


null

* @return the predecessor value of x.

*/

public T slowPred(T x) {

// Does an in-order traversal in O(n) time.

T pred = null; // Keep track of the predecessor of curr

Iterator it = iterator();

while( it.hasNext() ) {

T curr = it.next();

if( c.compare(curr, x) >= 0 ) { // we have our first >=


elt

return pred; // Return that last element we saw

pred = curr;

return pred; // never found anything >= elt

/*

* Return a BST that contains all elements >= x, and remove


them

* from this.

* @return the BST containing all elements >= x

*/

public BinarySearchTree chop(T x) {

Node sample = super.newNode();

BinarySearchTree other = new

BinarySearchTree(sample);

// TODO: Implement this method. It should match slowChop in

// behaviour, but should be faster :-)

return other;

/*

* Return a BST that contains all elements >= x, and remove


them

* from this.

* Runs in O(n*height) time, where in a balanced tree height is


O(log n).

* @return the BST containing all elements >= x

*/

public BinarySearchTree slowChop(T x) {

Node sample = super.newNode();

BinarySearchTree other = new

BinarySearchTree(sample);

// Iterate through the n nodes in-order.

// When see value >=x, add to new BST in O(height) time,


and

// remove it from this BST (on next iteration) in O(height)


time.

Iterator it = iterator();

T prev = null;

while( it.hasNext() ) {

T curr = (T)(it.next());

if( c.compare(curr, x) >= 0 ) { // we have our first >=


x

other.add(curr);

if( prev != null ) {

this.remove(prev); // safe to remove now

prev = curr;

if( prev != null ) {

this.remove(prev); // edge case, get that last one!

return other;

PLEASE HELP ANSWERED IN JAVA

Show transcribed image text

Expert Answer

Anonymous
answered this

* Return a BST that contains all elements >= x, and remove


them

* from this.

* @return the BST containing all elements >= x

*/

public BinarySearchTree chop(T x) {

Node sample = super.newNode();

BinarySearchTree other = new

BinarySearchTree(sample);

// TODO: Implement this method. It should match slowChop in

// behaviour, but should be faster :-)

return other;

/*

* Return a BST that contains all elements >= x, and remove


them

* from this.

* Runs in O(n*height) time, where in a balanced tree height is


O(log n).

* @return the BST containing all elements >= x

*/

2 Comment
Was this answer helpful? 0

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE
About Chegg Advertising Choices Cheap Textbooks Mobile Apps EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Sell Textbooks Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Solutions Manual Thinkful Help with eTextbooks
Corporate Development Intellectual Property Rights Chegg Study Help Study 101 Help to use EasyBib Plus
Investor Relations Terms of Use College Textbooks Textbook Rental Manage Chegg Study
Jobs Global Privacy Policy eTextbooks Used Textbooks Subscription
Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Digital Access Codes Return Your Books
Media Center Honor Code Learn Chegg Money Textbook Return Policy
Site Map Honor Shield Chegg Math Solver

© 2003-2021 Chegg Inc. All rights reserved.

You might also like