In: Computer Science
INTERFACE (BinaryTreeInterface):
BinaryTreeInterface.java
This interface represents all the functionalities a generic binary
tree.
Operations
+ getRootData(): T - This method returns the data stored in the
root node of a tree. Note that you cannot return the data of an
empty tree.
+ getRootNode(): BinaryNode<T> - This method returns the
reference to the root node of a tree.
+ setRootNode(BinaryNode<T>):void - This method sets the root
node of a tree.
+ getHeight():int - This method returns the height of a tree.
+ getNumberOfNodes(): int - This method returns the number of nodes
in a tree.
+ isEmpty():boolean - This method returns true, if a tree is empty,
false otherwise.
+ clear():void – clears a tree.
CLASS (BinaryTree): BinaryTree.java
This generic class implements the BinaryTreeInterface. The class
has one attribute root, which represents the root node of a tree.
In addition, it has the following behaviors:
+BinaryTree(): constructor - initializes root to null.
+ BinaryTree(data): constructor - initializes the root node’s
data.
+ BinaryTree(data, leftTree, rightTree): constructor - initializes
the root node with the given values.
+ setTree(data, leftTree, rightTree): void- set the tree to the
root with data and the leftTree and rightTree subtrees. Make sure
that the new created tree has only one point of entry, which is the
root.
+inorderTraversal(): void – displays all the nodes in tree using
inorder traversal. It displays the content of the nodes separated
by a space in a single line. For example, if the nodes’ content are
1, 2, 3, and 4, the method displays “1 2 3 4”. We have to have a
non empty tree for traversals.
Please implement those interface and class today, thank you
/*** BinaryTreeInterface.java ***/
public interface BinaryTreeInterface<T>
{
//This method returns the data stored in the root node
of a tree.
//Note that you cannot return the data of an empty
tree.
public T getRootData();
//This method returns the reference to the root node
of a tree.
public BinaryNode<T> getRootNode();
//This method sets the root node of a tree.
public void setRootNode(BinaryNode<T>
root);
//This method returns the height of a tree.
public int getHeight();
//This method returns the number of nodes in a
tree.
public int getNumberOfNodes();
//This method returns true, if a tree is empty, false
otherwise.
public boolean isEmpty();
//clears a tree.
public void clear();
}
/*** BinaryNode.java ***/
public class BinaryNode<T>
{
T data;
BinaryNode<T> left;
BinaryNode<T> right;
public BinaryNode(T data)
{
this.data = data;
left = null;
right = null;
}
public BinaryNode(T data, BinaryNode<T> left,
BinaryNode<T> right)
{
this.data = data;
this.left = left;
this.right = right;
}
public T getData()
{
return data;
}
}
/*** BinaryTree.java ***/
public class BinaryTree<T> implements
BinaryTreeInterface<T>
{
private BinaryNode<T> root;
//constructor - initializes root to null.
public BinaryTree()
{
root = null;
}
//constructor - initializes the root node’s
data.
public BinaryTree(T data)
{
root = new
BinaryNode<>(data);
}
//constructor - initializes the root node with the
given values.
public BinaryTree(T data, BinaryNode<T>
leftTree, BinaryNode<T> rightTree)
{
root = new BinaryNode<>(data,
leftTree, rightTree);
}
//set the tree to the root with data and the leftTree
and rightTree subtrees.
//Make sure that the new created tree has only one
point of entry, which is the root.
public void setTree(T data, BinaryNode<T>
leftTree, BinaryNode<T> rightTree)
{
root = new BinaryNode<>(data,
leftTree, rightTree);
}
//Helper method to displays all the nodes in tree
using inorder traversal.
private void inorder(BinaryNode<T> btnode)
{
if(btnode!=null)
{
inorder(btnode.left);
System.out.print(btnode.getData() + " ");
inorder(btnode.right);
}
}
//displays all the nodes in tree using inorder
traversal.
public void inorderTraversal()
{
inorder(root);
}
//This method returns the data stored in the root node
of a tree.
//Note that you cannot return the data of an empty
tree.
public T getRootData()
{
return root.getData();
}
//This method returns the reference to the root node
of a tree.
public BinaryNode<T> getRootNode()
{
return root;
}
//This method sets the root node of a tree.
public void setRootNode(BinaryNode<T>
root)
{
this.root = root;
}
//Helper method to calculate height of the binary
tree
private int getHeight(BinaryNode<T>
btnode)
{
if(btnode==null)
return 0;
int hleft =
getHeight(btnode.left);
int hright =
getHeight(btnode.right);
int max = hleft>hright? hleft:
hright;
return max + 1;
}
//This method returns the height of a tree.
public int getHeight()
{
return getHeight(root);
}
//Helper method returns the number of nodes in a
tree.
private int getNumberOfNodes(BinaryNode<T>
btnode)
{
if(btnode==null)
return 0;
return 1 +
getNumberOfNodes(btnode.left) +
getNumberOfNodes(btnode.right);
}
//This method returns the number of nodes in a
tree.
public int getNumberOfNodes()
{
return
getNumberOfNodes(root);
}
//This method returns true, if a tree is empty, false
otherwise.
public boolean isEmpty()
{
return root==null;
}
//clears a tree.
public void clear()
{
root = null;
}
}
/*** BinaryTreeApp.java ***/
public class BinaryTreeApp
{
//main method
public static void main (String[] args)
{
BinaryNode<Integer> btnode =
new BinaryNode<>(5, new BinaryNode<>(4, null,
null),
new
BinaryNode<>(6, null, null));
BinaryTree<Integer> b = new
BinaryTree<>(2, new BinaryNode<>(1, null, null),
btnode);
b.inorderTraversal();
System.out.println ();
System.out.println ("Height: " +
b.getHeight());
System.out.println ("Number of
nodes: " + b.getNumberOfNodes());
System.out.println ("Is the tree
empty? " + b.isEmpty());
b.clear();
System.out.println ("Is the tree
empty? " + b.isEmpty());
}
}
Output:
1 2 4 5 6
Height: 3
Number of nodes: 5
Is the tree empty? false
Is the tree empty? true