Hanyouslayer 12 Newbie Poster

I made a program that stores a collection of CDs. This collection is entered in a BinarySearchTree and contains the Artist, Album, favorite song, track numbers, and cost of cd. The inorder traversal works to print everything in the tree, and the cdPrintGroup method prints all the Artists(aka groups), but when I try and insert or delete, I get nothing. Inserting doesn't work, and when I do the delete method from the BinarySearchTree class, it states I can't delete an empty tree. How can that be if the inorderTraversal method works?

Thanks for your time.

Driver:

import java.io.*;
import java.util.*;

public class Lab9{
	
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args){
    	
    	//call CDBinaryTree class
        CDBinaryTree cdList = new CDBinaryTree();
        BinarySearchTree bst = new BinarySearchTree();
        
        //declare variables
        int choice;
        String group;
        String newCD;
        try{
        	
        	Scanner inFile =
               new Scanner(new FileReader("c:\\CDDat.txt"));

            createCdList(inFile, cdList);

            displayMenu();
            System.out.print("Enter your choice: ");
            choice = console.nextInt();
            console.nextLine();
            System.out.println();

            while (choice != 9){
            	
                switch (choice){
                
                case 1: System.out.print("Enter the CD information: ");
                		newCD = console.nextLine();
                		bst.insert(newCD);//something to insert an object
                		break;
                case 2: System.out.print("Enter the group to delete: ");
                		group = console.nextLine();
                		bst.search(group);//something to search to delete
                		bst.deleteNode(group);
                		//something to delete that object
                		System.out.println("Group deleted.");
                		break;
                case 3: cdList.cdPrintGroup();
                		break;
                case 4: cdList.inorderTraversal();
                		break;
                default: System.out.println("Please enter another number.");
                }//end switch
                
                //call method
                displayMenu();
                System.out.print("Enter your choice: ");
                //take in choice
                choice = console.nextInt();
                console.nextLine();
                System.out.println();
                
            }//end while
        }//end try
        catch (FileNotFoundException fnfe){
        	
            System.out.println(fnfe.toString());
        }//end catch
        catch (IOException ioe){
        	
            System.out.println(ioe.toString());
        }//end catch
    }//end main method

    public static void createCdList(Scanner inFile,
                                     CDBinaryTree cdList){
    	
    	//Declare variables
        String  artist;
        String  cdGroup;
        String  favTrack;
        String  numTracks;
        String  cdCost;
        
        //Create new CD instance
        CD newCD;
        
        //While loop to take in variables
        while (inFile.hasNext()){
        	
        	inFile.useDelimiter(",");
            artist = inFile.next();
            cdGroup = inFile.next();
            favTrack = inFile.next();
            numTracks = inFile.next();
            cdCost = inFile.next();
            inFile.nextLine();
            newCD = new CD();
            newCD.setCdInfo(artist, cdGroup, favTrack,
                                  numTracks, cdCost);
                                 
            cdList.insert(newCD);
        }//end while that takes in variables
    }//end createCdList

   //Method to print out menu to sdout corresponding to the switch statement.
   public static void displayMenu(){
	   
	   System.out.println();
       System.out.println("Select one of the following: ");
       System.out.println("1: To Insert another CD");
       System.out.println("\t Insert instructions: Insert the "
    		   				+ "Group/Artist, CD Title, Favourite");
       System.out.println("\t Track, # of Tracks, CD Cost,");
       System.out.println("\t An example: Van Halen, 1984, Jump, 9,"
    		   				+ " 9.99,");
       System.out.println("\t Be sure to add the additional , at the end.");
       System.out.println("2: To delete a CD from the collection");
       System.out.println("3: To print the groups of all "
                            + "the CDs");
       System.out.println("4: To print a list of all "
                            + "the CDs");
       System.out.println("9: To exit");
   }//end displayMenu method
}//end MainProgVideoStore class

Worker CDBinaryTree:

public class CDBinaryTree extends BinarySearchTree<CD>
{
        //Default constructor
        //Postcondition: root = null;
    public CDBinaryTree()
    {
        super();
    }

        //Method to search the video list for a
        //particular CD, specified by the parameter group.
        //Postcondition: If the CD is found, a reference to
        //            the node containing the CD is returned;
        //            otherwise, the value null is returned.
    private BinaryTreeNode<CD> searchCdList(String group)
    {
        boolean found = false;   //set found to false
        BinaryTreeNode<CD> current = null;
        CD temp = new CD();

        temp.setCdInfo(group, "", "", "", "");

        if (root == null)  //the tree is empty
            System.out.println("Cannot search an empty list. ");
        else
        {
            current = root;   //set current point to the root node
                                  //of the binary tree
            found = false;    //set found to false

            while (current != null && !found) //search the tree
                if (current.info.equals(temp)) //the item is found
                    found = true;
                else
                    if (current.info.compareTo(temp) > 0)
                        current = current.lLink;
                    else
                        current = current.rLink;
        } //end else

        return current;
    }//end searchCdList

    //Method to print the groups of all the CDs in stock.
    public void cdPrintGroup()
    {
        inorderGroup(root);
    }

           //Method to print the groups of all the CDs in
           //the tree pointed to by p.
    private void inorderGroup(BinaryTreeNode<CD> p)
    {
        CD temp;

        if (p != null)
        {
            inorderGroup(p.lLink);
            temp = (CD) p.info;
            temp.printGroup();
            inorderGroup(p.rLink);
        }
    }
}

Worker: CD:

public class CD implements Cloneable, Comparable
{
      //instance variables
    private String artist1;  //variable to store the name
                                //of the artist
    private String cdTitle1;  //variable to store the name
                                //of the title
    private String favoriteTrack;  //variable to store the name
                                //of the favourite track
    private String numberTracks;  //variable to store the name
                                   //of the numbersOfTracks
    private String costCD;  //variable to store the name
                                   //of the costOfCD
    
       //Default constructor
       //Instance variables are initialized to their
       //default values.
       //Postcondition: artist1 = ""; cdTitle1 = "";
       //         favoriteTrack= ""; numberTracks = "";
       //         costCD= "";
       
    public CD(){
    	
        artist1 = "";
        cdTitle1 = "";
        favoriteTrack= "";
        numberTracks = "";
        costCD= "";
    }//end CD constructor

       //Constructor with parameters
       //Instance variables are set according to the parameters
       //Postcondition: artist1 = group; cdTitle1 = cdTitle2;
       //         favoriteTrack= favouriteTrack; numberTracks = numbersOfTracks;
       //         costCD= costOfCD;
       
    public CD(String group, String cdTitle2,
                        String favouriteTrack, String numbersOfTracks,
                        String costOfCD){
    	
        setCdInfo(group, cdTitle2, favouriteTrack, numbersOfTracks, costOfCD);
    }//end CD constructor

       //Returns a copy of objects data in store.
       //Postcondition: A reference to a clone of CD's
       //               data is returned.
    public Object clone(){
    	
        try{
            return super.clone();
        }//end try
        
        catch (CloneNotSupportedException e){
        	
            return null;
        }//end catch
    }//end clone method

       //Method to set the details of a CD.
       //Instance variables  are set according to the
       //parameters.
       //Postcondition: artist1 = group; cdTitle1 = cdTitle2;
       //               favoriteTrack= favouriteTrack;
       //               numberTracks = numbersOfTracks;
       //               costCD= costOfCD;
      
    public void setCdInfo(String group, String cdTitle2,
                             String favouriteTrack, String numbersOfTracks,
                             String costOfCD){
    	
    	//reassign variables
        artist1 = group;
        cdTitle1 = cdTitle2;
        favoriteTrack= favouriteTrack;
        numberTracks = numbersOfTracks;
        costCD= costOfCD;
    }//end setCdInfo method 
    
       //Method to print the group of a CD.
    public void printGroup(){
    	
        System.out.println("Group: " + artist1);
    }//end printGroup method

       //Method to print the details of a CD.
    public void printInfo(){
    	
        System.out.println("Artist: " + artist1);
        System.out.println("CD Title: " + cdTitle1);
        System.out.println("Favourite Track: " + favoriteTrack);
        System.out.println("Numbers Of Tracks: " + numberTracks);
        System.out.println("Cost Of CD: " + costCD);
        System.out.println();
    }//end printInfo method

       //Method to determine whether group is the same as the
       //group of the CD.
       //Postcondition: Returns the value true if group is the
       //               same as the group of the CD,
       //               false otherwise.
    public boolean checkGroup(String group){
    	
        return(artist1.compareTo(group) == 0);
    }//end checkGroup method

    public String getGroup(){
    	
        return artist1;
    }//end getGroup method

       //Method to compare the groups of two CD.
       //Postcondition: Returns true if this CD's group is
       //               equal to the group of otherCD;
       //               otherwise returns false
    public boolean equals(Object otherCD){
    	
        CD temp = (CD) otherCD;

        return (artist1.compareTo(temp.artist1) == 0);
    }//end equals method

        //Method to compare the groups of two CDs.
        //Postconditition: Returns a negative value if the
        //                 group of this CD is less than
        //                 the group of otherCD; zero if
        //                 the group of this CD is the
        //                 same as the group of otherCD;
        //                 returns a positive value if the
        //                 group of this CD is greater
        //                 than the group of otherCD
    public int compareTo(Object otherCD){
    	
        CD temp = (CD) otherCD;

        return (artist1.compareTo(temp.artist1));
    }//end compareTo method

        //Method to return the CD info, as a string.
    public String toString(){
    	
        String cdInfo;

        cdInfo = "Artist: " + artist1 + "\n"
                     + "CD Title: " + cdTitle1 + "\n"
                     + "Favourite Track: " + favoriteTrack + "\n"
                     + "Numbers Of Tracks: " + numberTracks + "\n"
                     + "Cost Of CD: " + costCD+ "\n"
                     + "\n";
        
        return cdInfo;
    }//end toString method
}//end CD class

Worker: BinarySearchTree - Generic Class

public class BinarySearchTree<T> extends BinaryTree<T>
{
       //Default constructor
       //Postcondition: root = null;
    public BinarySearchTree()
    {
        super();
    }

       //Method to determine whether searchItem is in the
       //binary search tree.
       //Postcondition: Returns true if searchItem is found
       //               in the binary search tree;
       //               otherwise, returns false.
    public boolean search(T searchItem)
    {
        BinaryTreeNode<T> current;
        boolean found = false;

        if (root == null)
            System.out.println("Cannot search an empty tree.");
        else
        {
            current = root;

            while (current != null && !found)
            {
                Comparable<T> temp =
                         (Comparable<T>) current.info;

                if (temp.compareTo(searchItem) == 0)
                    found = true;
                else if (temp.compareTo(searchItem) > 0)
                    current = current.lLink;
                else
                    current = current.rLink;
            }//end while
        }//end else

        return found;
    }//end search

       //Method to insert insertItem in the binary search tree.
       //Postcondition: If no node in the binary search tree has
       //               the same info as insertItem, a node with
       //               the info insertItem is created and inserted
       //               in the binary search tree.
    public void insert(T insertItem)
    {
        BinaryTreeNode<T> current;    //reference variable to
                                      //traverse the tree
        BinaryTreeNode<T> trailCurrent = null; //reference variable
                                               //behind current
        BinaryTreeNode<T> newNode;       //reference variable to
                                         //create the node

        newNode = new BinaryTreeNode<T>(insertItem, null, null);

        if (root == null)
            root = newNode;
        else
        {
            current = root;

            while (current != null)
            {
                trailCurrent = current;

                Comparable<T> temp1 =
                              (Comparable<T>) current.info;

                if (temp1.compareTo(insertItem) == 0)
                {
                    System.err.print("The insert item is "
                                   + "already in the tree -- "
                                   + "duplicates are not "
                                   + "allowed.");
                    return;
                }
                else if (temp1.compareTo(insertItem) > 0)
                    current = current.lLink;
                else
                    current = current.rLink;
            }//end while

            Comparable<T> temp2 =
                       (Comparable<T>) trailCurrent.info;

            if (temp2.compareTo(insertItem) > 0)
                trailCurrent.lLink = newNode;
            else
                trailCurrent.rLink = newNode;
        }
    }//end insert

       //Method to delete deleteItem from the binary search tree
       //Postcondition: If a node with the same info as
       //               deleteItem is found, it is deleted from
       //               the binary search tree.
    public void deleteNode(T deleteItem)
    {
        BinaryTreeNode<T> current;  //reference variable to
                                  //traverse the tree
        BinaryTreeNode<T> trailCurrent; //reference variable
                                      //behind current
        boolean found = false;

        if (root == null)
            System.err.println("Cannot delete from an "
                             + "empty tree.");
        else
        {
            current = root;
            trailCurrent = root;

            while (current != null && !found)
            {
                if (current.info.equals(deleteItem))
                    found = true;
                else
                {
                    trailCurrent = current;

                    Comparable<T> temp =
                              (Comparable<T>) current.info;

                    if (temp.compareTo(deleteItem) > 0)
                        current = current.lLink;
                    else
                        current = current.rLink;
                }
            }//end while

            if (current == null)
                System.out.println("The delete item is not in "
                                 + "the tree.");
            else if (found)
            {
                if (current == root)
                    root = deleteFromTree(root);
                else
                {
                    Comparable<T> temp =
                             (Comparable<T>) trailCurrent.info;

                    if (temp.compareTo(deleteItem) > 0)
                        trailCurrent.lLink =
                              deleteFromTree(trailCurrent.lLink);
                    else
                        trailCurrent.rLink =
                              deleteFromTree(trailCurrent.rLink);
                }
            }//end else-if
        }
    }//end deleteNode

       //Method to delete the node, to which p points, from
       //the binary search tree.
       //Postcondition: The node to which p points is deleted
       //               from the binary search tree. The
       //               reference of the root node of the binary
       //               search tree after deletion is returned.
    private BinaryTreeNode deleteFromTree(BinaryTreeNode<T> p)
    {
        BinaryTreeNode<T> current;   //reference variable to
                                     //traverse the tree
        BinaryTreeNode<T> trailCurrent;   //reference variable
                                        //behind current
        if (p == null)
            System.err.println("Error: The node to be deleted "
                             + "is null.");
        else if (p.lLink == null && p.rLink == null)
             p = null;
        else if (p.lLink == null)
             p = p.rLink;
        else if (p.rLink == null)
             p = p.lLink;
        else
        {
            current = p.lLink;
            trailCurrent = null;

            while (current.rLink != null)
            {
                trailCurrent = current;
                current = current.rLink;
            }//end while

            p.info = current.info;

            if (trailCurrent == null) //current did not move;
                                      //current == p.lLink;
                                      //adjust p
               p.lLink = current.lLink;
            else
                trailCurrent.rLink = current.lLink;
        }//end else

        return p;
    }//end deleteFromTree
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.