This applet can be used to find chemicals that appear in chemical compounds. Enter a chemical in the "Enter Chemical" text field and then click the "Add Chemical" button. Do this for as many chemicals as you like. Examples might be Silicon, Calcium, Sodium, and Carbon. As each chemical is entered, it is put into a binary tree.

Type in an appropriate file name into the "Enter Chemical Compound Filename" text field. The file chemicals.txt is a good one.

Clicking the "Display Results" button shows how often the selected chemicals appear in the Chemical Compound file. Clicking the "Reverse Tree" button reverses the binary tree. Clicking the "Breadth First" button displays each level of the binary tree from top to bottom.

When you are all done, click the "Exit" button.


Compounds.java demonstrates file I/O

// class Compounds was written by a CS 181 Recitation Instructor at // Purdue University several years ago // Author: unknown // Adapted by: Prof. Dunsmore // Date: July, 2004 import java.awt.*; import java.awt.event.*; import java.io.*; import java.applet.Applet; import java.net.*; import java.text.DecimalFormat; public class Compounds extends Applet implements ActionListener { private Button addSearchString; private Button Done; private TextField stringField; private TextArea displayStrings; private TextField fileName; public TextArea displayResults; private Button Display; private Tree tree; private Panel p1,p2,p3,p4,p5,p6; Label label1,label2; private Button ReverseTree; private Button LinkedList; private String InputFilename; private String inputStream; private TextField message; private int mirror; public void init() { tree=new Tree(); setSize(600,650); p1=new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); label1=new Label("Enter Chemical"); stringField=new TextField(40); p1.add(label1); p1.add(stringField); p2=new Panel(); p2.setLayout(new GridLayout(1,2,5,5)); addSearchString=new Button("Add Chemical"); addSearchString.addActionListener(this); Done=new Button("Exit"); Done.addActionListener(this); p2.add(addSearchString); p2.add(Done); p3=new Panel(); p3.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); displayStrings=new TextArea(5,30); displayStrings.setEditable(false); p3.add(displayStrings); p3.add(p2); p4=new Panel(); label2=new Label("Enter Chemical Compound Filename"); p4.setLayout(new FlowLayout(FlowLayout.LEFT)); fileName=new TextField(40); p4.add(label2); p4.add(fileName); p5=new Panel(); p5.setLayout(new GridLayout(3,1)); Display=new Button("Display Results"); Display.addActionListener(this); ReverseTree=new Button("Reverse Tree"); ReverseTree.addActionListener(this); LinkedList=new Button("Breadth First"); LinkedList.addActionListener(this); p5.add(Display); p5.add(ReverseTree); p5.add(LinkedList); p6=new Panel(); p6.setLayout(new FlowLayout(FlowLayout.LEFT,10,10)); displayResults=new TextArea(5,30); displayResults.setEditable(false); message=new TextField(15); message.setEditable(false); p6.add(message); p6.add(p5); p6.add(displayResults); setLayout(new GridLayout(4,1,0,0)); add(p1); add(p3); add(p4); add(p6); mirror=1; setVisible(true); } public void actionPerformed (ActionEvent e) { if(e.getSource()==Done) { System.exit(0); } else if(e.getSource()==addSearchString) { addString(); stringField.setText(""); } else if(e.getSource()==Display){ displayResults.setText(""); showResults(); } else if(e.getSource()==ReverseTree){ mirror++; displayResults.setText(""); reverse (); if(mirror%2 ==0) message.setText("Mirror Image"); else message.setText(""); } else if(e.getSource()==LinkedList) { displayResults.setText(""); linkedList(); } } public void addString() { tree.insertNode(stringField.getText()); displayStrings.append(stringField.getText()+"\n"); } public void showResults() { tree.init(); InputFilename=fileName.getText(); int exc=0; try { URL file1 = new URL(getDocumentBase(), InputFilename); BufferedInputStream input=new BufferedInputStream (file1.openStream()); // Read in the values from this file . BufferedReader r=new BufferedReader(new InputStreamReader(input)); StreamTokenizer token=new StreamTokenizer(r); displayResults.setText(""); while(token.nextToken()!=token.TT_EOF) { tree.insertCount(token.sval); } } catch(IOException e) { exc=1; displayResults.setText(" File cannot be opened \n"); } if(exc!=1) { displayResults.append(" The count of the tokens is \n"); tree.displayCount(this); } } public void closeFile() { // try { // input.close(); // } // catch(IOException e) { // displayResults.setText(" Error in closing File \n"); // } } public void reverse() { tree.reverseTree(this); } public void linkedList() { tree.breadthFirst(this); } public static void main(String args[]) { new Compounds(); } public class Tree { private TreeNode root; public Tree() { root=null; } public synchronized void insertNode ( String d ) { if (root==null) root=new TreeNode(d); else root.insert(d); } public void inorderTraversal() { inorderHelper(root); } private void inorderHelper ( TreeNode node) { if(node==null) return; inorderHelper(node.left); inorderHelper(node.right); } public void insertCount(String presentToken) { helpCount(root,presentToken); } private void helpCount(TreeNode node,String token ) { if( node==null) return; if(token==null) return; if((node.data).compareTo(token)==0) { node.count++; return; } else if((node.data).compareTo(token)<0) helpCount(node.right,token); else helpCount(node.left,token); } public void displayCount(Compounds f) { helpDisplay(root,f); } private void helpDisplay(TreeNode node,Compounds f) { if(node==null) return; helpDisplay(node.left,f); f.displayResults.append(node.data + " " + (node.count)+ "\n"); helpDisplay(node.right,f); } public void reverseTree(Compounds f) { helpReverse(root,f); (f.displayResults).setText(""); (f.displayResults).append("The Tree has been reversed \n"); (f.displayResults).append("The inorder traversal is \n"); helpDisplay(root,f); } private void helpReverse(TreeNode node, Compounds f) { TreeNode tmp; if(node==null) return; helpReverse(node.left,f); helpReverse(node.right,f); tmp=node.left; node.left=node.right; node.right=tmp; } public void init() { initTree(root); } private void initTree(TreeNode node) { if(node==null) return; initTree(node.left); node.count=0; initTree(node.right); } public void breadthFirst(Compounds f) { (f.displayResults).append(" The breadth first scan gives :\n"); helpBreadthFirst(root,f); } private void helpBreadthFirst(TreeNode node, Compounds f) { Queue myQueue=new Queue(); TreeNode o; if(node==null) return; myQueue.enqueue(node); while(!myQueue.isEmpty()){ o=myQueue.dequeue(); (f.displayResults).append( o.data + " " + o.count+"\n"); if(o.left!=null) myQueue.enqueue(o.left); if(o.right!=null) myQueue.enqueue(o.right); } } } class TreeNode { TreeNode left; String data; int count; TreeNode right; public TreeNode(String name) { data =name; count=0; left=right=null; } public synchronized void insert (String name) { if (name.compareTo(data) < 0) { if(left==null) left=new TreeNode(name); else left.insert(name); } else if(name.compareTo(data)> 0) { if(right==null) right=new TreeNode(name); else right.insert(name); } } } public class List { private ListNode firstNode; private ListNode lastNode; private String name; public List(String s) { name=s; firstNode=lastNode=null; } public List() { this("list"); } public synchronized void insertAtFront(TreeNode insertItem) { if(isEmpty()) firstNode=lastNode=new ListNode(insertItem); else firstNode=new ListNode(insertItem,firstNode); } public synchronized void insertAtBack(TreeNode insertItem) { if(isEmpty()) firstNode=lastNode=new ListNode(insertItem); else lastNode=lastNode.next=new ListNode(insertItem); } public synchronized TreeNode removeFromFront() throws EmptyListException{ TreeNode removeItem=null; if(isEmpty()) throw new EmptyListException(name); removeItem=firstNode.data; if(firstNode.equals(lastNode)) firstNode=lastNode=null; else firstNode=firstNode.next; return removeItem; } public synchronized TreeNode removeFromBack() throws EmptyListException{ TreeNode removeItem=null; if(isEmpty()) throw new EmptyListException(name); removeItem=lastNode.data; if(firstNode.equals(lastNode)) firstNode=lastNode=null; else { ListNode current=firstNode; while(current.next!= lastNode) current=current.next; lastNode=current; current.next=null; } return removeItem; } public boolean isEmpty() { return firstNode==null; } } class ListNode { TreeNode data; ListNode next; ListNode (TreeNode o) { this (o,null); } ListNode(TreeNode o,ListNode nextNode) { data =o; next=nextNode; } TreeNode getObject() { return data; } ListNode getnext() { return next; } } public class Queue extends List { public Queue() { super("queue"); } public synchronized void enqueue(TreeNode o) { insertAtBack(o); } public synchronized TreeNode dequeue() throws EmptyListException{ return removeFromFront(); } public boolean isEmpty() { return super.isEmpty(); } } public class EmptyListException extends RuntimeException { public EmptyListException (String name) { super(" this queue is empty \n"); } } }