© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Final Review.

Slides:



Advertisements
Similar presentations
Lecture 22, Revision 1 Lecture notes Java code – CodeFromLectures folder* Example class sheets – 4 of these plus solutions Extra examples (quicksort) Lab.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advanced Data Structures
Review. What to know You are responsible for all material covered in lecture, the readings, or the programming assignments There will also be some questions.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
CSCE 210 Data Structures and Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Midterm Review.
Final Exam Preview Chapter 4,5,6,7,8,9. Remember to evaluate CS221  Go to  Ends tonight.
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Marc Smith and Jim Ten Eyck
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Review – Part 1 CSE 2011 Winter September 2015.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
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,
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Information and Computer Sciences University of Hawaii, Manoa
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Final Review Dr. Yingwu Zhu. Goals Use appropriate data structures to solve real- world problems –E.g., use stack to implement non-recursive BST traversal,
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Today: Office hours –No tomorrow. M/T- 3-on. Overloading operators Inheritance Review.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
CS342 Data Structures End-of-semester Review S2002.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
April 27, 2017 COSC Data Structures I Review & Final Exam
 Saturday, April 20, 8:30-11:00am in B9201  Similar in style to written midterm exam  May include (a little) coding on paper  About 1.5 times as long.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
Final Exam Review CS 3358.
CSCE 210 Data Structures and Algorithms
Chapter 12 – Data Structures
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Hashing Exercises.
Cse 373 April 26th – Exam Review.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
CSE 326: Data Structures: Midterm Review
Data Structures and Algorithms
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 CMPT 225 Final Review

© 2006 Pearson Addison-Wesley. All rights reserved8 A-2 Time and Place Exam is at 7:00-10:00pm on April 10 Room: HCC 1315 Exception... for the 3 students with a conflict in Burnaby, the room will be AQ4120

© 2006 Pearson Addison-Wesley. All rights reserved8 A-3 Topics Covered (before midterm) Java review Software Engineering Recursion –(and a little algorithm efficiency) Abstract Data Types Linked Lists Stacks Queues

© 2006 Pearson Addison-Wesley. All rights reserved8 A-4 Topics Covered (after midterm) Java Interfaces and Generics Trees Tables and Priority Queues Hashing Graphs Sorting

© 2006 Pearson Addison-Wesley. All rights reserved8 A-5 Topics Covered Roughly: chapters 1-14 Minus chapter 6... though we did some of it in class We didn’t read the second half of ch. 7, but we covered it in class Didn’t read first half of ch. 10, but we covered the material in class

© 2006 Pearson Addison-Wesley. All rights reserved8 A-6 Java Review You should be able to read Java code –You don’t need to memorize a bunch of java.util classes You should be able to write Java code –Defining your own classes, interfaces, methods, etc.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-7 Software Engineering What is a good solution to a problem? Object oriented design –Read a UML diagram –Write a UML diagram Modularity, modifiability, readability, ease of use, fail-safe programming…

© 2006 Pearson Addison-Wesley. All rights reserved8 A-8 Recursion Write recurrence relations –e.g. rabbits(n) = rabbits(n-1) + rabbits(n-2) Write recursive (Java) methods Solve problems with simple recursive solutions Backtracking Relation with induction

© 2006 Pearson Addison-Wesley. All rights reserved8 A-9 Algorithm Efficiency Big-O notation: –What does O(n) mean? –What about O(n 2 )? Count the steps of an algorithm, translate to big-O

© 2006 Pearson Addison-Wesley. All rights reserved8 A-10 Abstract Data Types Difference: –ADT vs. data structure How do you specify ADTs? Designing the right ADT for a problem ADT List and SortedList

© 2006 Pearson Addison-Wesley. All rights reserved8 A-11 Linked Lists Object references Using nodes and references Programming with linked lists –inserting, deleting, traversing Variations –tail references, circular lists, doubly linked

© 2006 Pearson Addison-Wesley. All rights reserved8 A-12 Stacks ADT Stack –LIFO Implementations Problem solving with Stacks

© 2006 Pearson Addison-Wesley. All rights reserved8 A-13 Queues ADT Queue –FIFO Implementations Problem solving with Queues

© 2006 Pearson Addison-Wesley. All rights reserved8 A-14 Java Generics What is a generic class? Why do you use them? How do you define one?

© 2006 Pearson Addison-Wesley. All rights reserved8 A-15 Trees General trees Binary trees Binary search trees Traversals (in-order, pre-order, post-order) Implementations Efficiency of search

© 2006 Pearson Addison-Wesley. All rights reserved8 A-16 Tables Basic implementations –array, reference Faster implementations –binary search tree –2-3 trees –red-black trees –hash functions

© 2006 Pearson Addison-Wesley. All rights reserved8 A-17 Hashing What is a good has function? What is a collision? Collision resolution

© 2006 Pearson Addison-Wesley. All rights reserved8 A-18 Graphs Definitions and basic properties Implementations –adjacency list –adjacency matrix Traversals and spanning trees –depth-first –breadth-first

© 2006 Pearson Addison-Wesley. All rights reserved8 A-19 Sorting Quadratic sorts: –Selection, insertion, bubble Log linear sorts: –merge, quick Special case: radix Should be able to explain/sketch/understand different sorts

© 2006 Pearson Addison-Wesley. All rights reserved8 A-20 Example 1 What can go wrong with this method? How can you protect yourself? public static double computation(double x) { return java.lang.Math.sqrt(x) / java.lang.Math.cos(x); }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-21 Example 1 - Solution Two hazards: 1)the argument x cannot be a negative number because computation() calls the sqrt() method with x as the argument. 2)since zero is in the range of the method cos(x), the result of this computation must be tested prior to using it as a divisor in order to avoid a "Divide by Zero error".

© 2006 Pearson Addison-Wesley. All rights reserved8 A-22 Example 1 – Solution double computation( double x ) { double denominator = Math.cos(x); double result = 0; if ( x < 0 ) {System.out.println("Cannot take square root of negative number"); result = -1; } if ( denominator == 0 ) {System.out.println("Cannot divide by zero"); result = -1; } if ( result != -1 ) result = Math.sqrt(x)/denominator; return result; }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-23 Example 2 Write the loops invariant for this: void printN(int n) { int i = 20; while(i>1) i--; }

© 2006 Pearson Addison-Wesley. All rights reserved8 A-24 Example 2 - Solution 20 >= i >= 1

© 2006 Pearson Addison-Wesley. All rights reserved8 A-25 Example 3 Write a recursive method that will compute the sum of the first n integers in an array of at least n integers.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-26 Example 3 - Solution int computeSum(final int a[], int n) { // base case if(n <= 0) return 0; else // reduce the problem size return a[n - 1] + computeSum(a, n - 1); } // end computeSum

© 2006 Pearson Addison-Wesley. All rights reserved8 A-27 Example 4 Write a recursive Java method that writes the digits of a positive decimal integer in reverse order.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-28 Example 4 - Solution void reverseDigits(int number){ if(number >= 0) // check for input bounds { // base case if(number < 10) System.out.print(number); else { // print out rightmost digit System.out.print(number % 10); // pass remainder of digits to next call reverseDigits(number / 10); } // end if } // end reverseDigits

© 2006 Pearson Addison-Wesley. All rights reserved8 A-29 Example 5 Implement a method “swap” for lists... use the ADT list, and handle the exception if the positions being swapped do not exist.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-30 Example 5 - Solution void swap(List aList, int i, int j) { Object first, second; try{ first = aList.get(i); second = aList.get(j); } catch(Exception e){ System.out.println(e); } aList.remove(i); aList.add(i, second); aList.remove(j); aList.add(j, first); } // end Swap

© 2006 Pearson Addison-Wesley. All rights reserved8 A-31 Example 6 Trace the selection sort as it sorts the following array: 7, 12, 24, 4, 19, 32

© 2006 Pearson Addison-Wesley. All rights reserved8 A-32 Example 6 - Solution

© 2006 Pearson Addison-Wesley. All rights reserved8 A-33 Example 7 Draw the complete binary tree obtained by inserting the following values in the given order: 4, 13, 5, 3, 7, 30

© 2006 Pearson Addison-Wesley. All rights reserved8 A-34 Example 7 - Solution 4 / \ 13 5 / \ /

© 2006 Pearson Addison-Wesley. All rights reserved8 A-35 Example 8 Draw the binary search tree obtained by inserting the following values in the given order: 4, 13, 5, 3, 7, 30

© 2006 Pearson Addison-Wesley. All rights reserved8 A-36 Example 7 - Solution 4 / \ 3 13 / \ 5 30 \ 7

© 2006 Pearson Addison-Wesley. All rights reserved8 A-37 Example 9 A general tree can be implemented by having an array of child references. 1) What are the advantages and disadvantages of this implementation? 2) Write a recursive preorder traversal method for a general tree.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-38 Example 9 - Solution Advantages: Access to the children is easy; direct access to each child is available. Disadvantages: Limits the number of children to a fixed maximum. If the maximum number of children possible is higher than the average number of children, then memory is wasted. If the children are kept in order, insertions and deletions may require shifting pointers in the array.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-39 Example 9 - Solution public void traverse(TreeNode tNode); {if (tNode != null) { queue.enqueue(tNode.getItem()); for (int i=0; i<tNode.numChildren; ++i) traverse(tNode.child[i]); } // end if } // end traverse

© 2006 Pearson Addison-Wesley. All rights reserved8 A-40 Example 10 What are the advantages of implementing ADT Table with a 2-3 tree instead of a binary search tree?

© 2006 Pearson Addison-Wesley. All rights reserved8 A-41 Example 10- Solution The height of a binary search tree depends on the order in which the items are inserted. Advantage: In a tree with N items, the height can range between N (equivalent to a linked list) and log2 ( N+1) (a fully balanced tree). A 2-3 tree is always balanced and thus has height proportional to log N. So, a 2-3 tree is a more efficient ADT table implementation than a binary search tree. Advantage: Maintaining a balanced binary search tree may become very expensive in the face of frequent inserts and deletions as the entire tree must be rebuilt in the worst case.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-42 Example 11 Write pseudocode for the tableDelete operation when the implementation uses a hash-table with seperate chaining.

© 2006 Pearson Addison-Wesley. All rights reserved8 A-43 Example 11 - Solution tableDelete(Object item) hashval = h(item.searchKey) let LL = the linked list at table[hashval] LL.delete(item)

© 2006 Pearson Addison-Wesley. All rights reserved8 A-44 Example 12 Briefly explain/define these terms: –2-3 tree –hash function –adjacency list

© 2006 Pearson Addison-Wesley. All rights reserved8 A-45 Example 12 Briefly explain/define these terms: –2-3 tree tree where each node contains either 1 or 2 data items. nodes containing 1 data item have 2 children, nodes containing 2 data items have 3. –hash function a function that maps a search key to a position in an array, to implement an ADT table with efficient search/retrieval –adjacency list a way to implement the ADT graph in which the nodes adjacent to each node are stored in a linked list

© 2006 Pearson Addison-Wesley. All rights reserved8 A-46 Example 13 Given the following running times for input n, what is the big-O measure of efficiency –n –n (log n) + n(n ) –log n n

© 2006 Pearson Addison-Wesley. All rights reserved8 A-47 Example 13 - Solution Given the following running times for input n, what is the big-O measure of efficiency –n O(n) –n (log n) + n(n ) O(n 3 ) –log n n O(n)

© 2006 Pearson Addison-Wesley. All rights reserved8 A-48 Example 14 Which ADT is appropriate for each type of data: –A pile of shoeboxes in a warehouse –The World Wide Web –A collection of employees at a company

© 2006 Pearson Addison-Wesley. All rights reserved8 A-49 Example 14 - Solution Which ADT is appropriate for each type of data: –STACK –DIRECTED GRAPH –TABLE

© 2006 Pearson Addison-Wesley. All rights reserved8 A-50 Some more notes The final will look a lot like the midterm.... same kinds of questions Some people on the midterm seemed to have trouble with recursion... it will be back There will be no cheat sheet of ADT operations this time

© 2006 Pearson Addison-Wesley. All rights reserved8 A-51 More types of examples Displaying contents of a Queue or Stack Writing data structure for an ADT... say the Bag from earlier... need to decide data members Inserting nodes in doubly linked/circular linked lists