Question

In: Computer Science

(a) Estimate the asymptotic running time of searching in a skewed BST and a balanced BST....

(a) Estimate the asymptotic running time of searching in a skewed BST and a balanced BST. Fill the following table with the big-O running times of each case.

skewed tree balanced tree
search

(b) Explain each of the running times in the table applying the rules of counting seen in class. Specify which method(s) of the BST class you will use, if we have covered them already in class, directly refer to the running times of those methods. (Partial credit if you do not explain both running times. No points for ambiguous or incomplete explanations.)

/**
* Exception class for access in empty containers
* such as stacks, queues, and priority queues.
* @author Mark Allen Weiss
*/
public class UnderflowException extends RuntimeException
{
}

// BinarySearchTree class
//
// CONSTRUCTION: with no initializer
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// boolean contains( x ) --> Return true if x is present
// Comparable findMin( ) --> Return smallest item
// Comparable findMax( ) --> Return largest item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
// ******************ERRORS********************************
// Throws UnderflowException as appropriate

/**
* Implements an unbalanced binary search tree.
* Note that all "matching" is based on the compareTo method.
* @author Mark Allen Weiss
*/
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
{
/**
* Construct the tree.
*/
public BinarySearchTree( )
{
root = null;
}

/**
* Insert into the tree; duplicates are ignored.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
root = insert( x, root );
}

/**
* Remove from the tree. Nothing is done if x is not found.
* @param x the item to remove.
*/
public void remove( AnyType x )
{
root = remove( x, root );
}

/**
* Find the smallest item in the tree.
* @return smallest item or null if empty.
*/
public AnyType findMin( )
{
if( isEmpty( ) )
throw new UnderflowException( );
return findMin( root ).element;
}

/**
* Find the largest item in the tree.
* @return the largest item of null if empty.
*/
public AnyType findMax( )
{
if( isEmpty( ) )
throw new UnderflowException( );
return findMax( root ).element;
}

/**
* Find an item in the tree.
* @param x the item to search for.
* @return true if not found.
*/
public boolean contains( AnyType x )
{
return contains( x, root );
}

/**
* Make the tree logically empty.
*/
public void makeEmpty( )
{
root = null;
}

/**
* Test if the tree is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( )
{
return root == null;
}

/**
* Print the tree contents in sorted order.
*/
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}

/**
* Internal method to insert into a subtree.
* @param x the item to insert.
* @param t the node that roots the subtree.
* @return the new root of the subtree.
*/
private BinaryNode<AnyType> insert( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
return new BinaryNode<AnyType>( x, null, null );
  
int compareResult = x.compareTo( t.element );
  
if( compareResult < 0 )
t.left = insert( x, t.left );
else if( compareResult > 0 )
t.right = insert( x, t.right );
else
; // Duplicate; do nothing
return t;
}

/**
* Internal method to remove from a subtree.
* @param x the item to remove.
* @param t the node that roots the subtree.
* @return the new root of the subtree.
*/
private BinaryNode<AnyType> remove( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
return t; // Item not found; do nothing
  
int compareResult = x.compareTo( t.element );
  
if( compareResult < 0 )
t.left = remove( x, t.left );
else if( compareResult > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) // Two children
{
t.element = findMin( t.right ).element;
t.right = remove( t.element, t.right );
}
else
t = ( t.left != null ) ? t.left : t.right;
return t;
}

/**
* Internal method to find the smallest item in a subtree.
* @param t the node that roots the subtree.
* @return node containing the smallest item.
*/
private BinaryNode<AnyType> findMin( BinaryNode<AnyType> t )
{
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}

/**
* Internal method to find the largest item in a subtree.
* @param t the node that roots the subtree.
* @return node containing the largest item.
*/
private BinaryNode<AnyType> findMax( BinaryNode<AnyType> t )
{
if( t != null )
while( t.right != null )
t = t.right;

return t;
}

/**
* Internal method to find an item in a subtree.
* @param x is item to search for.
* @param t the node that roots the subtree.
* @return node containing the matched item.
*/
private boolean contains( AnyType x, BinaryNode<AnyType> t )
{
if( t == null )
return false;
  
int compareResult = x.compareTo( t.element );
  
if( compareResult < 0 )
return contains( x, t.left );
else if( compareResult > 0 )
return contains( x, t.right );
else
return true; // Match
}

/**
* Internal method to print a subtree in sorted order.
* @param t the node that roots the subtree.
*/
private void printTree( BinaryNode<AnyType> t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}

/**
* Internal method to compute height of a subtree.
* @param t the node that roots the subtree.
*/
private int height( BinaryNode<AnyType> t )
{
if( t == null )
return -1;
else
return 1 + Math.max( height( t.left ), height( t.right ) );
}
  
// Basic node stored in unbalanced binary search trees
private static class BinaryNode<AnyType>
{
// Constructors
BinaryNode( AnyType theElement )
{
this( theElement, null, null );
}

BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt )
{
element = theElement;
left = lt;
right = rt;
}

AnyType element; // The data in the node
BinaryNode<AnyType> left; // Left child
BinaryNode<AnyType> right; // Right child
}


/** The tree root. */
private BinaryNode<AnyType> root;


// Test program
public static void main( String [ ] args )
{
BinarySearchTree<Integer> t = new BinarySearchTree<Integer>( );
final int NUMS = 4000;
final int GAP = 37;

System.out.println( "Checking... (no more output means success)" );

for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
t.insert( i );

for( int i = 1; i < NUMS; i+= 2 )
t.remove( i );

if( NUMS < 40 )
t.printTree( );
if( t.findMin( ) != 2 || t.findMax( ) != NUMS - 2 )
System.out.println( "FindMin or FindMax error!" );

for( int i = 2; i < NUMS; i+=2 )
if( !t.contains( i ) )
System.out.println( "Find error1!" );

for( int i = 1; i < NUMS; i+=2 )
{
if( t.contains( i ) )
System.out.println( "Find error2!" );
}
}
}

Solutions

Expert Solution

Skewed Binary Tree is a tree which has either one child or no child. The structure is similar to the Linked-List data structure.

Balanced Binary Search Tree is a tree whose height is balanced at each node. This is similar to AVL tree. The depth difference in two sides of a node in not more than 1 in Balanced BST.

Since Skewed Binary tree is similar to Linked-list, it follows same time complexity as linked-list. Hence It's Searching time complexities are:
Best case: O(1).
Average case: O(n).
Worst case: O(n)

Now, since Balanced BST is similar to AVL, while searching, at any instance, one of the two subtree of a node is traversed skipping the other subtree of nearly same size. Hence, it's time complexity is:
Best case: O(1).
Average case: O(log2n).
Worst case: O(log2n)

NOTE: Copy only the average case time complexity in the solution please. No need to copy other time complexity. It is for your knowledge only.

Henceforth, Searching in Balanced BST is far more better than that in Skewed Binary Tree.

Hope it helps.


Related Solutions

Question 15: Use the Master theorem to find the asymptotic complexity of the running time function...
Question 15: Use the Master theorem to find the asymptotic complexity of the running time function T(n) of the program in problem 14. --- Problem 14 --- def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ***Big-O, Omega, Theta complexity...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
An OBST (optimal BST) minimizes the average search time across all keys in the BST. Given...
An OBST (optimal BST) minimizes the average search time across all keys in the BST. Given 5 ordered keys. k1<k2<k3<k4<k5, with probabilities of occurrence (0.25, 0.15, 0.10, 0.20, 0.30), respectively Use a Greedy algorithm that attempts to construct a BST that is an OBST. What is the complexity of your Greedy algorithm?   Is your Greedy BST an OBST? Explain apply the DP algorithm to acquire an OBST show your work, including tables and the extraction of the actual OBST analyze...
For the problem below, please estimate the worst-case Running Time for a random array of size...
For the problem below, please estimate the worst-case Running Time for a random array of size n, and prove that it is indeed ( ). Please show all your work. Just stating something like "since 2 Binary Search runs in ( ) time, our algorithm has the same runtime estimate" is not enough. A rigorous explanation is expected. import java.util.*; public class Main { static int binarySearch(int arr[], int l, int r, int x) { if (r >= l) {...
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to...
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l) #1 def merge(): l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] m = [15, 16, 17, 18] lm = [] for l_i in l: lm.append(l_i) for m_i in m:...
estimate the median and mode, is the this distribution symmetrical or skewed? Frequency Distribution: 1.5 to...
estimate the median and mode, is the this distribution symmetrical or skewed? Frequency Distribution: 1.5 to 4.5    3 4.5 to 7.5    5 7.5 to 10.5    9 10.5 to 13.5    5 13.5 to 16.5    3
A government is running a balanced budget. An election is approaching and the government decides on...
A government is running a balanced budget. An election is approaching and the government decides on a one-time, temporary, massive tax cut that will cut tax revenue by $50 billion in one year; after the year is over, tax rates and tax revenue return to normal. The government decides to issue perpetual bonds of $50 billion to cover the cost of the tax cut. The interest rate on these bonds is constant at 6%. The tax to pay the interest...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
prove the running time of heap sort.
prove the running time of heap sort.
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem,...
***Only Complete the Bolded Part of the Question*** Complete the asymptotic time complexity using Master theorem, then use the "Elimination Method" to validate your solution. Clue for Last question: you need to use the elimination method to obtain analytical TC first. 1. T(n)= 4T(n/3) + n lg n 2. T(n)= 3T(n/3) + n / lg n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT