Question

Write a java application to conduct an experiment that demonstrates the speed difference when performing insert...

Write a java application to conduct an experiment that demonstrates the speed difference when performing insert and search between the AVL tree and BST.

You want to vary the size of the dataset (N) and measure the number of comparison operations in the best/average/worst case for every value of N (from 1-500).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

   In this i am using bst and avl tree program saperatly. I use n=1 to 10.

BST = Best/average/worst - 2473/5686/9128 milisec

AVL = Best/average/worst - 2087/4932/14800 milisec

     import java.util.Scanner;

   


     class BSTNode

     {

         BSTNode left, right;

         int data;

   


         public BSTNode()

         {

             left = null;

             right = null;

             data = 0;

         }


         public BSTNode(int n)

         {

             left = null;

             right = null;

             data = n;

         }


         public void setLeft(BSTNode n)

         {

             left = n;

         }


         public void setRight(BSTNode n)

         {

             right = n;

         }


         public BSTNode getLeft()

         {

             return left;

         }


         public BSTNode getRight()

         {

             return right;

         }


         public void setData(int d)

         {

             data = d;

         }


         public int getData()

         {

             return data;

         }   

     }

     class BST

     {

         private BSTNode root;

   

   
         public BST()

         {

             root = null;

         }

   
         public boolean isEmpty()

         {

             return root == null;

         }

   

         public void insert(int data)

         {

             root = insert(root, data);

         }

   
         private BSTNode insert(BSTNode node, int data)

         {

             if (node == null)

                 node = new BSTNode(data);

             else

             {

                 if (data <= node.getData())

                     node.left = insert(node.left, data);

                 else

                     node.right = insert(node.right, data);

             }

             return node;

         }

   
         public boolean search(int val)

         {

             return search(root, val);

         }

   
         private boolean search(BSTNode r, int val)

         {
          

             boolean found = false;

             while ((r != null) && !found)

             {

                 int rval = r.getData();

                 if (val < rval)

                     r = r.getLeft();

                 else if (val > rval)

                     r = r.getRight();

                 else

                 {

                     found = true;

                     break;

                 }

                 found = search(r, val);

             }

             return found;

         }


         public void inorder()

         {

             inorder(root);

         }

         private void inorder(BSTNode r)

         {

             if (r != null)

             {

                 inorder(r.getLeft());

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

                 inorder(r.getRight());

             }

         }

    
       
     }

   

     public class BinarySearchTree

     {

         public static void main(String[] args)

        {               

            Scanner scan = new Scanner(System.in);

    
            BST bst = new BST();

            System.out.println("Binary Search Tree Test\n");        

            char ch;

    
            do  

            {

                System.out.println("\nBinary Search Tree Operations\n");

                System.out.println("1. insert ");

                System.out.println("2. search");

                System.out.println("3. check empty");

   

                int choice = scan.nextInt();          

                switch (choice)

                {

                case 1 :

                    System.out.println("Enter integer element to insert");

                    bst.insert( scan.nextInt() );                   

                    break;                        
                       

                case 2 :
                   long startTime = System.currentTimeMillis();

                    System.out.println("Enter integer element to search");

                    System.out.println("Search result : "+ bst.search( scan.nextInt() ));
                  
                    long stopTime = System.currentTimeMillis();
                  
                    long elapsedTime = stopTime - startTime;
                
                    System.out.println("Time Taken : " +elapsedTime);

                    break;                                        

                case 3 :

                    System.out.println("Empty status = "+ bst.isEmpty());

                    break;          

                default :

                    System.out.println("Wrong Entry \n ");

                    break;

                }


                System.out.print("\nIn order : ");

                bst.inorder();

   

                System.out.println("\nDo you want to continue (Type y or n) \n");

                ch = scan.next().charAt(0);                      

            } while (ch == 'Y'|| ch == 'y');             

        }

     }

import java.util.Scanner;

/* Class AVLNode */

class AVLNode

{  

     AVLNode left, right;

     int data;

     int height;

     /* Constructor */

     public AVLNode()

     {

         left = null;

         right = null;

         data = 0;

         height = 0;

     }

     /* Constructor */

     public AVLNode(int n)

     {

         left = null;

         right = null;

         data = n;

         height = 0;

     }   

}

/* Class AVLTree */

class AVLTree

{

     private AVLNode root;   

     /* Constructor */

     public AVLTree()

     {

         root = null;

     }

     /* Function to check if tree is empty */

     public boolean isEmpty()

     {

         return root == null;

     }

     /* Make the tree logically empty */

     public void makeEmpty()

     {

         root = null;

     }

     /* Function to insert data */

     public void insert(int data)

     {

         root = insert(data, root);

     }

     /* Function to get height of node */

     private int height(AVLNode t )

     {

         return t == null ? -1 : t.height;

     }

     /* Function to max of left/right node */

     private int max(int lhs, int rhs)

     {

         return lhs > rhs ? lhs : rhs;

     }

     /* Function to insert data recursively */

     private AVLNode insert(int x, AVLNode t)

     {

         if (t == null)

             t = new AVLNode(x);

         else if (x < t.data)

         {

             t.left = insert( x, t.left );

             if( height( t.left ) - height( t.right ) == 2 )

                 if( x < t.left.data )

                     t = rotateWithLeftChild( t );

                 else

                     t = doubleWithLeftChild( t );

         }

         else if( x > t.data )

         {

             t.right = insert( x, t.right );

             if( height( t.right ) - height( t.left ) == 2 )

                 if( x > t.right.data)

                     t = rotateWithRightChild( t );

                 else

                     t = doubleWithRightChild( t );

         }

         else

           ; // Duplicate; do nothing

         t.height = max( height( t.left ), height( t.right ) ) + 1;

         return t;

     }

     /* Rotate binary tree node with left child */   

     private AVLNode rotateWithLeftChild(AVLNode k2)

     {

         AVLNode k1 = k2.left;

         k2.left = k1.right;

         k1.right = k2;

         k2.height = max( height( k2.left ), height( k2.right ) ) + 1;

         k1.height = max( height( k1.left ), k2.height ) + 1;

         return k1;

     }

     /* Rotate binary tree node with right child */

     private AVLNode rotateWithRightChild(AVLNode k1)

     {

         AVLNode k2 = k1.right;

         k1.right = k2.left;

         k2.left = k1;

         k1.height = max( height( k1.left ), height( k1.right ) ) + 1;

         k2.height = max( height( k2.right ), k1.height ) + 1;

         return k2;

     }

     /**

      * Double rotate binary tree node: first left child

      * with its right child; then node k3 with new left child */

     private AVLNode doubleWithLeftChild(AVLNode k3)

     {

         k3.left = rotateWithRightChild( k3.left );

         return rotateWithLeftChild( k3 );

     }

     /**

      * Double rotate binary tree node: first right child

      * with its left child; then node k1 with new right child */    

     private AVLNode doubleWithRightChild(AVLNode k1)

     {

         k1.right = rotateWithLeftChild( k1.right );

         return rotateWithRightChild( k1 );

     }  

     /* Functions to search for an element */

     public boolean search(int val)

     {

         return search(root, val);

     }

     private boolean search(AVLNode r, int val)

     {

         boolean found = false;

         while ((r != null) && !found)

         {

             int rval = r.data;

             if (val < rval)

                 r = r.left;

             else if (val > rval)

                 r = r.right;

             else

             {

                 found = true;

                 break;

             }

             found = search(r, val);

         }

         return found;

     }

     /* Function for inorder traversal */

     public void inorder()

     {

         inorder(root);

     }

     private void inorder(AVLNode r)

     {

         if (r != null)

         {

             inorder(r.left);

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

             inorder(r.right);

         }

     }

   
}

/* Class AVL Tree Test */

public class AVLTreeTest

{

     public static void main(String[] args)

    {          

        Scanner scan = new Scanner(System.in);

        /* Creating object of AVLTree */

        AVLTree avlt = new AVLTree();

        System.out.println("AVLTree Tree Test\n");        

        char ch;

        /* Perform tree operations */

        do  

        {

            System.out.println("\nAVLTree Operations\n");

            System.out.println("1. insert ");

            System.out.println("2. search");

            System.out.println("3. check empty");

            System.out.println("4. clear tree");

            int choice = scan.nextInt();          

            switch (choice)

            {

            case 1 :

                System.out.println("Enter integer element to insert");

                avlt.insert( scan.nextInt() );                   

                break;                        

            case 2 :
               long startTime = System.currentTimeMillis();

                System.out.println("Enter integer element to search");

                System.out.println("Search result : "+ avlt.search( scan.nextInt() ));

                long stopTime = System.currentTimeMillis();
              
                long elapsedTime = stopTime - startTime;
            
                System.out.println("Time Taken : " +elapsedTime);
                break;                                        

            case 3 :

                System.out.println("Empty status = "+ avlt.isEmpty());

                break;   

            case 4 :

                System.out.println("\nTree Cleared");

                avlt.makeEmpty();

                break;       

            default :

                System.out.println("Wrong Entry \n ");

                break;

            }

            /* Display tree */


            System.out.print("\nIn order : ");

            avlt.inorder();

            System.out.println("\nDo you want to continue (Type y or n) \n");

            ch = scan.next().charAt(0);                      

        } while (ch == 'Y'|| ch == 'y');             

    }

}

Add a comment
Know the answer?
Add Answer to:
Write a java application to conduct an experiment that demonstrates the speed difference when performing insert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y

     a. The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y. Construct the tree and determine the output of the PREORDER traversal output.   b. One main difference between a binary search tree (BST) and an AVL (Adelson-Velski and Landis) tree is that an AVL tree has a balance condition, that is, for every node in the AVL tree, the height of the left and right subtrees differ by at most 1....

  • Canvas →XC 6 D Question 10 5 pts When sorting n records, Quicksort has worst-case cost...

    Canvas →XC 6 D Question 10 5 pts When sorting n records, Quicksort has worst-case cost On) On 2) On logn) Olm Question 11 5 pts In the worst case, the very best that a comparison based sorting algorithm can do when sorting n records is On 2) Allog in! (n) (login) Question 12 5 pts An AVL tree is a Binary Search Tree that has the following additional property none of the above for every node in the tree....

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • Answer B java Problem 2 For each problem given below, do the following: 1. Create an...

    Answer B java Problem 2 For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2.Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Determine the number of operations in each step of the pseudocode. To do that, identify...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • 1 Overview For this assignment you are required to write a Java program that plays (n,...

    1 Overview For this assignment you are required to write a Java program that plays (n, k)-tic-tac-toe; (n, k)-tic- tac-toe is played on a board of size n x n and to win the game a player needs to put k symbols on adjacent positions of the same row, column, or diagonal. The program will play against a human opponent. You will be given code for displaying the gameboard on the screen. 2 The Algorithm for Playing (n, k)-Tic-Tac-Toe The...

  • Are my answers correct? Question 1 (Mandatory) (1 point) Saved Modern software systems are blurring the...

    Are my answers correct? Question 1 (Mandatory) (1 point) Saved Modern software systems are blurring the distinction between local files and web pages, which may be stored on a remote computer, so the amount of data that we might wish to search is virtually unlimited. Remarkably, the methods that we shall study can support search and insert operations on symbol tables containing trillions of items or more using only four or five references to small blocks of data. 1) True...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT