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: In this problem, you will implement various algorithms operating
on binary search trees. We have ...
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<T extends
Comparable<? super T>>
// implement these!
abstract int height();
abstract T imbalance();
abstract BinarySearchTree<T> mirror();
abstract T smallestGreaterThan(T t);
abstract void prettyPrint();
// stripped down from
https://users.cs.fiu.edu/~weiss/dsaajava2/code/BinarySearchTree.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<T> insert( T x, BinaryNode<T> t
)
if( t == null )
return new BinaryNode<T>( 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<T> remove( T x, BinaryNode<T> 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<T> findMin(
BinaryNode<T> t )
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
private boolean contains( T x, BinaryNode<T> 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<T>
{
BinaryNode( T thedata )
this( thedata, null, null );
BinaryNode( T thedata, BinaryNode<T> lt,
BinaryNode<T> rt )
data = thedata;
left = lt;
right = rt;
T data; // The data in the node
BinaryNode<T> left; // Left child
BinaryNode<T> right; // Right child
BinaryNode<T> root;
Expert Answer
M_Rifas
answered this
Here is the completed code for this problem. Comments
are included, go through it, learn how things
work and let me know
if you have any doubts or if you need anything to change. If you
are satisfied with
the solution, please rate the answer.
Thanks
// BetterBST.java
public class BetterBST<T extends Comparable<T>>
extends BinarySearchTree<T> {
@Override
int height() {
return height(root);
}
// recursive helper method to find the height of the tree
private int height(BinaryNode<T> node) {
// if node is null, returning 0 (end condition)
if (node == null) {
return 0;
}
// finding height of left subtree
int leftHeight = height(node.left);
// finding height of right subtree
int rightHeight = height(node.right);
// finding which among the two is higher, and add 1 to it
(current
// level) and returning it
return 1 + (leftHeight > rightHeight ? leftHeight :
rightHeight);
}
@Override
T imbalance() {
// specification not mentioned, so not coded
return null;
}
@Override
BinarySearchTree<T> mirror() {
//creating a BST
BinarySearchTree<T> tree = new BetterBST<T>();
//taking copy of this tree and assigning to the above tree
tree.root = copy(root);
//mirroring nodes of copied tree
mirror(tree.root);
//returning mirrored tree
return tree;
}
//private helper method to copy a binary search tree, given a root
node
private BinaryNode<T> copy(BinaryNode<T> node) {
if (node == null) {
//base condition.
return null;
}
//creating a node with data = node's data
BinaryNode<T> root = new BinaryNode<T>(node.data);
//copying left subtree and assigning to root.left
root.left = copy(node.left);
//copying right subtree and assigning to root.right
root.right = copy(node.right);
//returning copied tree
return root;
}
//private helper method to mirror the nodes of a tree
private void mirror(BinaryNode<T> node) {
if (node == null) {
//end condition
return;
}
//swapping left and right nodes
BinaryNode<T> temp = node.left;
node.left = node.right;
node.right = temp;
//mirroring left subtree
mirror(node.left);
//mirroring right subtree
mirror(node.right);
}
@Override
T smallestGreaterThan(T t) {
// using recursive method to find the smallest element which is
greater
// than t
return smallestGreaterThan(t, root);
}
// recursive helper method to find the smallest element which is
greater
// than t
private T smallestGreaterThan(T t, BinaryNode<T> node) {
// if node is null, returning null.
if (node == null) {
return null;
}
// if node is smaller than or equal to t, calling the method
// recursively, passing the right subtree
if (node.data.compareTo(t) <= 0) {
return smallestGreaterThan(t, node.right);
}
// if left node is null, we have found the smallest element greater
than
// t, returning it
if (node.left == null)
return node.data;
// otherwise, returning smallest element greater than t in left
subtree
return smallestGreaterThan(t, node.left);
}
@Override
void prettyPrint() {
prettyPrint(root, "");
}
// recursive method to print the BST. Even though specification is
not given
// I'm just writing this according to my understanding
// here indentation specifies the indentation of the node
void prettyPrint(BinaryNode<T> node, String indentation)
{
if (node == null) {
// end condition
return;
}
// printing right subtree if not null, one tab additional spacing
before
// it
if (node.right != null) {
prettyPrint(node.right, indentation + "\t");
}
// printing current node, with indentation specified in
parameter
System.out.println(indentation + node.data);
// printing left subtree if not null, one tab additional spacing
before
// it
if (node.left != null) {
prettyPrint(node.left, indentation + "\t");
}
}
// Test.java (a class for testing the new class &
methods)
public class Test {
public static void main(String[] args) {
// creating a bst, adding some values, testing new methods
BetterBST<Integer> bst = new BetterBST<Integer>();
bst.insert(1);
bst.insert(3);
bst.insert(5);
bst.insert(10);
bst.insert(7);
bst.insert(9);
bst.insert(0);
System.out.println("smallestGreaterThan(2): "
+ bst.smallestGreaterThan(2));
System.out.println("smallestGreaterThan(7): "
+ bst.smallestGreaterThan(7));
System.out.println("smallestGreaterThan(13): "
+ bst.smallestGreaterThan(13));
System.out.println("Height: " + bst.height());
System.out.println("BST pretty printing:");
bst.prettyPrint();
BinarySearchTree<Integer> mirror= bst.mirror();
System.out.println("Mirror: ");
mirror.prettyPrint();
}
/*OUTPUT*/
8 Comment
Was this answer helpful? 1
Questions viewed by other students
Problem 2 - Binary Search Tree (BST) Algorithms - 30 points
(JAVA)
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...
See answer 100% (4 ratings)
*****************************In
Java***************************************In
Java***********************************In
Java*************************
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...
See answer
Show more
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.