You are on page 1of 5

ITWB2043 Algorithms and Data Structures

Assignment Guidelines and Requirements

Assignment 1 – 16%
Start: Week 3
Due Date: Week 7

Answer ALL questions.


1. Write the order of each method below, using big-Oh. (5 marks)

Method Order
(big-Oh)
a) public void add(int i, Object e){ e
Node newNode = new Node(e, null);
if(size==0){ O(i)
head=newNode;
tail = newNode;
return;
}
int k=0;
Node node = this.head;
while(k<i){
node=node.next;
k++;
}
if(k==0){ //Add element e to the beginning of the list
newNode.next=head;
head = newNode;
} if( k == zise){ //Add element e to the end of the list
node.next = newNode;
tail = newNode;
}else {
newNode.next=node.next;
node.next=newNode;
}
size++;
return;
}

b) public Object removeLast(){


if(size==0) return null;
Object retE = tail.element; O(n)
Node node1 = head;
Node node2 = null;
while(node1.next!=null){
node2 = node1;
node1=node1.next;
}
if(node2 == null)
head=tail=null;
else {
node2.next=null;
tail = node2;
}
size--;
return retE;
}

c) pubic void reverse(){ T


if(size<=1) return;
O(n)
Node node1=head;
tail =head;

1
Node node2 = node1.next;
node1.next=null;
while(node2!=null){
Node node3 = node2.next;
node2.next=node1;
node1=node2;
node2=node3;
}
head = node1;
}

d) void func_a(int arr[], int n) //n=size of arr


{ O(1)
System.out.println( "Item 0 = " + arr[0] );
System.out.println( "Item 1 = " + arr[1] );
System.out.println( "Item 2 = " + arr[2] );
}

e) void func_b(int arr[], int n) //n=size of arr


{
for (int a = 0; a < n; ++a) O(n^2)
for (int b = 0; b < a; ++b)
for (int c = 0; c < b; ++c)
arr[c] += arr[b];
}

2
2. Based on the following algorithm:

int total1 = 0;
int total2 = 0;
for (int x = 0; x <= n; x++)
total1 = total1 + x;
for (int y = 1; y < n; y++)
total2 += total1 * y;

Show the steps to find the total operations of the algorithm given by fill in the table given.
Then, define the value of Big Oh in its worse-case. (5 mark)

Operations Number of operations

Assignment 2

Addition n+n-1

Multiplication n-1

Total Operation 3n

O(n)
The big Oh is = ________________ (1mark)

3
3. Suppose that a class, Employee, is defined as follows:

class Employee {
String lastName;
String firstName;
double hourlyWage;
int yearsWithCompany;
}

Assume that data about 100 employees is already stored in an array:

Employee[] employeeData = new Employee[100];

Write a code segment that will output the first name, last name, and hourly wage of each
employee who has been with the company for 20 years or more.

Answer (10 marks)

for (Employee employee : employeeData) {


if (employee != null && employee.yearsWithCompany >= 20) {
System.out.println("First Name: " + employee.firstName);
System.out.println("Last Name: " + employee.lastName);
System.out.println("Hourly Wage: " + employee.hourlyWage);
System.out.println("---------------------------");
}
}

4. What tree results when you add the values 44, 88, 55, 77, 33, 99, 66, 22, 25 and 75 to each of the
following initially empty trees?
a) Binary Search Tree (1mark)

b) Based on the answer in question of 1(a):


i) delete node ‘55’ and redraw the tree. (1mark)

ii) delete node ‘44’ (after deleting node 55) and redraw the tree.
(1mark)
c) AVL tree (1 mark)

d) B-Tree (m=3) (1mark)

(For each question, show only the final result.)

a) b) i) b) ii)

c) d)

/ \

/ \ / | \

4
5. Based on this following binary tree :

Pahang

Johor Kedah

Kelanta Sabah

a) Assign the correct values to an array according to a sequential index Perak


numbers. (1mark)

b) Identify type of traversal if the result is

i) Kelantan Johor Perak Sabah Kedah Pahang


(1mark)

ii) Pahang Johor Kedah Kelantan Sabah Perak


(1mark)

6. Given this arithmetic expression.


M – (P + Q) / S * T * U

a) Draw an expression tree. (1 mark)

b) Traverse your expression tree above to get the


i) Prefix expression (1mark)

ii) Postfix expression (1mark)

5.

a) Index |Node Value b) i) 1.Visit the current node.


--------------------------- 2.Traverse the left subtree.
0 |Pahang 3.Traverse the right subtree.
1 |Johor
2 |Kedah b) ii) 1.Traverse the left subtree.
3 |Kelantan 2.Visit the current node.
4 |Sabah 3.Traverse the right subtree.
5 |Perak

6.

a) b) i) - M * / + P Q * * S T U

b) ii) M P Q + S / T U * * -

You might also like