Java.
ALL THE CHARACTERISTICS CONCERNING THE ENTRANCE REPRESENTATIVE IS AN ANIMATION OF THE BINARY TREE AND ITS MIRROR IMAGE. IT IS ALSO SPECIALLY WATCHED WITH ANIMATION. IMAGINE ME IF YOU HAVE ANY MALFUNCTION UNDERSTANDING THE CODE.
package classassignmentalgo;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
public class ClassAssignmentAlgo extends JFrame implements ActionListener{
JPanel panel1 = new JPanel(new GridLayout(0,2));
JPanel panel2 = new JPanel(new GridLayout(0,2));
JTextField txtBox = new JTextField();
static JLabel lbl1 = new JLabel();
static JLabel lbl2 = new JLabel();
static JLabel lbl3 = new JLabel();
static JLabel lbl4 = new JLabel();
static JLabel lbl5 = new JLabel();
static JLabel lblExtra = new JLabel();
static String preorder = new String();
static String inorder = new String();
static String postorder = new String();
JButton btnTree = new JButton("Tree");
JButton btnPreorder = new JButton(("Preorder"));
JButton btnInorder = new JButton("Inorder");
JButton btnPostorder = new JButton("postorder");
JButton btnLeafNodes = new JButton("Leaf Nodes");
JButton btnNonLeaf = new JButton("Non- leaf nodes");
JButton btnMirror = new JButton("Mirror");
JButton btnSearch = new JButton("Search");
JTextField txtSearch = new JTextField();
static int[][] coordinateArray;
public ClassAssignmentAlgo(){
panel2.add(btnTree);
panel2.add(txtBox);
panel2.add(btnPreorder);
panel2.add(lbl1);
panel2.add(btnInorder);
panel2.add(lbl2);
panel2.add(btnPostorder);
panel2.add(lbl3);
panel2.add(btnLeafNodes);
panel2.add(lbl4);
panel2.add(btnNonLeaf);
panel2.add(lbl5);
panel2.add(btnMirror);
panel2.add(lblExtra);
panel2.add(btnSearch);
panel2.add(txtSearch);
getContentPane().setLayout(new GridLayout(2,0));
add(panel1);
add(panel2);
btnTree.addActionListener(this);
btnPreorder.addActionListener(this);
btnInorder.addActionListener(this);
btnPostorder.addActionListener(this);
btnLeafNodes.addActionListener(this);
btnNonLeaf.addActionListener(this);
btnMirror.addActionListener(this);
btnSearch.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new ClassAssignmentAlgo();
frame.setSize(900,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btnTree){
String input = txtBox.getText();
insertInBtree(input,8);
}
if(e.getSource() == btnPreorder){
String input = txtBox.getText();
insertInBtree(input,0);
}
if(e.getSource() == btnPostorder){
String input = txtBox.getText();
insertInBtree(input,2);
}
if(e.getSource() == btnInorder){
String input = txtBox.getText();
insertInBtree(input,1);
}
if(e.getSource() == btnLeafNodes){
String input = txtBox.getText();
insertInBtree(input,4);
}
if(e.getSource() == btnNonLeaf){
String input = txtBox.getText();
insertInBtree(input,5);
}
if(e.getSource() == btnMirror){
String input = txtBox.getText();
insertInBtree(input,3);
}
if(e.getSource() == btnSearch){
String input = txtBox.getText();
insertInBtree(input,6);
}
}
public void insertInBtree(String a,int b){
BTree instance = new BTree();
instance.imp(a,b);
}
public class BTree {
BTree root = null;
BTree temp1;
BTree temp2;
int data;
BTree right,left;
int[] array = new int[20];
public BTree(){
right = null;
left = null;
data = 0;
}
public BTree(int n){
data = n;
right = null;
left = null;
}
String print = new String();
void imp(String received,int a) {
try{
String[] array = received.split(",");
coordinateArray = new int[array.length][2];
for(int i=0;i<array.length;i++){
InBTree(Integer.parseInt(array[i]));
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Wrong Input");
System.exit(0);
}
temp1 = root;
temp2 = root;
switch(a){
case 0 : preorderOfBtree(root);
break;
case 1 : inorderOfBtree(root);
break;
case 2 : postorderOfBtree(root);
break;
case 3 :mirrorImageOfBtree(temp2);
printMirror(temp2);
break;
case 4: leafNodesOfBtree(root);
break;
case 5 : nonleafNodesOfBtree(root);
break;
case 6 : searchInBtree(root);
break;
case 8 : drawingBtree(root,180);
break;
default:
break;
}
}
int a = 40;
int radiusOfNode = 20;
int gapHeight = 50;
void searchInBtree(BTree node){
Graphics g = getGraphics();
displayInPanel(g,node,180,80,80);
if(flag == 0){
JOptionPane.showMessageDialog(null,"Element not found");
}
}
void drawingBtree(BTree node,int a){
drawBtree(node,a);
}
void drawBtree(BTree node,int startingPoint){
Graphics g = getGraphics();
if(node != null){
displayTreeInorderMirror(g,node,startingPoint,80,80);
}
}
int flag = 0;
void displayInPanel(Graphics f, BTree node,int xCoordinates,int yCoordinates,int gapWidth){
String s = txtSearch.getText();
int sInt = Integer.parseInt(s);
if(sInt == node.data){
f.setColor(Color.green);
flag = 1;
}
else{
if(flag ==0){
f.fillOval(xCoordinates-radiusOfNode, yCoordinates-radiusOfNode, 2*radiusOfNode, 2*radiusOfNode);
f.setColor(Color.red);
}
else{
f.drawOval(xCoordinates-radiusOfNode, yCoordinates-radiusOfNode, 2*radiusOfNode, 2*radiusOfNode);
f.setColor(Color.lightGray);
}
}
f.drawString(String.valueOf(node.data), xCoordinates-6, yCoordinates+4);
f.setColor(Color.black);
if(node.left!=null){
drawLeftLine(f,xCoordinates-gapWidth,yCoordinates+gapHeight,xCoordinates,yCoordinates);
displayInPanel(f,node.left,xCoordinates-gapWidth,yCoordinates+gapHeight,gapWidth/2);
}
if(node.right!=null){
drawRightLine(f,xCoordinates+gapWidth,yCoordinates+gapHeight,xCoordinates,yCoordinates);
displayInPanel(f,node.right,xCoordinates+gapWidth,yCoordinates+gapHeight,gapWidth/2);
}
}
void printMirror(BTree temporary){
drawingBtree(temporary,650);
}
void displayTreeInorderMirror(Graphics f, BTree node,int xCoordinates,int yCoordinates,int gapWidth){
f.fillOval(xCoordinates-radiusOfNode, yCoordinates-radiusOfNode, 2*radiusOfNode, 2*radiusOfNode);
f.setColor(Color.LIGHT_GRAY);
f.drawString(String.valueOf(node.data), xCoordinates-6, yCoordinates+4);
f.setColor(Color.BLACK);
if(node.left!=null){
drawLeftLine(f,xCoordinates-gapWidth,yCoordinates+gapHeight,xCoordinates,yCoordinates);
displayTreeInorderMirror(f,node.left,xCoordinates-gapWidth,yCoordinates+gapHeight,gapWidth/2);
}
if(node.right!=null){
drawRightLine(f,xCoordinates+gapWidth,yCoordinates+gapHeight,xCoordinates,yCoordinates);
displayTreeInorderMirror(f,node.right,xCoordinates+gapWidth,yCoordinates+gapHeight,gapWidth/2);
}
}
void drawLeftLine(Graphics f,int coordinateX,int coordinateY,int coordinateX2,int coordinateY2){
double d = Math.sqrt(gapHeight*gapHeight+(coordinateX2-coordinateX)*(coordinateX2-coordinateX));
int startingX = (int)(coordinateX+radiusOfNode*(coordinateX2-coordinateX)/d);
int startingY = (int)(coordinateY-radiusOfNode * gapHeight/d);
int endingX = (int)(coordinateX2 - radiusOfNode * (coordinateX2-coordinateX)/d);
int endingY = (int)(coordinateY2+radiusOfNode * gapHeight /d);
try {
Thread.sleep(600);
} catch (InterruptedException ex) {
}
f.drawLine(startingX,startingY,endingX,endingY);
}
void drawRightLine(Graphics f,int coordinateX,int coordinateY,int coordinateX2,int coordinateY2){
double d = Math.sqrt(gapHeight*gapHeight+(coordinateX2-coordinateX)*(coordinateX2-coordinateX));
int startingX = (int)(coordinateX-radiusOfNode*(coordinateX-coordinateX2)/d);
int startingY = (int)(coordinateY-radiusOfNode * gapHeight/d);
int endingX = (int)(coordinateX2 - radiusOfNode * (coordinateX-coordinateX2)/d);
int endingY = (int)(coordinateY2+radiusOfNode * gapHeight /d);
try {
Thread.sleep(600);
} catch (InterruptedException ex) {
}
f.drawLine(startingX,startingY,endingX+5,endingY-8);
}
void InBTree(int n){
root = insertInBtree(root,n);
}
BTree insertInBtree(BTree node,int da){
if(node == null ){
node = new BTree(da);
}
else{
if(node.data > da){
node.left = insertInBtree(node.left,da);
}
else{
node.right = insertInBtree(node.right,da);
}
}
return node;
}
void preorderOfBtree(BTree r)
{
if (r != null)
{
print += r.data +" ";
preorderOfBtree(r.left);
preorderOfBtree(r.right);
}
lbl1.setText(print);
}
void leafNodesOfBtree(BTree r){
if(r == null){
return;
}
if(r.left!=null || r.right!=null){
leafNodesOfBtree(r.left);
leafNodesOfBtree(r.right);
}
else{
print += r.data+" ";
}
lbl4.setText(print);
}
void nonleafNodesOfBtree(BTree r){
if(r==null){
return;
}
if(r.left ==null && r.right == null){
return;
}
else{
nonleafNodesOfBtree(r.left);
nonleafNodesOfBtree(r.right);
print += r.data+" ";
}
lbl5.setText(print);
}
void postorderOfBtree(BTree r){
if(r!=null){
postorderOfBtree(r.left);
postorderOfBtree(r.right);
print += r.data+" ";
}
lbl3.setText(print);
}
void inorderOfBtree(BTree r){
if(r != null){
inorderOfBtree(r.left);
print += r.data+" ";
inorderOfBtree(r.right);
}
lbl2.setText(print);
}
BTree mirrorImageOfBtree(BTree n){
if(n == null){
return null;
}
else{
BTree tree = new BTree();
mirrorImageOfBtree(n.left);
mirrorImageOfBtree(n.right);
tree = n.left;
n.left = n.right;
n.right = tree;
}
return n;
}
}
}