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 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...
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 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 =...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT