230 likes | 356 Views
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.
E N D
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.
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.
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.
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
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.
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.
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;
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.
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; }
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; }
Analysis: • Best case: n primitive operations (S2) • Worst case: 2n primitive operations • In Big_O: O(n)
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; } }
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; } }
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.
(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)); } }
1 2 3 4 5 6
(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.
(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.
// method in BinaryTree public int height(){ if(root != null) return root.height(); else return 0; }
// 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) }
(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.
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).