# CIS*2420 - Quiz 2 - PowerPoint PPT Presentation

CIS*2420 - Quiz 2

1 / 23
CIS*2420 - Quiz 2

## CIS*2420 - Quiz 2

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. CIS*2420 - Quiz 2

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.

3. 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.

4. 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.

5. The following sequence of values is a valid representation of a heap stored as an array. • Yes. 3 6 5 9 8 10 3 6 5 9 8 10

6. 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.

7. 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.

8. Algorithm: IsPalindrome(String aString) Input: a char string aString Output: a Boolean value Stack 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++) do if (s.pop() != q.dequeue()) return false; return true;

9. 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.

10. 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; }

11. 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; }

12. Analysis: • Best case: n primitive operations (S2) • Worst case: 2n primitive operations • In Big_O: O(n)

13. 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; } }

14. 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; } }

15. 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.

16. (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)); } }

17. 1 2 3 4 5 6

18. (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.

19. (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.

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

21. // method defined in TreeNode class public 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) }

22. (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.

23. 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).