/** * carTree.java * * @author Jasper Showers * * This is the node class with the car make as the "key" * * Made by Jasper Showers * for Judith Gurka * CSI 2050 * April 8, 2007 */ import java.util.*; //for tokenizer import java.io.*; //for filewriter public class carTree { carNode root; carNode current; ArrayList temp = new ArrayList(); boolean set = false; public carTree() { } /** Will take the input text and put it in the right place in our tree * @param carInfo The information to be added */ public void addCar(String carInfo) { boolean set = false; if (root == null) { //if this is the first car put in root = new carNode(); fillInInfo(root, carInfo); current = root; } else { while (!set) { if (carInfo.compareToIgnoreCase(current.carMake) > 0) { //go right if (current.Right == null ) { current.Right = new carNode(); fillInInfo(current.Right, carInfo); set = true; } else current = current.Right; } if (carInfo.compareToIgnoreCase(current.carMake) < 0) { //go left if (current.Left == null) { current.Left = new carNode(); fillInInfo(current.Left, carInfo); set = true; } else current = current.Left; } }//end of while }// end of else } /** will take the node and the string and detokenize it and fill in the what is known about it * @param curNode The node for the information to be added to * @param carInfo The string of information to be added to it */ private void fillInInfo(carNode curNode, String carInfo) { StringTokenizer st = new StringTokenizer(carInfo, ","); curNode.carMake = st.nextToken(); while (st.hasMoreTokens()) { curNode.carList.add(st.nextToken()); } } /** Will print the number of cars in our databse * @return int the number of cars */ public int numberOfCars() { int total = addCars(root); return total; } /** will return the total number of cars in our database * @return int the number of cars in the database */ private int addCars(carNode curNode) { if (curNode == null) return 0; else return 1 + addCars(curNode.Left) + addCars(curNode.Right); } /** This method will search through the makes of cars and return the information about each one with the same make * @param make The make of the cars we are looking for * @return ArrayList all the info about the cars listed */ public ArrayList searchByMake(String make) { if (!temp.isEmpty()) temp.clear(); findCarByMake(root, make); return temp; } /** This method will be the recursive method to find all the info on a certain car make * @param curNode The current node we are on * @param make The car make we are looking for. */ private void findCarByMake(carNode curNode, String make) { if (curNode == null) return; else if(!set) { //if the first node of make hasn't been found yet if (make.compareToIgnoreCase(curNode.carMake) > 0) { //go right findCarByMake(curNode.Right, make); } else if (make.compareToIgnoreCase(curNode.carMake) < 0) { //go right findCarByMake(curNode.Left, make); } else { //it must be the make we are looking for set = true; findCarByMake(curNode, make); } } else { //if the first occurance has already been found, find the rest if (make.compareToIgnoreCase(curNode.carMake) == 0) temp.add(curNode.carMake + " " + curNode.carList.toString()); findCarByMake(curNode.Left, make); findCarByMake(curNode.Right, make); } } /** Will take the given criteria and search through the entire database * to find matches. It will then return whatever matched in an * ArrayList. * @param criteria The criteria by which to search for */ public ArrayList searchByOther(String criteria) { if (!temp.isEmpty()) temp.clear(); findCarByOther(root, criteria); return temp; } /**Will take the given carNode and criteria and search through the entire database. * It will then put them into the ArrayList. * @param curNode The current node we are on * @param criteria The string for which to search by */ private void findCarByOther(carNode curNode, String criteria) { if (curNode == null) return; else { for (int i=0; i