Assignment 3
Assignment 3
York University
AP/ITEC 2620 3.0
INTRODUCTION TO DATA STRUCTURES
Assignment
Prof. S. Chen
Surname: ___________________________________
Given Names: ___________________________________
Student Number: ___________________________________
1
2
3
Total
Page 2 of 5
Question 1 (15 marks) Short Answer (maximum 20 words):
Answer all five parts below.
Part A (3 marks): What is the worst case time complexity for binary search on a sorted array
with n elements? Explain.
Part B (3 marks): The first time you run algorithm A on a dataset of n elements; it is faster than
algorithm B. The second time you run algorithm A on a dataset of n elements; it is slower than
algorithm B. Explain how this is possible. Give an example for algorithm A and algorithm B.
Part C (3 marks): What is the best case time complexity to insert n nodes into a linked list?
Assume that the linked list is initially empty and that it will be sorted from smallest to largest.
Explain.
Part D (3 marks): What is the time complexity to delete the root of a minimum-level BST with
n nodes? Explain.
Part E (3 marks): What is the shape of a BST that is O(n) to delete the root? Draw an example
with at least 8 nodes.
Page 3 of 5
Question 2 (10 marks) Complexity Analysis/Estimation:
What is the complexity of the given code as a function of the problem size n? Show the details
of your analysis.
f or ( i nt i = 0; i < 2*n; i ++)
{
i f ( i == n)
{
f or ( i nt j = 0; j < i ; j ++)
f or ( i nt k = 0; k < i ; k++)
O( 1)
}
el se
{
f or ( i nt j = 0; j < i ; j ++)
O( 1)
}
}
Page 4 of 5
Question 3 (10 marks) Recursion:
Write a recursive function that will calculate the height of a binary tree.
Note: root1 and root2 are instances of the class BinNode:
publ i c cl ass Bi nNode
{
publ i c char val ue;
publ i c Bi nNode l ef t ;
publ i c Bi nNode r i ght ;
}
Thus, the following statements would lead to the underlined output:
Example 1:
System.out.println( treeHeight (root1) );
3
Example 2:
System.out.println( treeHeight (root2) );
1
Please write your method on the following page.
D
B
A
E
C
root1
root2
Page 5 of 5
publ i c st at i c i nt t r eeHei ght ( Bi nNode cur r ent )
{
}