Question

In: Computer Science

write a java program that implements the splay tree data structure for the dictionary abstract data...

write a java program that implements the splay tree data structure for the dictionary abstract data type.

Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line.

in.dat file contents:

3456
5678
1234
2369
7721
3354
1321
4946
3210
8765

Then the program starts an interactive mode. The commands are as follows.

S 1000 - splay the tree at 1000

F 2000 - search/find the node with key 2000

I 3000 - insert a node with key 3000

D 4000 - delete the node with key 4000

For each command,

1. Report appropriate message after the command is executed. Examples are:

Splay is done

Search is successful

Search is unsuccessful

The key is inserted into the tree

Duplicated keys

The key is deleted from the tree

The key is not in the tree

2. Display the new tree.

Solutions

Expert Solution

public class Main

{

    class SplayNode

    {    

        SplayNode left, right, parent;

        int element;

        

        public SplayNode()  // Constructor

        {

            this(0, null, null, null);

        }          

    

        public SplayNode(int ele)  // Constructor

        {

            this(ele, null, null, null);

        }   

     

        public SplayNode(int ele, SplayNode left, SplayNode right, SplayNode parent)   // Constructor

        {

            this.left = left;

            this.right = right;

            this.parent = parent;

            this.element = ele;         

        }    

    }

    class SplayTree

    {

        private SplayNode root;

        private int count = 0;

        public SplayTree() // Constructor

        {

            root = null;

        }

        public boolean isEmpty()   // to check if tree is empty

        {

            return root == null;

        }

        

        public void clear()    //to clear tree

        {

            root = null;

            count = 0;

        }

        public void insert(int ele)    // to insert element

        {

            SplayNode z = root;

            SplayNode p = null;

            while (z != null)

            {

                p = z;

                if (ele > p.element)

                    z = z.right;

                else

                    z = z.left;

            }

            z = new SplayNode();

            z.element = ele;

            z.parent = p;

            if (p == null)

                root = z;

            else if (ele > p.element)

                p.right = z;

            else

                p.left = z;

            Splay(z);

            count++;

        }

        public void makeLeftChildParent(SplayNode c, SplayNode p)

        {

            if ((c == null) || (p == null) || (p.left != c) || (c.parent != p))

                throw new RuntimeException("WRONG");

            if (p.parent != null)

            {

                if (p == p.parent.left)

                    p.parent.left = c;

                else

                    p.parent.right = c;

            }

            if (c.right != null)

                c.right.parent = p;

            c.parent = p.parent;

            p.parent = c;

            p.left = c.right;

            c.right = p;

        }

        

        public void makeRightChildParent(SplayNode c, SplayNode p)

        {

            if ((c == null) || (p == null) || (p.right != c) || (c.parent != p))

                throw new RuntimeException("WRONG");

            if (p.parent != null)

            {

                if (p == p.parent.left)

                    p.parent.left = c;

                else

                    p.parent.right = c;

            }

            if (c.left != null)

                c.left.parent = p;

            c.parent = p.parent;

            p.parent = c;

            p.right = c.left;

            c.left = p;

        }

        private void Splay(SplayNode x)    // function to splay

        {

            while (x.parent != null)

            {

                SplayNode Parent = x.parent;

                SplayNode GrandParent = Parent.parent;

                if (GrandParent == null)

                {

                    if (x == Parent.left)

                        makeLeftChildParent(x, Parent);

                    else

                        makeRightChildParent(x, Parent);                 

                }

                else

                {

                    if (x == Parent.left)

                    {

                        if (Parent == GrandParent.left)

                        {

                            makeLeftChildParent(Parent, GrandParent);

                            makeLeftChildParent(x, Parent);

                        }

                        else

                        {

                            makeLeftChildParent(x, x.parent);

                            makeRightChildParent(x, x.parent);

                        }

                    }

                    else

                    {

                        if (Parent == GrandParent.left)

                        {

                            makeRightChildParent(x, x.parent);

                            makeLeftChildParent(x, x.parent);

                        }

                        else

                        {

                            makeRightChildParent(Parent, GrandParent);

                            makeRightChildParent(x, Parent);

                        }

                    }

                }

            }

            root = x;

        }

        public void remove(int ele)     //to remove element

        {

            SplayNode node = findNode(ele);

            remove(node);

        }

        private void remove(SplayNode node)     //to remove node

        {

            if (node == null)

                return;

            Splay(node);

            if( (node.left != null) && (node.right !=null))

            {

                SplayNode min = node.left;

                while(min.right!=null)

                    min = min.right;

                min.right = node.right;

                node.right.parent = min;

                node.left.parent = null;

                root = node.left;

            }

            else if (node.right != null)

            {

                node.right.parent = null;

                root = node.right;

            }

            else if( node.left !=null)

            {

                node.left.parent = null;

                root = node.left;

            }

            else

            {

                root = null;

            }

            node.parent = null;

            node.left = null;

            node.right = null;

            node = null;

            count--;

        }

        public int countNodes()     // to count number of nodes

        {

            return count;

        }

        public boolean search(int val)    // to search for an element

        {

            return findNode(val) != null;

        }

        private SplayNode findNode(int ele)

        {

            SplayNode PrevNode = null;

            SplayNode z = root;

            while (z != null)

            {

                PrevNode = z;

                if (ele > z.element)

                    z = z.right;

                else if (ele < z.element)

                    z = z.left;

                else if(ele == z.element)

                {

                    Splay(z);

                return z;

                }

            }

            if(PrevNode != null)

            {

                Splay(PrevNode);

                return null;

            }

            return null;

        }

        public void inorder()  //for inorder traversal

        {

            inorder(root);

        }

        private void inorder(SplayNode r)

        {

            if (r != null)

            {

                inorder(r.left);

                System.out.print(r.element +" ");

                inorder(r.right);

            }

        }

        public static void main(String[] args)

        {

            File file = new File("in.dat");

            Scanner scnr = new Scanner(file);

            int n,i=0;

        

            while(scnr.hasNextLine())   // count number of integers in file

            {

                n= Integer.parseInt(scnr.nextLine());

                i++;

            }

            int item[i];    //to store data in array

            i=0;

            while(scnr.hasNextLine())

            {

                n= Integer.parseInt(scnr.nextLine();

                item[i]=n;

                i++;

            }

            node root = newNode(1000);

            System.out.System.out.println("splay is done");

            root = spt.search(2000);

            if(root!=null)

                System.out.System.out.println("search is successful");

            else

                System.out.System.out.println("search is not successful");

            spt.insert(3000);

            System.out.System.out.println("The key is inserted into the tree");

            k=spt.remove(4000);

            if(k)

                System.out.System.out.println("The key is deleted from the tree");

            else

                System.out.System.out.println("The key is not in the tree");

            System.out.System.out.println("the tree is :");

            spt.inorder();

        

        }

}


Related Solutions

Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are...
**write a java program infix to postfix**showing the expression tree*** I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Write a spell checking program (java) which uses a dictionary of words (input by the user...
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words. Functional requirements: For each word in the input string, your program should search the dictionary string for the given word. If the word is not in...
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a...
Write a Java program that implements the Depth-First Search (DFS) algorithm. Input format: This is a sample input from a user. 3 2 0 1 1 2 The first line (= 3 in the example) indicates that there are three vertices in the graph. You can assume that the first vertex starts from the number 0. The second line (= 2 in the example) represents the number of edges, and following two lines are the edge information. This is the...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT