Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question
100%

(Java) How can I implement a binary search tree in a program to store data, and use the delete method to trim the tree? The program should have a 'populate' button that obtains a string from the user, creates a sorted binary search tree of characters, and prints the tree using the DisplayTree class. It should also print the characters on one line in sorted order by traversing the tree in inorder fashion. The program should also have a 'Trim Tree' button that obtains a second line of input to delete characters from the tree, trimming the tree accordingly. It should ignore characters that are not in the tree, and only delete one character for each occurrence in the second line of input. When all characters from the second line have been deleted from the tree, the program should print the remaining characters in the tree using the DisplayTree class. The output should be labeled appropriately, and no spaces or commas should be used between tree elements in the inorder traversal.
Here is the base code given to me: 

package problem_set_30;
import static javax.swing.JOptionPane.*;

public class Pro3001{
    public static void main(String[] args) {
        new Environment101().setVisible(true);
}}


class Environment101 extends View
{
 //   BinaryTree101< ?? > charTree = ??;

    @Override
    public void buttonOneAction()
    {
        String lineInput =
            showInputDialog("Enter a line of input to insert:","marauder");


        //**************************************
        //  Create a binary search tree from
        //  the input string.
        //**************************************
        //      o
        //      o

       
        output.setText( "Tree after input.\n" );
    //    output.append( new DisplayTree</* o */>( charTree.getRoot()).toString() );
       
        //      o
        //      o
    }

    @Override
    public void buttonTwoAction()
    {
        String lineInput =
            showInputDialog("Enter a line of input to remove:","rummage");


        //**************************************
        //  Trim the binary search tree using
        //  the input string.
        //**************************************
        //      o
        //      o

       
        output.setText( "Tree after trimming.\n" );
    //    output.append( new DisplayTree</* o */>( charTree.getRoot()).toString() );
       
        //      o
        //      o
    }
   
    @Override
    public void setParams()
    {
        buttonOne.setText("Create Tree");
        buttonTwo.setText("Trim Tree");
    }
}


class BinaryTree101<T extends Comparable<T>>
{
    private TreeNode<T> root = null;

    public void delete(T key)
    {
        root = delete(root, key);
    }

    private TreeNode<T> delete(TreeNode<T> current, T item)
    {
        if(current == null)
            return null;

        if(  item.equals(current.getData())  )
        {
            if(current.getLeft() == null && current.getRight() == null)
                return null;
            if (current.getRight() == null)
                return current.getLeft();
            if (current.getLeft() == null)
                return current.getRight();
               
            TreeNode<T> follow = current;
            TreeNode<T> travel = current.getRight();
            while(travel.getLeft() != null)
            {
                follow = travel;
                travel = travel.getLeft();
            }
            travel.setLeft(current.getLeft());
            if(current.getRight() != travel)
            {
                follow.setLeft(travel.getRight());
                travel.setRight(current.getRight());
            }
            return travel;
        }

        if( item.compareTo(current.getData()) <= 0  )
            return current.setLeft( delete(current.getLeft(),item) );
        else
            return current.setRight( delete(current.getRight(),item) );
    }

    public boolean searchTree(T key)
    {
        return searchTree(root, key);
    }

    private boolean searchTree(TreeNode<T> current, T key)
    {
        if (current == null)
            return false;
        else
            if ( key.equals( current.getData() ) )
                return true;
        else
            if ( key.compareTo(current.getData()) <= 0 )
                return searchTree(current.getLeft(),key);
            else
                return searchTree(current.getRight(),key);
    }

    public void add(T data)
    {
        root = add( root, new TreeNode<>( data, null, null ) );
    }

    private TreeNode<T> add(TreeNode<T> current, TreeNode<T> temp)
    {
        if( current == null )
            return temp;
        else
            if ( temp.getData().compareTo(current.getData()) <= 0 )
                return current.setLeft( add(current.getLeft(), temp) );
            else
                return current.setRight ( add(current.getRight(), temp) );
    }

    public TreeNode<T> getRoot()
    {
        return root;
    }
}
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education