subject
Computers and Technology, 03.04.2020 20:16 jmalfa

In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree. java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought of somewhat like an interface but with some methods actually implemented). You will need to write a BetterBST class which extends BinarySearchTree. Your BetterBST class can then be treated just like a regular BinarySearchTree, just with some additional functionality.

The methods that you will need to implement in BetterBST perform various algorithms on BST instances. For some of these methods, you may find it convenient to implement a private helper method as you did in previous assignments.

public T smallestGreaterThan(T t) - given some generic comparable value t, find the smallest value in the BST that is larger than t. For example, if a binary search tree contains the values 1, 3, and 5, and the function is called with t = 2, it should return 3.
public abstract class BinarySearchTree>
{
// implement these!
abstract int height();
abstract T imbalance();
abstract BinarySearchTree mirror();
abstract T smallestGreaterThan(T t);
abstract void prettyPrint();

// stripped down from https://users. cs. fiu. edu/~weiss/dsaajava2/code/BinarySea rchTree. java
public BinarySearchTree( )
{
root = null;
}

public void insert( T x )
{
root = insert( x, root );
}

public void remove( T x )
{
root = remove( x, root );
}

public T findMin( )
{
if(root == null)
throw new NullPointerException( );
return findMin( root ).data;
}

public boolean contains( T x )
{
return contains( x, root );
}

private BinaryNode insert( T x, BinaryNode t )
{
if( t == null )
return new BinaryNode( x, null, null );

int compareResult = x. compareTo( t. data );

if( compareResult < 0 )
t. left = insert( x, t. left );
else if( compareResult > 0 )
t. right = insert( x, t. right );
else
; // Duplicate; do nothing
return t;
}

private BinaryNode remove( T x, BinaryNode t )
{
if( t == null )
return t; // Item not found; do nothing

int compareResult = x. compareTo( t. data );

if( compareResult < 0 )
t. left = remove( x, t. left );
else if( compareResult > 0 )
t. right = remove( x, t. right );
else if( t. left != null && t. right != null ) // Two children
{
t. data = findMin( t. right ).data;
t. right = remove( t. data, t. right );
}
else
t = ( t. left != null ) ? t. left : t. right;
return t;
}

private BinaryNode findMin( BinaryNode t )
{
if( t == null )
return null;
else if( t. left == null )
return t;
return findMin( t. left );
}

private boolean contains( T x, BinaryNode t )
{
if( t == null )
return false;

int compareResult = x. compareTo( t. data );

if( compareResult < 0 )
return contains( x, t. left );
else if( compareResult > 0 )
return contains( x, t. right );
else
return true; // Match
}

static class BinaryNode
{

BinaryNode( T thedata )
{
this( thedata, null, null );
}

BinaryNode( T thedata, BinaryNode lt, BinaryNode rt )
{
data = thedata;
left = lt;
right = rt;
}

T data; // The data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}

BinaryNode root;
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 05:00
Pls do you believe that the use of 3d animation has grown in feature films over the last few years? if so, do you think the trend will continue? what are the forces driving this trend?
Answers: 2
question
Computers and Technology, 22.06.2019 14:10
Dean wants a quick way to look up staff members by their staff id. in cell q3, nest the existing vlookup function in an iferror function. if the vlookup function returns an error result, the text “invalid staff id” should be displayed by the formula. (hint: you can test that this formula is working by changing the value in cell q2 to 0, but remember to set the value of cell q2 back to 1036 when the testing is complete.)
Answers: 3
question
Computers and Technology, 23.06.2019 02:30
What is the power dissipated by a resistor with a current of 0.02 a and a resistance of 1,000 ? a. 200 w b. 20 w c. 0.4 w d. 4 w
Answers: 1
question
Computers and Technology, 23.06.2019 02:30
How to launch an app: steps to be successful? launching an app is a great idea, but it’s not that easy as we supposed to think. the majority of mobile applications don’t generate revenue because companies aren’t ready to be competitive. referring to our experience in successfully building and launching apps we hope to you omit these difficulties. we are going to talk about ideas, marketing, testing your product, its development, distribution and support. you will learn 8 product launch stages to succeed.
Answers: 1
You know the right answer?
In this problem, you will implement various algorithms operating on binary search trees. We have pro...
Questions
question
Mathematics, 26.10.2021 17:50
question
Mathematics, 26.10.2021 17:50
question
History, 26.10.2021 17:50
Questions on the website: 13722367