Implement the class shown on the following slides. You can only use Java to solv
ID: 3751465 • Letter: I
Question
Implement the class shown on the following slides. You can only use Java to solve this problem. You cannot use other language to do this work.
You need to implement BinaryTree() , BinaryTree(String d) ,BinaryTree(BinaryTree b1, String d, BinaryTree b2) ,BinaryTree(String t, String open, String close, String empty) , InorderIterator implements Iterator, PostorderIterator implements Iterator, Iterator inorder(), Iterator postorder() , String toString()
public class BinaryTree {
//Implements a Binary Tree of Strings
private class Node {
private Node left;
private String data;
private Node right;
private Node parent; // reference to the parent node
// the parent is null for the root node
private Node(Node L, String d, Node r, Node p) {
left = L;
data = d;
right = r;
parent = p;
}
}
private Node root;
public BinaryTree() {
// create an empty tree
}
public BinaryTree(String d) {
// create a tree with a single node
}
public BinaryTree(BinaryTree b1, String d, BinaryTree b2) {
// merge the trees b1 AND b2 with a common root with data d
}
public BinaryTree(String t, String open, String close, String empty) {
/*
* create a binary tree from the in order format discussed in class. Assume t is
* a syntactically correct string representation of the tree. Open and close are
* the strings which represent the beginning and end markers of a tree. Empty
* represents an empty tree. The example in class used ( ) and ! for open, close
* and empty respectively. The data in the tree will not include strings
* matching open, close or empty. All tokens (data, open, close and empty) will
* be separated By white space Most of the work should be done in a private
* recursive method
*/
}
public class InorderIterator implements Iterator {
// An iterator that returns data in the tree in an in order pattern
// the implementation must use the parent pointer and must not use an
// additional data structure
public InorderIterator() {
}
public boolean hasNext() {
}
public String next() {
}
public void remove() {
// optional method not implemented
}
}
public class PostorderIterator implements Iterator {
// An iterator that returns data in the tree in a post order pattern
// This implementation must use a stack and must not use the parent pointer
// You must use Java’s stack class
public PostorderIterator() {
}
public boolean hasNext() {
}
public String next() {
}
public void remove() {
// optional method not implemented
}
}
public Iterator inorder() {
// return a new in order iterator object
}
public Iterator postorder() {
// return a new post order iterator object
}
public String toString() {
// returns the string representation of the tree using the in order format
// discussed in class. If the tree was created from a string use the
// the values of open, close and empty given to the constructor otherwise
// use (, ) and ! for open, close and empty respectively
// most of the work should be done in a recursive private method.
}
Stri open The tee wil beExplanation / Answer
This is a Java Program to actualize Binary Tree. A twofold tree is a tree information structure in which every hub has at most two youngster hubs, generally recognized as "left" and "right". Hubs with youngsters are parent hubs, and tyke hubs may contain references to their folks. Outside the tree, there is regularly a reference to the "root" hub (the predecessor all things considered), on the off chance that it exists. Any hub in the information structure can be come to by beginning at root hub and more than once following references to either the left or right youngster. A tree which does not have any hub other than root hub is known as an invalid tree. In a double tree, a level of each hub is greatest two. A tree with n hubs has precisely n1 branches or degree.
Parallel trees are utilized to actualize double pursuit trees and paired stacks, discovering applications in effective looking and arranging calculations.
Here is the source code of the Java program to actualize Binary Tree.
import java.util.Scanner;
class BinaryTree_Node
{
BinaryTree_Node L, R;
int info;
public BinaryTree_Node()
{
L = null;
R = null;
info = 0;
}
public BinaryTree_Node(int n)
{
L = null;
R = null;
info = n;
}
public void setLeft(BinaryTree_Node n)
{
L = n;
}
public void setRight(BinaryTree_Node n)
{
R = n;
}
public BinaryTree_Node getLeft()
{
return L;
}
public BinaryTree_Node getRight()
{
return R;
}
public void setData(int d)
{
info = d;
}
public int getData()
{
return info;
}
}
class BinaryTree
{
private BinaryTree_Node rt;
public BinaryTree()
{
rt = null;
}
public boolean isEmpty()
{
return rt == null;
}
public void insert(int info)
{
rt = insert(rt, info);
}
private BinaryTree_Node insert(BinaryTree_Node nodee, int info)
{
if (nodee == null)
nodee = new BinaryTree_Node(info);
else
{
if (nodee.getRight() == null)
nodee.R = insert(nodee.R, info);
else
nodee.L = insert(nodee.L, info);
}
return nodee;
}
public int count_Node()
{
return count_Node(rt);
}
private int count_Node(BinaryTree_Node r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += count_Node(r.getLeft());
l += count_Node(r.getRight());
return l;
}
}
public boolean search(int val)
{
return search(rt, val);
}
private boolean search(BinaryTree_Node r, int val)
{
if (r.getData() == val)
return true;
if (r.getLeft() != null)
if (search(r.getLeft(), val))
return true;
if (r.getRight() != null)
if (search(r.getRight(), val))
return true;
return false;
}
public void postorder()
{
postorder(rt);
}
private void postorder(BinaryTree_Node r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
public class BinaryTree
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
BinaryTree BinaryTree = new BinaryTree();
System.out.println("Binary Tree Test ");
char choice;
do
{
System.out.println(" Binary Tree Operations ");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
BinaryTree.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to search");
System.out.println("Search result : "+ BinaryTree.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ BinaryTree.count_Node());
break;
case 4 :
System.out.println("Empty status = "+ BinaryTree.isEmpty());
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.print(" Post order : ");
BinaryTree.postorder();
System.out.println(" Do you want to continue (Type y or n) ");
choice = scan.next().charAt(0);
} while (choice == 'Y'|| choice == 'y');
}
}