cis*2420 - quiz 2

23
CIS*2420 - Quiz 2

Upload: dagan

Post on 21-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

CIS*2420 - Quiz 2. For a large array, and in the worst case, selection sort is faster than insertion sort. False Both selection sort and insertion sort take O(N 2 ) time in the worse case. In a binary tree, the number of internal nodes = the number of external nodes + 1. False. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CIS*2420 - Quiz 2

CIS*2420 - Quiz 2

Page 2: CIS*2420 - Quiz 2

For a large array, and in the worst case, selection sort is faster than insertion sort.

False Both selection sort and insertion sort take O(N2) time in the worse case.

Page 3: CIS*2420 - Quiz 2

In a binary tree, the number of internal nodes = the number of external nodes + 1.

False. On the contrary, in a binary tree, the number of internal nodes + 1 = the number of external nodes.

Page 4: CIS*2420 - Quiz 2

In the worst case, the running time of traversing elements organized as a Binary Trees is O(log n), where n is the number of elements. This is because the height of the binary tree < # internal nodes = (# nodes - 1)/2.

False. The running time of traversing an N-node Binary tree takes at least O(N) time because we have to visit N nodes in any case.

Page 5: CIS*2420 - Quiz 2

The following sequence of values is a valid representation of a heap stored as an array.

Yes.

3 6 5 9 8 10

3

8

6 5

109

Page 6: CIS*2420 - Quiz 2

Using the bottom-up algorithm to construct a heap from a sequence of elements requires n/2 upheap() operations.

Yes or No, (a typo in this question, should be downheap() instead of upheap() operation. n/2 downheap() operations are required for the bottom-up heap construction.

Page 7: CIS*2420 - Quiz 2

A Palindrome string is a string whose first half is the reverse of its second half. For example: aba is a Palindrome, aabaa is a Palindrome, aabbaa is a Palindrome, abaaba is a Palindrome, and abaabaaa is not a palindrome. Write a pseudo-code algorithm to check if a string aString is a palindrome string using exactly one Stack and one Queue ADTs. Note: you must use the Stack and the Queue in a meaningful way.

Page 8: CIS*2420 - Quiz 2

Algorithm: IsPalindrome(String aString)Input: a char string aStringOutput: a Boolean valueStack s;Queue q; for ( k 0; k < aString.length; k++) do

s.push(aString[k]);q.enqueue(aString[k]);

for ( k 0; k < aString.length; k++) doif (s.pop() != q.dequeue())

return false; return true;

Page 9: CIS*2420 - Quiz 2

Suppose that each row of an n n array A consists of 1's and 0's such that in any row of A, all the 1's come before any 0's in that row from left to right. Assuming A is already in memory, describe a method runningin O(n) time (not O(n2) time) for finding the row of A that contains the most 1's.

Page 10: CIS*2420 - Quiz 2

Solution 1:

boolean maxOnes(int A[][]){ int maxrow = 0; int i = 0, j = 0; while (i < A.length){

while(A[i][j] == 1 && j < A.length){j++;maxrow = i;

}i++;

} return maxrow;}

Page 11: CIS*2420 - Quiz 2

Solution 2:boolean maxOnes(int A[][]){ int maxrow = 0; int i = 0; j = 0; while (i < A.length && j < A.length){

if (A[i][j] == 1){maxrow = i;j++;

}else i++;

} return maxrow;}

Page 12: CIS*2420 - Quiz 2

Analysis:

Best case: n primitive operations (S2) Worst case: 2n primitive operations In Big_O: O(n)

Page 13: CIS*2420 - Quiz 2

Consider the following implementation of the classes BinaryTree and TreeNode:class BinaryTree { private TreeNode root; public BinaryTree(){ root = null; } public void insert(Object anObject){ TreeNode currentNode = root; TreeNode newNode = new TreeNode(null, anObject, null); if(currentNode != null){ while(currentNode.hasLeft() && currentNode.hasRight()){ currentNode = currentNode.getRight(); } if(currentNode.hasLeft()) currentNode.setRight(newNode); else currentNode.setLeft(newNode); }else root = newNode; }}

Page 14: CIS*2420 - Quiz 2

class TreeNode { private Object element; private TreeNode left; private TreeNode right;

public TreeNode(TreeNode ln,Object obj, TreeNode rn){ setElement(obj); setLeft(ln); setRight(rn); } public Object getElement(){ return element; } public boolean hasLeft(){ return (left != null); } public void setLeft(TreeNode ln){ left = ln; } public TreeNode getLeft(){ return left; } public boolean hasRight(){ return (right != null); } public void setRight(TreeNode rn){ right = rn; } public TreeNode getRight(){ return right; }}

Page 15: CIS*2420 - Quiz 2

The purpose of this implementation is to ensure that elements are added into the tree level-by-level from left to right such that the tree is complete. This idea is analogues to the structural property of heaps we studied in the lectures.

Page 16: CIS*2420 - Quiz 2

(1) Draw the content of BinaryTree aBinaryTree after the following insertion

import java.util.*;import java.io.*;class Test { public static void main(String[] args){ BinaryTree aBinaryTree = new BinaryTree(); aBinaryTree.show(); aBinaryTree.insert(new Integer(1)); aBinaryTree.insert(new Integer(2)); aBinaryTree.insert(new Integer(3)); aBinaryTree.insert(new Integer(4)); aBinaryTree.insert(new Integer(5)); aBinaryTree.insert(new Integer(6)); }}

Page 17: CIS*2420 - Quiz 2

1

5

2 3

6

4

Page 18: CIS*2420 - Quiz 2

(2) Does this algorithm implement the intended insertion operation? Explain why and how.

No! By using the insert() of the algorithm, the binary tree constructed is not a complete binary tree, instead, it is a degenerated binary tree with all odd numbered nodes (except the last one) being internal nodes while all the even numbered nodes being external nodes.

Page 19: CIS*2420 - Quiz 2

(3) Write the method height() defined on the class BinaryTree which returns the height of the BinaryTree.Hint: you may define any additional methods on the class TreeNode.

Page 20: CIS*2420 - Quiz 2

// method in BinaryTree public int height(){ if(root != null) return root.height(); else return 0; }

Page 21: CIS*2420 - Quiz 2

// method defined in TreeNode classpublic int height(){ int leftHeight = 0; int rightHeight = 0; if(left!=null) leftHeight = 1 + getLeft().height(); if(right != null) rightHeight = 1 +

getRight().height(); return (int) Math.max(leftHeight,

rightHeight) }

Page 22: CIS*2420 - Quiz 2

(4) According to your implementation of the method height(), express and explain the running time complexity of computing the height of a BinaryTree containing n nodes. Hint: be careful with worst-case analysis.

Page 23: CIS*2420 - Quiz 2

Analysis:

Without modifying the insert() method, the running time complexity of height() is always O(N), no matter the tree is balanced or not. However, if the insert() is modified as expected, we may correspondingly modify the height() method by a depth first visit of the nodes in left-most branch only, therefore, we could reach O(log n).