package shaukat;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class sample extends JFrame{
JFrame f=new JFrame();
JFrame f2=new JFrame();
JTextField txt=new JTextField();
private TreeNode root;
JTextArea result=new JTextArea();;
JScrollPane scrolltxt = new JScrollPane(result);
JButton b1,b2,b3;
String Str="";
String duplicate="";
String processString="";
int cl=0;
int cr=0;
public sample()
{
f.setSize(800, 300);
f2.setSize(1200, 600);
f2.setBackground(Color.WHITE);
f.setResizable(false);
f2.setLocation(150, 100);
f2.setVisible(false);
f.setTitle("Removal of Duplicate words From Given Sentence Using BST");
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setResizable(false);
f.setLocation(200, 50);
f.setLayout(null);
txt.setBounds(10, 10, 780, 30);
b1=new JButton("PROCESS");
b1.setBounds(10, 50, 100, 30);
b2=new JButton("SPEAK");
b2.setBounds(350, 50, 100, 30);
b3=new JButton("DISPLAY");
b3.setBounds(690, 50, 100, 30);
scrolltxt.setBounds(10, 90, 780, 180);
result.setEditable(false);
f.add(txt);
f.add(b1);
f.add(b2);
f.add(b3);
f.add(scrolltxt);
speak("THIS APPLICATION DELETES , ,duplicate words FROM , ,ABOVE sentences, ,USING BINARY SEARCH TREE");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process();
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
speak(txt.getText());
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f2.setContentPane(new backImage(root));
levelorderTraversal();
result.append("\n");
inorderTraversal();
result.append("\n");
result.append("\n------------------DUPLICATE ELEMENTS-------------------\n");
result.append(duplicate);
result.append("\n------------------PROCESS SENTENCES-------------------\n");
result.append(processString);
f2.setVisible(true);
}
});
f.validate();
}
//----------------------------------------------------------------------------------
//process
public void process()
{
Str=txt.getText();
Str=Str+" ";
String temp="";
char ch;
for(int i=0;i<Str.length();i++)
{
ch=Str.charAt(i);
if(ch==' ')
{
insert(temp);
temp="";
}else
{
temp=temp+ch;
}
}
}
//speak function
public void speak(String str){
Voice v;
VoiceManager vm=VoiceManager.getInstance();
v=vm.getVoice("kevin16");
v.allocate();
v.speak(str);
}
//INSERT
public void insert(String d) {
if (root == null) { // must handle case of empty tree first
root = new TreeNode(d);
root.xCo=600;
root.yCo=20;
root.pxCo=600;
root.pyCo=20;
root.level=0;
processString=processString+" "+root.data;
return;
}
TreeNode loc = root; // start search downward at root
while (true) {
if (d.compareTo(loc.data) < 0) { // look left
if (loc.left != null)
{
loc = loc.left;
}
else {
int x=loc.xCo;
int y=loc.yCo;
int templevel=loc.level;
loc.left = new TreeNode(d);
loc=loc.left;
loc.level=templevel+1;
loc.xCo=x-(200/loc.level);
loc.yCo=y+100-(10*loc.level);
loc.pxCo=x;
loc.pyCo=y;
processString=processString+" "+loc.data;
break;
}
}
else if (d.compareTo(loc.data) > 0) { // look right
if (loc.right != null) loc = loc.right;
else {
int x=loc.xCo;
int y=loc.yCo;
int templevel=loc.level;
loc.right = new TreeNode(d);
loc = loc.right;
loc.level=templevel+1;
loc.xCo=x+(200/loc.level);
loc.yCo=y+100-(10*loc.level);
loc.pxCo=x;
loc.pyCo=y;
processString=processString+" "+loc.data;
break;
}
}
else{
duplicate=duplicate+" "+loc.data;
break; // found! Don't insert duplicate
}
}
}
// levelorder
public void levelorderTraversal()
{
levelorderT(root);
}
private void levelorderT(TreeNode t){
TreeNode temp;
Queue<TreeNode> qe=new LinkedList<TreeNode>();
if(t==null)
{
return;
}else{
qe.add(t);
while(!qe.isEmpty())
{
temp=qe.poll();
//System.out.print("\t"+temp.data);
result.append("\t"+temp.data);
result.append(Integer.toString(temp.xCo)+Integer.toString(temp.yCo));
if(temp.left!=null)
{
qe.add(temp.left);
}
if(temp.right!=null)
{
qe.add(temp.right);
}
}
}
}
//inorder
// inorderTraversal: need because root is hidden
public void inorderTraversal() {inorderT(root); }
// inorderT: recursive function that does the work
private void inorderT(TreeNode t) {
if (t != null) {
inorderT(t.left);
//System.out.print(t.data + " ");
result.append("\t"+t.data);
inorderT(t.right);
}
}
//---------------------------------------------MAIN-------------------------------
public static void main(String args[])
{
sample obj=new sample();
}
}
//-----------------------------------------TREE NODE CLASS-------------------------------------
class TreeNode {
public String data; // data at each node
public TreeNode left, right;
public int xCo,yCo;
public int pxCo,pyCo;
public int level;
public TreeNode(String d) { // construct leaf node
data = d;
left = right = null;
}
}
//-----------------------------------------PAINT CLASS-----------------------------------------
//-----------------
class backImage extends JPanel {
Graphics G;
String str;
private TreeNode t;
//Creating Constructer
public backImage(TreeNode root) {
t=root;
}
//Overriding the paintComponent method
@Override
public void paintComponent(Graphics g) {
G=g;
TreeNode temp;
Queue<TreeNode> qe1=new LinkedList<TreeNode>();
if(t==null)
{
return;
}else{
qe1.add(t);
while(!qe1.isEmpty())
{
temp=qe1.poll();
//System.out.print("\t"+temp.data);
// result.append("\t"+temp.data);
//result.append(Integer.toString(temp.xCo)+Integer.toString(temp.yCo));
g.setColor(Color.black);
g.drawString(temp.data, temp.xCo, temp.yCo);
g.setColor(Color.RED);
g.drawLine(temp.pxCo, temp.pyCo, temp.xCo, temp.yCo);
if(temp.left!=null)
{
qe1.add(temp.left);
}
if(temp.right!=null)
{
qe1.add(temp.right);
}
}
}
}
}
Monday, 13 October 2014
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment