Add the following methods to the class BinaryTree shown below:
public T get(int i)
public void add(int i, T element)
public T remove(int i)
All these methods use the inorder traversal of the tree. Feel free to use
helper methods and/or extra fields.
Here are the description of what the methods do.
---------------------------------------------------------------------------------
get(i) returns the element of the i-th node of the inorder traversal of the tree.
If i is outside the range it throws a NOSuchElementException.
For example if the inorder traversal of the tree t is
a, b, c, d, e, f, g, h, i, j, k
[login to view URL](4) returns e.
----------------------------------------------------------------------------------
public void add(int i, T element)
inserts element in the tree such that it is the i-th item in the inorder traversal.
Throw an index out of bounds exception if i is out of bounds and increase size
if the insertion is successful.
For example if the inorder traversal of the tree t is
a, b, c, d, e, f, g, h, i, j, k
after the call [login to view URL](5, x);
the inorder traversal of t is
a, b, c, d, e, x, f, g, h, i, j, k
-----------------------------------------------------------------------------------
public T remove(int i)
removes the i-th item of the tree.
Throw a no such element exception if i is out of range.
Don't forget to decrease the size if the remove is successful.
For example if the inorder traversal of the tree t is
a, b, c, d, e, f, g, h, i, j, k
the call [login to view URL](5);
returns f and the inorder traversal of t is
a, b, c, d, e, g, h, i, j, k
--------------------------------------------------------------------------------------
Here is the class [login to view URL]
import [login to view URL];
public class BinaryTree<T> {
/** Creates an empty binary tree */
public BinaryTree()
{
root = null;
}
// create a binary tree with a given root value
public BinaryTree( T rootItem)
{
root = new BinaryNode( rootItem, null, null);
size = 1;
}
// @return the root of the tree
public BinaryNode<T> getRoot()
{
return root;
}
// @return the size of the tree
public int size()
{
return size;
}
// @return the the height of the tree
public int height()
{
return [login to view URL](root);
}
// print the tree in preorder
public void printPreOrder()
{
if (root != null)
[login to view URL]();
}
// print the tree in postorder
public void printPostOrder()
{
if (root != null)
[login to view URL]();
}
// print the tree in inorder
public void printInOrder()
{
if (root != null)
[login to view URL]();
}
// empty the tree
public void makeEmpty()
{
root = null;
size = 0;
}
// check if the tree is empty
public boolean isEmpty()
{
return root == null;
}
// forms a new tree from rootItem t1 and t2
// does not allow t1 and t2 to be the same
public void merge(T rootItem, BinaryTree<T> t1, BinaryTree<T> t2)
{
if ([login to view URL] == [login to view URL] && [login to view URL] != null)
throw new IllegalArgumentException();
// allocate new node
root = new BinaryNode<T>(rootItem, [login to view URL], [login to view URL]);
size = [login to view URL]() + [login to view URL]() + 1;
// ensures that every node is in one tree
if (this != t1)
[login to view URL] = null;
if ( this != t2)
[login to view URL] = null;
}
private BinaryNode<T> root;
private int size = 0;
}