Data Structures Brett Bernstein

Similar documents
Lecture 13: AVL Trees and Binary Heaps

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

CS61BL. Lecture 5: Graphs Sorting

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

CS521 \ Notes for the Final Exam

March 20/2003 Jayakanth Srinivasan,

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

) $ f ( n) " %( g( n)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Course Review for Finals. Cpt S 223 Fall 2008

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.


n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

Total Score /1 /20 /41 /15 /23 Grader

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Trees. (Trees) Data Structures and Programming Spring / 28

Binary Trees, Binary Search Trees

CMPT 225. Binary Search Trees

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

n 2 ( ) ( ) + n is in Θ n logn

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Lecture Summary CSC 263H. August 5, 2016

2. (a) Explain when the Quick sort is preferred to merge sort and vice-versa.

COMP : Trees. COMP20012 Trees 219

Priority queues. Priority queues. Priority queue operations

Recursion, Binary Trees, and Heaps February 18

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

INF2220: algorithms and data structures Series 1

Analysis of Algorithms

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

Priority Queues. 04/10/03 Lecture 22 1

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

( ) 1 B. 1. Suppose f x

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

CS 310 Advanced Data Structures and Algorithms

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

22 Elementary Graph Algorithms. There are two standard ways to represent a

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

( ) n 3. n 2 ( ) D. Ο

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Trees Algorhyme by Radia Perlman

COMP 251 Winter 2017 Online quizzes with answers

Trees. A tree is a directed graph with the property

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Undirected Graphs. Hwansoo Han

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Introduction to Algorithms I

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Recitation 9. Prelim Review

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

CS24 Week 8 Lecture 1

Second Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Problem Score Maximum MC 34 (25/17) = 50 Total 100

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY THIRD SEMESTER B.TECH DEGREE EXAMINATION, JULY 2017 CS205: DATA STRUCTURES (CS, IT)

Design and Analysis of Algorithms - - Assessment

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Basic Data Structures (Version 7) Name:

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

( D. Θ n. ( ) f n ( ) D. Ο%

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

CSE 100: GRAPH ALGORITHMS

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

22 Elementary Graph Algorithms. There are two standard ways to represent a

Programming II (CS300)

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Stores a collection of elements each with an associated key value

MLR Institute of Technology

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Transcription:

Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of nodes?. Consider the following binary tree node class: TreeNode.java public class TreeNode<T> { private T value; private TreeNode<T> left, right; //Children //private TreeNode<T> parent; //optional public TreeNode(T t, TreeNode<T> l, TreeNode<T> r) { value = t; left = l; right = r; } } public T getvalue() { return value; } public void setvalue(t t) { value = t; } public TreeNode<T> getleft() { return left; } public TreeNode<T> getright() { return right; } public void setleft(treenode<t> t) { left = t; } public void setright(treenode<t> t) { right = t; } (a) Write a method that gets the size of the tree. public static <T> int size(treenode<t> root) (b) Write a method that gets the number of leaves. public static <T> int numleaves(treenode<t> root) (c) ( ) Write a method that prints all paths from the root to the leaves (each path on a separate line) public static <T> void allpaths(treenode<t> root) 1

(You will need a helper function.) (d) ( ) Write a method that determines if the given tree is a BST public static boolean isbst(treenode<integer> root) (You will need a helper function.) 3. Consider the following binary tree: (a) Give a preorder traversal of the tree. 5 7 9 1 9 1 (b) Give a postorder traversal of the tree. (c) Give an inorder traversal of the tree. (d) Give a level-by-level traversal of the tree. (e) If we stored the above tree the same way we store heaps, what would the resulting storage look like?. ( ) Describe how to find the closest (lowest) common ancestor given two nodes in a binary tree (assume each node has a reference to its parent). 5. Using the TreeNode class above, implement a function that prints the nodes out in level-by-level order. 6. Evaluate each of the following postfix expressions, or state that they are invalid. (a) 1 + 1 3 - (b) 1 3 - - - - (c) 3 + + 5 + (d) 3 * 1 + * 7. Explain how to evaluate a postfix expression. 8. You are given a list of precious stones whose values are given in an array values. (a) Suppose the array has values 1,,3. Draw a tree that shows all possible ways of choosing subsets of the stones, and note the sum of the values in the subset for each subset. (b) ( ) Write a recursive function that counts how many subsets of the stones have value that adds up to v. (You will need a helper function.)

public static int numways(int[] values, int v) 9. Consider the following node class used for storing an arbitrary arity tree (i.e., not just binary). ListTreeNode.java public class ListTreeNode<T> { private T value; private ListTreeNode<T> firstchild; private ListTreeNode<T> nextsibling; public ListTreeNode(T v, ListTreeNode<T> f, ListTreeNode<T> n) { value = v; firstchild = f; nextsibling = n; } public T getvalue() { return value; } public void setvalue(t t) { value = t; } public ListTreeNode<T> getfirstchild() { return firstchild; } public ListTreeNode<T> getnextsibling() { return nextsibling; } public void setfirstchild(listtreenode<t> f) { firstchild = f; } public void setnextsibling(listtreenode<t> n) { nextsibling = n; } } (a) Write a method that gets the size of the tree. public static <T> int size(listtreenode<t> root) (b) Write a method that gets the number of leaves. public static <T> int numleaves(listtreenode<t> root) 10. Consider the following binary search tree. 10 5 15 1 1 18 16 17 Perform the following operations in order. (a) Remove 15. 3

(b) Remove 10. (c) Add 13. (d) Add 8. 11. A (a) Label above (with a B ) the node that is the successor of A. (b) Label above (with a C ) the node that is the successor of B. (c) What is the balance factor of the root? 1. Give all possible binary search trees for the values 1,,3. 13. Implement find for a BST. 1. Given a sorted array of distinct integers, explain how to create a BST of minimal height. 15. Start with an empty AVL tree and perform the following operations in order. (a) Add 1,3,5,,6. (b) Remove 1. 16. For each, give the worst-case runtime for a tree with n nodes. (a) Find an element in a BST. (b) Find an element to an AVL Tree. (c) Add an element to an AVL Tree. (d) Output the minimum element of an AVL Tree. (e) Output the minimum element of a min heap. (f) Output the minimum element of a max heap. (g) Add an element to a max heap. (h) Turn an array into a heap.

17. Consider the following min heap. 5 9 6 1 11 7 8 15 16 1 15 (a) What is the index of 11 in the corresponding array? (b) Add an 8 to the min heap. (c) Then add a 1 to the min heap. (d) Then removemin from the min heap. (e) Then removemin from the min heap. 18. Explain in detail how sift-down works on a min heap. 19. Show how to get the 5th smallest element of an array in Θ(n) time. 0. Consider the array {3,, 1, 5, }: (a) How many inversions are in the array? (b) Show what steps insertion sort performs on the array. (c) Show what steps bubble sort performs on the array. (d) Show what steps merge sort performs on the array. (e) Partition the array around 3 using the minimum number of swaps possible. 1. Implement merge, which merges two sorted lists.. Implement insertion sort. 3. ( ) Assuming you have access to a function that partitions (as in quick sort), show how to find the kth smallest element in expected linear time.. Given two sorted arrays A 1 and A, each containing distinct values, compute the number of values they have in common. public static int numshared(int[] a1, int[] a) 5. For each of the following, give the best and worst-case runtimes of the following: (a) Bubble sort of an array of size n. (b) Insertion sort of an array of size n. 5

(c) Quick sort of an array of size n (using the first element as pivot choice). 6. Consider an array where all of the values are ints between 0 and 100, inclusive. Describe how to determine the number of distinct values in Θ(n) time. 7. Name different methods of sorting with worst-case runtime Θ(n lg n). 8. Consider the following graph: 3 0 1 5 6 7 11 1 10 8 15 13 9 1 (a) How many connected components are there? (b) Give the degree of each vertex? (c) What is the length of the longest path? (d) How many of the components are trees? (e) What is the least number of edges that must be removed to yield a forest? (f) What is the most number of edges that can be added (excluding multiple edges) such that the number of components in the graph doesn t change? 9. Define a tree. 30. For the graph below, show how it would be stored in an adjacency matrix (for both edges and weights) and in an adjacency list (for both edges and weights). 6

0 1 3 31. What are the worst-case space requirements for an adjacency matrix and an adjacency list. 3. Give the worst-case runtimes of the following operations for an adjacency matrix and an adjacency list. (a) Determine if there is an edge from i to j. (b) Output all of the edges of the graph. (c) List the neighbors of a vertex v (i.e., the vertices w such that there is an edge from v to w). (d) List all vertices that have v as a neighbor (i.e., the vertices w such that there is an edge from w to v). (e) Output the degrees of every vertex. 33. (a) What is the worst-case runtime of DFS on an adjacency list? (b) What is the worst-case runtime of DFS on an adjacency matrix? (c) What is the runtime of Dijkstra s algorithm? (d) What is the runtime of Prim s algorithm? 3. Consider the following graph. 7

1 0 1 6 8 6 3 9 Below assume lowered numbered vertices are preferred in case of a tie. (a) Show how Prim s algorithm finds the minimum spanning tree starting from vertex. In case of a tie, prefer lower-numbered vertices. (b) Compute the distance of getting from vertex to all other vertices using Dijkstra s algorithm. In case of a tie, prefer lower-numbered vertices. 35. Implement DFS for an adjacency list. 36. Implement BFS for an adjacency list. 37. Explain what algorithm you would use to solve the following problems. Assume the graph is weighted, undirected, and has no negatively weighted edges. In each case give the worst-case runtime. Try to be as efficient as possible. 38. (a) Compute the shortest path between vertices v and w. (b) Compute the two distinct vertices that are closest. (c) Compute the two distinct vertices that are furthest apart (they determine the diameter). (d) Assuming the graph is also connected, compute the minimum cost tree that uses all of the vertices and some of the edges of the graph. (e) Detect if the graph is acyclic. (f) Count the number of connected components. 8

0 5 1 3 6 7 Assume below that in case of a tie, lower numbered vertices are preferred. (a) Give the order in which DFS visits the vertices. (b) Give the order in which BFS visits the vertices. 39. Implement the following functions. (a) Return an array of the degrees of each vertex in an undirected graph: static int[] degrees(arraylist<arraylist<integer>> adj) (b) Return the largest vertex index reachable from v in a directed graph: static int largest(arraylist<arraylist<integer>> adj, int v) 0. Given an adjacency list for a DAG, give code to find the number of steps in the longest path. You may create an array outside of the function to store needed information. static int longest(arraylist<arraylist<integer>> adj) 9