CS32 Discussion Section 1B Week 10 TA: Hao Yu (Cody)

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
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.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Overview CS113, Fall 2000 Gene Itkis. The Promise Heavy Fast-paced Challenging Rewarding.
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,
CS 240: Data Structures Thursday, July 12 th Sorting – Bubble, Insertion, Quicksort, Mergesort, Analysis, STL.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Memory Management CS Chakrabarti Variable storage thus far  Never used global variables  All variables allocated inside functions, passed.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Information and Computer Sciences University of Hawaii, Manoa
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
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.
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.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
April 27, 2017 COSC Data Structures I Review & Final Exam
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Final Exam Review COP4530.
Final Exam Review CS 3358.
Searching and Sorting Searching algorithms with simple arrays
Chapter 24 Sorting.
Introduction to Computers Computer Generations
Top 50 Data Structures Interview Questions
CS 215 Final Review Ismail abumuhfouz Fall 2014.
CS32 Discussion Section 1B Week 8
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.
Heaps & Priority Queues
Stacks Linked Lists Queues Heaps Hashes
Final Exam Review COP4530.
Containers and the Standard Template Library (STL)
Final Review Dr. Yingwu Zhu.
Sorting Dr. Yingwu Zhu.
EE 312 Final Exam Review.
Searching/Sorting/Searching
Final Exam Review Inheritance Template Functions and Classes
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
EE 312 Software Design and Implementation I
Presentation transcript:

CS32 Discussion Section 1B Week 10 TA: Hao Yu (Cody)

Reminder & Agenda Review: Go through all topics! Practice problems Final exam Tomorrow! TV TOKYO Communications Corporation, LINE stickers

Constructor/Deconstructor Type Default constructor Copy constructor Deconstructor Note Ordering! Scope of variables Allocated memory ( new ) must be freed ( delete )

Linked Lists Type Single linked lists Doubly linked lists Circularly linked lists (dummy node?) Operations Insert/search/delete Consider all cases! (head/middle/tail/empty/full) Application stack/queue/heap/hash …

Inheritance Concept The process of deriving a new class using another class as a base Note What can you see in the base/derived class? The order of constructors/deconstructors Overriding vs. overloading Polymorphism and virtual functions Abstract base class

Recursion Design guideline – Leap of faith 1.Find the base cases What are the corner cases? e.g. empty array What’s the terminal condition? e.g. n=0, length=1 2.Decompose the problem What’s the small problem you solve in one iteration? 3.Finish the small problem Note Any recursion and iterative implementation can be transformed to each other Practice and practice!

Template Concept Let compile generate multiple versions of functions for you Note Type conversion Specialized version first Legal implementation types template class Pair { public: Pair(); Pair(T first, U second); void setFirst(T value); void setSecond(U value); int getFirst() const; int getSecond() const; private: T m_first; U m_second; };

Standard Template Library Common used data structures vector (array) list (doubly linked list) set (binary search tree) map stack queue Iterator Note: Time complexity Insert/search/delete

Time Complexity

Sorting Selection sort 1.Find the minimum value 2.Swap the minimum value and the current value 3.Repeat until sorted Insertion sort 1.Pick one value 2.Insert it to the right position 3.Repeat until sorted Bubble sort 1.Compare two consecutive values and swap if necessary 2.Repeat until sorted Merge sort 1.Keep splitting the array until the length is 1 2.Merge Quick sort 1.Pick a pivot and move smaller values to the left, larger values to the right 2.Repeat step 1 to all subarrays

Binary Trees Another data structure! Usually implemented using linked list class Node { Node(int val) { value = val; left = nullptr; right = nullptr; } int value; Node *left, *right; }; class BinaryTree { BinaryTree() { m_root = nullptr; } ~BinaryTree() { freeTree(m_root); } void preOrder(Node *node); void inOrder(Node *node); void postOrder(Node *node); void levelOrder(Node *node); int numNode(Node *node); int numLeafNode(Node *node); int numNonLeafNode(Node *node); int height(Node *node); void freeTree(Node *node); Node *m_root; };

Heaps Properties A complete binary tree A node value must greater/less than its children Implementations Linked-list Array Operations Find max/min Insert/Delete Application Heapsort O(nlogn)

Hash Tables Closed vs. Open hash table Efficiency: the load factor Hash function Fast and less collisions Time complexity Usually O(1) but depends

Practice – Chang’s Problem #3

Practice – Chang’s Problem #4 X X O O X O O

Practice – Chang’s Problem #6 X O O O O

Practice – Chang’s Problem #13

A B CD EF G HI J

Practice – Choi’s Problem #7, #8 UCL N A Left  right  center G E Center  left  right C NL U A GE

Practice – Choi’s Problem #

Practice – Choi’s Problem #13 1.Only +, -, *, / 2.Precedence: * = / > + = - 3.Number range: 0 - 9

Practice – Choi’s Problem # x x x x Possible situations: Two operators 1.P(op1) = P(op2) 2.P(op1) < P(op2) 3.P(op1) > P(op2)

Practice – Choi’s Problem #13 Node *addOp(Node *root, char op, char digit) { if (isDigit(root->val)) { // First operator Node *digitNode = new Node(digit, NULL, NULL); Node *newRoot = new Node(op, root, digitNode); return newRoot; } else if (isLower(root->val, op)) { // P(op1) < P(op2) root->right = addOp(root->right, op, digit); return root; } else { // P(op1) >= P(op2) Node *digitNode = new Node(digit, NULL, NULL); Node *newRoot = new Node(op, root, digitNode); return newRoot; } If empty tree (only has a digit) else if P(op1) < P(op2) else if P(op1) >= P(op2)

Practice – Choi’s Problem #13 int evaluate(Node *root) { if (isDigit(root->val)) return root->val - '0'; switch (root->val) { case '+': return evaluate(root->left) + evaulate(root->right); case '-': return evaluate(root->left) - evaulate(root->right); case '*': return evaluate(root->left) * evaulate(root->right); case '/': return evaluate(root->left) / evaulate(root->right); } }

Finally… Review all slides (lectures, discussions) Practice released problem sets The devil is in the detail Congratulations! Good luck TV TOKYO Communications Corporation, LINE stickers