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.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Binary Trees CS 110: Data Structures and Algorithms First Semester,
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Trees-part1. Objectives Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right subtrees.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Binary Search Trees.
Heaps and Priority Queues
Representation of a Threaded Tree
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
BINARY TREE CSC248 – Data Structure.
CSC 143 Binary Search Trees.
1 Lecture 10 CS2013.
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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

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 running in O(n) time (not O(n 2 ) 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)); }

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