搜索二叉树的应用

来源:互联网 发布:linux ssh开启 编辑:程序博客网 时间:2024/05/19 17:24

一个简单的在线租赁程序,利用文本文件作为其后台数据库。

搜索树充当临时数据库,借助搜索树的高效性,租借功能直接对数进行操作,最后在关闭窗口时再迭代的将树中每个节点存储的内容复制到文本文件里。


界面:


左侧为仍然拥有的东西以及每个东西的数量,右侧是已经借出的东西,这些东西从上至下按字母顺序排列,Tool文本框输入你想要借的东西,

底端两个按钮分别负责借和还,底端右侧文本框用来显示操作状态。


当输入java后点击rent按钮,效果如上图所示。


输入php点击rent若干次,如上图


输入java点击return效果如上图


假如php被借完了,再次输入php点击rent则显示已经没有php了


附上源代码:

package demo;public class Tool implements Comparable{private String toolName;private int toolCount;public boolean stats=false;//tool对象状态,0为移除,1为添加public Tool(String name){toolName=name;toolCount=1;}public String getName(){return toolName;}public int getCount(){return toolCount;}public String toString(){return toolName+"("+toolCount+")";}public void updateCount(){if(stats)toolCount++;elsetoolCount--;}public int compareTo(Object tool) {int orderValue=toolName.compareTo(((Tool)tool).getName());if(orderValue==0)return 0;else if(orderValue>0)return 1;elsereturn -1;}}

package demo;import java.util.Collection;import java.util.Iterator;import demo.STNode;public  class Stree<T> implements Collection<T>{public STNode root;int treeSize;int modCount;public Stree(){root=null;treeSize=0;modCount=0;}private class IteratorImp1 implements Iterator{STNode nextNode=null;STNode lastReturned=null;private int expectedModCount=modCount;public IteratorImp1(){nextNode=root;if(nextNode!=null)while(nextNode.left!=null)nextNode=nextNode.left;}public boolean hasNext() {if(nextNode==null)return false;elsereturn true;}public Object next() {if(nextNode==null)return null;lastReturned=nextNode;STNode p;if(nextNode.right!=null){nextNode=nextNode.right;while(nextNode.left!=null)nextNode=nextNode.left;}else{p=nextNode.parent;while(p!=null&&p.right==nextNode){nextNode=p;p=p.parent;}nextNode=p;}return lastReturned.tool;}public void remove() {}}public int size() {return treeSize;}public boolean isEmpty() {if(treeSize==0){return true;}else{return false;}}public boolean contains(Object o) {// TODO Auto-generated method stubreturn false;}public Iterator<T> iterator() {return new IteratorImp1();}@Overridepublic Object[] toArray() {// TODO Auto-generated method stubreturn null;}@Overridepublic <T> T[] toArray(T[] a) {// TODO Auto-generated method stubreturn null;}public boolean add(T tool) {STNode t=root,parent=null,newNode;int orderValue=0;while(t!=null){parent=t;orderValue=((Comparable)tool).compareTo(t.tool);if(orderValue==0){t.tool.stats=true;t.tool.updateCount();return true;}else if(orderValue>0){t=t.right;}elset=t.left;}newNode=new STNode((Tool)tool,parent);if(parent==null)root=newNode;else if(orderValue>0)parent.right=newNode;elseparent.left=newNode;treeSize++;modCount++;return true;}private void removeNode(STNode dNode){if(dNode==null){return;}STNode rNode,pNode;int orderValue=0;pNode=dNode.parent;if(dNode.left==null||dNode.right==null){if(dNode.left==null)rNode=dNode.right;elserNode=dNode.left;if(rNode!=null)rNode.parent=pNode;if(pNode==null)root=rNode;else if(((Comparable)dNode.tool).compareTo(pNode.tool)>0)pNode.right=rNode;elsepNode.left=rNode;}else{STNode pOfRNode=dNode;rNode=dNode.right;while(rNode.left!=null){pOfRNode=rNode;rNode=rNode.left;}dNode.tool=rNode.tool;if(pOfRNode==dNode)pOfRNode.right=rNode.right;elsepOfRNode.left=rNode.right;if(rNode.right!=null)rNode.right.parent=pNode;}}public Tool find(Tool tool){STNode t=findNode(tool);Tool value=null;if(t!=null)value=t.tool;return value;}private STNode findNode(Tool tool){STNode t=root;int orderValue=0;if(t==null)return null;while(t!=null){orderValue=((Comparable)tool).compareTo(t.tool);if(orderValue==0)return t;else if(orderValue>0)t=t.right;elset=t.left;}return null;} public boolean remove(Object tool) {STNode dNode=findNode((Tool)tool);if(dNode!=null&&dNode.tool.getCount()==1){removeNode(dNode);treeSize--;modCount++;return true;}else if(dNode==null){return false;}else{dNode.tool.stats=false;dNode.tool.updateCount();return true;}}@Overridepublic boolean containsAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean addAll(Collection<? extends T> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean removeAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean retainAll(Collection<?> c) {// TODO Auto-generated method stubreturn false;}public void clear() {}}

package demo;import demo.STNode;public class STNode{public Tool tool;public STNode left;public STNode right;public STNode parent;public STNode(Tool item,STNode parentValue){left=null;right=null;parent=parentValue;tool=item;}}

package demo;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;import java.util.Scanner;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import java.util.Iterator;public class View extends JFrame implements ActionListener{public static final int HEIGHT=300;public static final int WIDTH=400;private JTextField inputToolField;private JTextArea tools;private JTextArea rentals;private JButton rentButton;private JButton returnButton;private JTextField showInformationField;private Stree toolsTree;private Stree rentalsTree;private PrintWriter outputStream=null;private Scanner inputStream=null;public View(){setTitle("Rent and Return");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(WIDTH, HEIGHT);this.setLocation(430, 100);setLayout(new BorderLayout());JPanel inputPanel=new JPanel();inputPanel.setLayout(new FlowLayout());JLabel toolLabel=new JLabel("Tool");inputToolField=new JTextField(20);inputPanel.add(toolLabel);inputPanel.add(inputToolField);JPanel showPanel=new JPanel();showPanel.setLayout(new GridLayout(1,2));tools=new JTextArea(5,10);JScrollPane scrollpane=new JScrollPane(tools);scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);rentals=new JTextArea(5,10);showPanel.add(scrollpane);JScrollPane scrollpaneRentals=new JScrollPane(rentals);scrollpaneRentals.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);scrollpaneRentals.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);showPanel.add(scrollpaneRentals);JPanel operationPanel=new JPanel();rentButton=new JButton("Rent");rentButton.addActionListener(this);returnButton=new JButton("Return");returnButton.addActionListener(this);showInformationField=new JTextField(20);operationPanel.add(rentButton);operationPanel.add(returnButton);operationPanel.add(showInformationField);add(inputPanel,BorderLayout.NORTH);add(showPanel,BorderLayout.CENTER);add(operationPanel,BorderLayout.SOUTH);this.addWindowListener(new WindowsCloseListener());toolsTree=new Stree();rentalsTree=new Stree();}private class WindowsCloseListener extends WindowAdapter{public void windowClosing(WindowEvent e){try {PrintWriter outputStream=new PrintWriter("tools.txt");Iterator it=toolsTree.iterator();String content="";while(it.hasNext()){Tool tool=(Tool)it.next();while(toolsTree.remove(tool)){content+=tool.getName()+"\n";toolsTree.remove(tool);}}outputStream.println(content);outputStream.close();outputStream=new PrintWriter("rentals.txt");it=rentalsTree.iterator();content="";while(it.hasNext()){Tool tool=(Tool)it.next();while(rentalsTree.remove(tool)){content+=tool.getName()+"\n";rentalsTree.remove(tool);}}outputStream.println(content);outputStream.close();} catch (FileNotFoundException e1) {e1.printStackTrace();}}}public static void main(String[]args){View view=new View();view.setVisible(true);view.setupInventory(view.toolsTree);}public void setupInventory(Stree t) {try {inputStream=new Scanner(new File("tools.txt"));while(inputStream.hasNext()){Tool tool=new Tool(inputStream.next());t.add(tool);}inputStream.close();Iterator it=t.iterator();String content="";while(it.hasNext()){content+=it.next()+"\n";}tools.setText(content);inputStream=new Scanner(new File("rentals.txt"));while(inputStream.hasNext()){Tool tool=new Tool(inputStream.next());rentalsTree.add(tool);}inputStream.close();it=rentalsTree.iterator();content="";while(it.hasNext()){content+=it.next()+"\n";}rentals.setText(content);} catch (FileNotFoundException e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {String command=e.getActionCommand();Tool tool;if(command.equals("Rent")){String rentItem=inputToolField.getText();tool=new Tool(rentItem);if(toolsTree.remove(tool)){Iterator toolsit=toolsTree.iterator();String content="";while(toolsit.hasNext()){content+=toolsit.next().toString()+"\n";}tools.setText(content);rentalsTree.add(tool);showInformationField.setText("RENTED : "+tool);}else{showInformationField.setText("OUT OF STOCK : "+tool);}Iterator rentalsit=rentalsTree.iterator();String content="";while(rentalsit.hasNext()){content+=rentalsit.next().toString()+"\n";}rentals.setText(content);}else if(command.equals("Return")){String returnItem=inputToolField.getText();tool=new Tool(returnItem);if(rentalsTree.remove(tool)){toolsTree.add(tool);showInformationField.setText("RETURNED : "+tool);}else{showInformationField.setText("OUT OF STOCK : "+tool);}Iterator toolsit=toolsTree.iterator();Iterator rentalsit=rentalsTree.iterator();String content="";while(rentalsit.hasNext()){content+=rentalsit.next().toString()+"\n";}rentals.setText(content);content="";while(toolsit.hasNext()){content+=toolsit.next().toString()+"\n";}tools.setText(content);}}}


原创粉丝点击