Question

Write a program that does the following. *Java Code* • Create an instance of the Hashtable...

Write a program that does the following. *Java Code*

• Create an instance of the Hashtable class from the Java API

• Make the initial table size of the hash table 1000 and the load factor 0.75 (which has been shown experimentally to be optimal).

• Create an instance of the AVLtree class attached to this homework.

• Measure the running time of adding various numbers of entries (as required by the table below) to the hash table and the AVL tree.

• For each of those cases, measure the running time of searching for a key that is not in the hash table and do the same for the AVL tree.

Fill in the following charts. Adjust the values of n as needed according to your platform to obtain at least 4 measurements.

construction time n = 10^2 n = 10^3 n = 10^4 n = 10^5 n = 10^6

Hash table

Tree

search time n = 10^2 n = 10^3 n = 10^4 n = 10^5 n = 10^6

Hash table

Tree

How do these measurements compare with your conjecture in parts 1 and 2? If the results differ from your conjecture, investigate the reason by looking carefully at the code of Hashtable and AVLtree provided and explain what might have happened.

Homework Answers

Answer #1

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 count number of nodes /
public int countNodes()
{
return countNodes(root);
}
private int countNodes(AVLNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.left);
l += countNodes(r.right);
return l;
}
}
/ 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);
}
}
/ Function for preorder traversal /
public void preorder()
{
preorder(root);
}
private void preorder(AVLNode r)
{
if (r != null)
{
System.out.print(r.data +" ");
preorder(r.left);
preorder(r.right);
}
}
/ Function for postorder traversal /
public void postorder()
{
postorder(root);
}
private void postorder(AVLNode r)
{
if (r != null)
{
postorder(r.left);
postorder(r.right);
System.out.print(r.data +" ");
}
}
}


/ 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. count nodes");
System.out.println("4. check empty");
System.out.println("5. 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 :
System.out.println("Enter integer element to search");
System.out.println("Search result : "+ avlt.search( scan.nextInt() ));
break;
case 3 :
System.out.println("Nodes = "+ avlt.countNodes());
break;
case 4 :
System.out.println("Empty status = "+ avlt.isEmpty());
break;
case 5 :
System.out.println("\nTree Cleared");
avlt.makeEmpty();
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/ Display tree /
System.out.print("\nPost order : ");
avlt.postorder();
System.out.print("\nPre order : ");
avlt.preorder();
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');
}
}
advertisements

AVLTree Tree Test


AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
4
Empty status = true

Post order :
Pre order :
In order :
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
10

Post order : 10
Pre order : 10
In order : 10
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
9

Post order : 9 10
Pre order : 10 9
In order : 9 10
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
8

Post order : 8 10 9
Pre order : 9 8 10
In order : 8 9 10
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
7

Post order : 7 8 10 9
Pre order : 9 8 7 10
In order : 7 8 9 10
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
6

Post order : 6 8 7 10 9
Pre order : 9 7 6 8 10
In order : 6 7 8 9 10
Do you want to continue (Type y or n)

y

AVLTree Operations

1. insert
2. search
3. count nodes
4. check empty
5. clear tree
1
Enter integer element to insert
5

Post order : 5 6 8 10 9 7
P

Know the answer?
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
Similar Questions
This should be a simple code for beginning Java; Create a Student class with four instance...
This should be a simple code for beginning Java; Create a Student class with four instance variables, a default constructor, a 4-arg constructor, and getters and setters for each instance variable.
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Write the program in java Implement a class Product. Create instance variables to store product name...
Write the program in java Implement a class Product. Create instance variables to store product name and price and supply the values through constructor. For example new Product(“Toaster’, 29.95). Create methods, getName, getPrice. Write a method productPrinter that prints the product name and its price after reducing it by $5. Create a main class and necessary constructs in the main class to run the Product class.
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
In Java programming language 11.Create the code for a for loop to print the numbers 1  through...
In Java programming language 11.Create the code for a for loop to print the numbers 1  through 5 inclusive vertically so the output is 1 2 3 4 5 11b What is the output of the following               OUTPUT int i=3; while(int i >0) { System.out.println(i); i--; } 11c What is the output of the following               OUTPUT for(int i=3;i<10;i++) System.out.println(3*i); 11d Create the line of code to print the numbers 10 through 1 decreasing by 1 each time 11e Create the line of code...
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should...
For Java. Let's get some practice using Java libraries! Create a class named Formatter. Formatter should provide one public class method formatDate. It should accept a positive long representing milliseconds since 1970 and return a String containing the ISO-8601 representation of this time. Here is one example for a recent timestamp: given 1602106609897 your function should return 2020-10-07T21:36:49.897Z. Do not overthink this. The solution we are after is a single line of code. We suggest that you explore the various...
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT