Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview

Slides:



Advertisements
Similar presentations
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Advertisements

Sorting Gordon College 13.1 Some O(n2) Sorting Schemes
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
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,
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Final Exam Review CS 3358.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Heap Sort Example Qamar Abbas.
CMSC 341 Lecture 13 Leftist Heaps
ITEC 2620M Introduction to Data Structures
ADT Heap data structure
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.
Function and class templates
Description Given a linear collection of items x1, x2, x3,….,xn
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
A Kind of Binary Tree Usually Stored in an Array
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
A Robust Data Structure
Ch. 12 Tables and Priority Queues
Priority Queues & Heaps
Final Review Dr. Yingwu Zhu.
Adapted from instructor resource slides
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Sorting Dr. Yingwu Zhu.
DATA STRUCTURE.
EE 312 Final Exam Review.
Priority Queues (Heaps)
Searching/Sorting/Searching
Algorithmic complexity
Mark Redekopp David Kempe
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Presentation transcript:

Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview

Data Structures: Exam 3 Preview Lecture outline Announcements/reminders Program 6 (extra credit) posted; due 5/3 Will add test cases more details soon Q&A session to be Th 5/4; time/room TBD Final exam: F 5/5 3-6 PM, Ball 412 Course eval to be submitted at final exam Link to be posted on site; blank copies available outside office Today’s lecture: Exam 3 Preview 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Exam 3 notes Allowed one 8.5” x 11” double-sided sheet of notes No other notes, electronic devices (calculator, phone) Exam will last three hours but will be written for ~50 minutes Covers all lectures after Exam 2 except Lecture 35 (Wednesday) Question types (will provide more detailed outline once exam is written) Multiple choice Code reading Code writing 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Templates Templates allow us to write general functions/classes and specify the data type later template keyword indicates that what follows is a pattern, not a full definition Generic typename specified in angle brackets with typename (or class) keyword At least one templated parameter must be used Desired type automatically bound if calling function Desired type must be specified if declaring object 7/5/2019 Data Structures: Exam 3 Preview

Review: Function template example General form: template <typename type> function definition Rewriting swap with templates: template <typename T> void swap(T &v1, T &v2) { T temp = v1; v1 = v2; v2 = temp; } Calling swap: int x = 10; int y = 20; swap(x, y); 7/5/2019 Data Structures: Exam 3 Preview

Review: Class template example template <typename T> class AQueue { public: AQueue(); bool isEmpty(); void enqueue(T val); void dequeue(); T front(); void display(ostream &out); private: T Qarr[CAPACITY]; int Qfront, Qback; }; 7/5/2019 Data Structures: Exam 3 Preview

Review: Class template example: enqueue template <typename T> void AQueue<T>::enqueue(T val) { int newBack = (Qback + 1) % CAPACITY; if (newBack != Qfront) { Qarr[newBack] = val; Qback = newBack; } else cerr << "Queue is full--can't enqueue\n"; 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Recursion Some functions call other functions Recursive functions call themselves A recursive function has two parts Anchor / base case: function value is defined for one or more specific argument values Inductive / recursive case: function value for current argument value defined in terms of previously defined function values and/or arguments Common uses Graph/tree traversal (e.g., BSTs) Divide and conquer algorithms (e.g., quicksort) 7/5/2019 Data Structures: Exam 3 Preview

Review: Recursive example Recursive power function double power(double x, unsigned n){ if (n == 0) return 1.0; else return x * power(x, n – 1); } 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Strings String data type found in <string> library Concepts to work with strings Relational operators: ==, !=, <, >, <=, >= Character-by-character comparison using ASCII Concatenation: +, += Choosing single character: [], at() at() provides boundary checking Substrings: substr() function If s1 = “ECE 264”  s1.substr(0,3) = “ECE”  3 chars starting at position 0 s1.substr(4) = “ 264”  all chars from position 4 to end of string Checking string length: length(), empty() functions 7/5/2019 Data Structures: Exam 3 Preview

Review: Binary search tree Binary search tree: binary tree in which each node follows property value in left child of node ≤ value in node ≤ value in right child of node Source: https://commons.wikimedia.org/w/index.php?curid=488330 7/5/2019 Data Structures: Exam 3 Preview

Review: Binary Trees as Recursive Data Structures A binary tree is either empty … or Consists of a node called the root root has pointers to two disjoint binary (sub)trees called … right (sub)tree left (sub)tree Anchor Inductive step Which is either empty … or … Which is either empty … or … 7/5/2019 Data Structures: Exam 3 Preview

Review: BST iterative search bool BST::search(const DataType &item) { BNode *p = root; bool found = false; while (!found && p != NULL) { if (item < p->data) p = p->left; else if (item > p->data) p = p->right; else found = true; } return found; 7/5/2019 Data Structures: Exam 3 Preview

Review: BST recursive search template <typename DataType> bool BST<DataType>::search(const DataType &item) { return searchSub(root, item); } bool BST<DataType>::searchSub(BNode *subTree, const DataType &item) { if (subTree == NULL) return false; else if (item == subTree->data) return true; else if (item < subTree->data) return searchSub(subTree->left, item); else return searchSub(subTree->right, item); 7/5/2019 Data Structures: Exam 3 Preview

Review: BST insert/delete BST insertion Modified search—find correct position for new node, then insert New nodes always inserted as leaf node (in non-balanced BST) BST deletion: 3 cases to consider Leaf node: simply set parent->leaf ptr = NULL Node w/1 child: set parent->node ptr = parent->node->child Node w/2 children: copy immediate successor to node to be deleted, then delete immediate successor Immediate successor: go R once, then L as far as possible 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heaps A heap is a binary tree with properties: It is complete Each level of tree completely filled (except possibly bottom) It satisfies heap-order property Data in each node >= data in children Also called a maxheap If data in node <= data in children, it’s a minheap Can be efficiently stored in array Store all nodes at given level from left to right in consecutive locations If first element @ index 1, then, given index n Children are at indexes 2*n and 2*n + 1 Parent is at index n/2 7/5/2019 Data Structures: Exam 3 Preview

Review: Basic Heap Operations Constructor Set mySize to 0, allocate array Empty Check value of mySize Retrieve max item Return root of the binary tree, myArray[1] 7/5/2019 Data Structures: Exam 3 Preview

Review: Basic Heap Operations Delete max item Max item is the root, replace with last node in tree Then interchange root with larger of two children Continue this with the resulting sub-tree(s) “Percolate down” Result called a semiheap 7/5/2019 Data Structures: Exam 3 Preview

Review: Basic Heap Operations Insert an item Amounts to a percolate up routine Place new item at end of array Interchange with parent so long as it is greater than its parent 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heapsort Given a list of numbers in an array Stored in a complete binary tree Convert to a heap Begin at last node not a leaf Apply percolate down to this subtree Continue 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heapsort Now swap element 1 (root of tree) with last element This puts largest element in correct location Use percolate down on remaining sublist Converts from semi-heap to heap 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Heapsort Again swap root with rightmost leaf Continue this process with shrinking sublist 7/5/2019 Data Structures: Exam 3 Preview

Review: Heapsort Algorithm 1. Consider x as a complete binary tree, use heapify to convert this tree to a heap 2. for i = n down to 2: a. Interchange x[1] and x[i] (puts largest element at end) b. Apply percolate_down to convert binary tree corresponding to sublist in x[1] .. x[i-1] 7/5/2019 Data Structures: Exam 3 Preview

Review: Priority Queue A collection of data elements Items stored in order by priority Higher priority items removed ahead of lower Commonly implemented in heap Highest priority item  top of heap Write priority queue operations in terms of heap operations Example: remove highest priority = remove top of heap 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Quicksort Efficient exchange sorting scheme Uses divide-and-conquer approach Quicksort steps Choose pivot Perform exchanges so list is ordered as {items < pivot}, pivot, {items > pivot} Search from right until item < pivot found Search from left until item > pivot found Exchange the two Stop exchanges once indexes equal, then swap pivot with that spot Return list rearranged as { quicksort({items < pivot}), pivot, quicksort({items > pivot}) } 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Mergesort Mergesort can be used both as an internal and an external sort. Basic operation in mergesort is merging, combining two lists that have previously been sorted resulting list is also sorted. Repeatedly do the following Split file (list) into two files (lists) Merge sorted subfiles (sublists) together into one file 7/5/2019 Data Structures: Exam 3 Preview

Review: Natural Merge Sort Copy alternate groupings into two files Use the sub-groupings, not a power of 2 Look for possible larger groupings 7/5/2019 Data Structures: Exam 3 Preview

Review: Natural Merge Sort Merge the corresponding sub files EOF for F2, Copy remaining groups from F1 7/5/2019 Data Structures: Exam 3 Preview

Review: Natural Merge Sort Split again, alternating groups Merge again, now two subgroups One more split, one more merge gives sort 7/5/2019 Data Structures: Exam 3 Preview

Review: Split for natural mergesort Splits file F into F1 and F2 by copying sorted subfiles alternately Open F for input and F1 and F2 for output While EOF not reached for F Repeatedly read element of F into F1 until Next element of F is less than last element copied, or End of F reached Repeatedly read element of F into F2 as described above End while 7/5/2019 Data Structures: Exam 3 Preview

Review: Merge for natural mergesort Merge corresponding sorted subfiles from F1 and F2 to produce F Open F1 and F2 for input, F for output Initialize numSubfiles to 0 While EOF not reached for either input file While no end of a subfile in F1 or F2 reached If next element in F1 < next element in F2, copy element from F1 to F Else, copy element from F2 to F End while If end of subfile in F1 reached, copy rest of subfile from F2 to F Else, copy rest of subfile from F1 to F numSubfiles++ Copy any remaining subfiles to F, incrementing numSubfiles for each 7/5/2019 Data Structures: Exam 3 Preview

Review: Natural Merge Sort Repeat the following until numSubfiles == 1 Split F into F1 and F2 Merge F1 and F2 back into F Worst case for natural merge sort O(n log2n) 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Review: Hash Tables In some situations faster search is needed Solution is to use a hash function Value of key field given to hash function Location in a hash table is calculated 7/5/2019 Data Structures: Exam 3 Preview

Review: Linear probing Observe the problem with same value returned by h(i) for different values of i Called collisions A simple solution is linear probing Linear search begins at collision location Continues until empty slot found for insertion 7/5/2019 Data Structures: Exam 3 Preview

Review: other collision strategies Linear probing can result in primary clustering Consider quadratic probing Probe sequence from location i is i + 1, i – 1, i + 4, i – 4, i + 9, i – 9, … Secondary clusters can still form Double hashing Use a second hash function to determine probe sequence 7/5/2019 Data Structures: Exam 3 Preview

Review: other collision strategies Chaining Table is a list or vector of head nodes to linked lists When item hashes to location, it is added to that linked list 7/5/2019 Data Structures: Exam 3 Preview

Data Structures: Exam 3 Preview Final notes Next time Exam 3 Reminders: Program 6 (extra credit) posted; due 5/3 Will add test cases more details soon Q&A session to be Th 5/4; time/room TBD Final exam: F 5/5 3-6 PM, Ball 412 Course eval to be submitted at final exam Link to be posted on site; blank copies available outside office 7/5/2019 Data Structures: Exam 3 Preview