Cpt S 122 Data Structures Course Review FINAL

  • Slides: 59
Download presentation
Cpt S 122 – Data Structures Course Review FINAL Nirmalya Roy School of Electrical

Cpt S 122 – Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Final n n When: Wednesday (12/12) 1: 00 pm -3: 00 pm Where: In

Final n n When: Wednesday (12/12) 1: 00 pm -3: 00 pm Where: In Class n Closed book, Closed notes Comprehensive n Material for preparation: n ¡ ¡ ¡ Lecture Slides Quizzes, Labs and Programming assignments Deitel & Deitel book (Read and re-read Chapter 15 to 22 and Chapter 24 and the last 5 weeks lecture notes from course webpage)

Course Overview (First 5 weeks) n Functions (Chapter 5) ¡ ¡ n Pointers (Chapter

Course Overview (First 5 weeks) n Functions (Chapter 5) ¡ ¡ n Pointers (Chapter 7) ¡ ¡ ¡ n Function Call Stack and Stack Frames Pass-by-value and Pass-by-reference Pointer Operators Passing Arguments to Functions by Reference const qualifiers with Pointers Characters and Strings (Chapter 8) ¡ Fundamentals of Strings and Characters

Course Overview n Data Structures (Chapter 12) ¡ ¡ ¡ Self Referential Structures Dynamic

Course Overview n Data Structures (Chapter 12) ¡ ¡ ¡ Self Referential Structures Dynamic Memory Allocation Linked Lists, Stacks and Queues ¡ ¡ ¡ insert, delete, is. Empty, print Binary Trees, Binary Search Trees Tree Traversals ¡ pre. Order, in. Order, post. Order

Course Overview (Second 5 weeks) n C++ as a better C; Introducing Object Technology

Course Overview (Second 5 weeks) n C++ as a better C; Introducing Object Technology (Chapter 15) ¡ ¡ ¡ n Introduction to Classes, Objects & Strings (Chapter 16) ¡ ¡ n Inline Function Overloading and Function Templates Pass-by-value and Pass-by-reference Data members, Members functions, set and get functions Constructors Classes: A Deeper Look, Part I (Chapter 17) ¡ ¡ Separating interface from implementation Destructors

Course Overview n Classes: A Deeper Look, Part 2 (Chapter 18) ¡ ¡ n

Course Overview n Classes: A Deeper Look, Part 2 (Chapter 18) ¡ ¡ n const Objects and const Member functions Composition: Objects as members of class friend function and friend class this pointer Operator Overloading; Class String (Chapter 19) ¡ ¡ ¡ Implementation of operator overloading Dynamic memory management using new operator Explicit constructor

Course Overview n Object Oriented Programming: Inheritance (Chapter 20) ¡ ¡ n Base Classes

Course Overview n Object Oriented Programming: Inheritance (Chapter 20) ¡ ¡ n Base Classes & Derived Classes public, protected, and private Inheritance Object Oriented Programming: Polymorphism (Chap. 21) ¡ ¡ ¡ Abstract Classes & pure virtual Functions & Dynamic Binding Polymorphism & Run. Time Type Information (RTTI) n ¡ downcasting, dynamic_cast virtual Destructors

Course Overview n Templates (Chapter 22) ¡ ¡ ¡ n Function Template Class Templates

Course Overview n Templates (Chapter 22) ¡ ¡ ¡ n Function Template Class Templates STL Containers: example of container class template such as stack Exception Handling (Chapter 24) ¡ ¡ ¡ Use of try, catch and throw to detect, handle and indicate exceptions, respectively. Exception handling with constructors & destructors Processing new failures

Course Overview (Last 5 weeks) n Templatized Linked List ¡ n Templatized Stack ¡

Course Overview (Last 5 weeks) n Templatized Linked List ¡ n Templatized Stack ¡ n push, pop, top, is. Stack. Empty, print. Stack Templatized Queue ¡ n insert, delete, is. Empty, print. List enqueue, dequeue, is. Queue. Empty, print. Queue Templatized Tree ¡ insert. Node, pre. Order, in. Order, post. Order

Course Overview (Last 5 weeks) n Sorting algorithms and Runtime analysis ¡ ¡ n

Course Overview (Last 5 weeks) n Sorting algorithms and Runtime analysis ¡ ¡ n Standard Template Library ¡ n Bubble sort, Selection sort, Insertion sort, Shell sort, Merge sort, Quick sort Enumeration of sorting algorithms in practice Containers, Iterators, Algorithms Abstract Data Types ¡ ¡ vector, list, stack, queue Runtime complexity

Function Overloading n C++ enables several functions of the same name to be defined,

Function Overloading n C++ enables several functions of the same name to be defined, as long as they have different signatures. ¡ n The C++ compiler selects the proper function to call ¡ n examining the number, types and order of the arguments in the call. Overloaded functions are distinguished by their signatures. ¡ n This is called function overloading. A signature is a combination of a function’s name and its parameter types (in order). Function overloading is used to create several functions of the same name ¡ perform similar tasks, but on different data types.

Function Templates n n Overloaded functions normally perform similar or identical operations on different

Function Templates n n Overloaded functions normally perform similar or identical operations on different types of data. If the operations are identical for each type, they can be expressed more compactly and conveniently using function templates.

Inheritance n n n With object-oriented programming, we focus on the commonalities among objects

Inheritance n n n With object-oriented programming, we focus on the commonalities among objects in the system rather than on the special cases. We distinguish between the is-a relationship and the has-a relationship. The is-a relationship represents inheritance. ¡ In an is-a relationship, n n an object of a derived class can be treated as an object of its base class. By contrast, the has-a relationship represents composition.

Dynamic Memory Management n n n Use the new operator to dynamically allocate (i.

Dynamic Memory Management n n n Use the new operator to dynamically allocate (i. e. , reserve) the exact amount of memory required to hold an object or array at execution time. Once memory is allocated in the free store, you can access it via the pointer that operator new returns. To destroy a dynamically allocated object, use the delete operator as follows: n n delete ptr; To deallocate a dynamically allocated array, use the statement n delete [] ptr;

Polymorphism late binding, or dynamic binding, virtual function early binding, or static linking Compile

Polymorphism late binding, or dynamic binding, virtual function early binding, or static linking Compile time Polymorphism Function Overloading Operator Overloading Runtime Polymorphism Virtual Functions

Abstract Classes and pure virtual Functions n A class is made abstract by declaring

Abstract Classes and pure virtual Functions n A class is made abstract by declaring one or more of its virtual functions to be “pure. ” ¡ A pure virtual function is specified by placing “= 0” in its declaration, as in virtual void draw() const = 0; n Abstract classes are classes from which you never intend to instantiate any objects. n Classes that can be used to instantiate objects are called concrete classes.

Exception Handling n What is exception handling? ¡ ¡ ¡ n Exception Specifications ¡

Exception Handling n What is exception handling? ¡ ¡ ¡ n Exception Specifications ¡ n Example: Handling an attempt to divide by zero Use try, catch and throw to detect, handle and indicate exceptions, respectively. Rethrowing an exception Processing unexpected and uncaught exceptions Processing new failures ¡ ¡ Dynamic memory allocation Use unique_ptr to prevent memory leak

Exception Handling (cont. ) n n try blocks enable exception handling. ¡ The try

Exception Handling (cont. ) n n try blocks enable exception handling. ¡ The try block encloses statements that might cause exceptions and statements that should be skipped if an exception occurs. Exceptions are processed by catch handlers (also called exception handlers), which catch and handle exceptions. ¡ At least one catch handler must immediately follow each try block.

Last 5 Weeks n n n Templated Classes Sorting and Algorithm Analysis Standard Template

Last 5 Weeks n n n Templated Classes Sorting and Algorithm Analysis Standard Template Library (STL) Abstract Data Type Runtime Complexity

List. Node Template Class

List. Node Template Class

List Class Template

List Class Template

List Class Constructor

List Class Constructor

List Class Destructor

List Class Destructor

insert. At. Front() & Runtime

insert. At. Front() & Runtime

insert. At. Back() & Runtime

insert. At. Back() & Runtime

remove. From. Front() & Runtime

remove. From. Front() & Runtime

remove. From. Back() & Runtime

remove. From. Back() & Runtime

is. Empty() & Runtime

is. Empty() & Runtime

Templated Linked List Problem n Please write a insert. Node() method for the class

Templated Linked List Problem n Please write a insert. Node() method for the class List. This method should insert data in the list in order. ¡ ¡ ¡ First create a new node with the provided value. Make sure to check if the list is empty and if so set the pointers appropriately to insert the new node into the empty list. Otherwise traverse the list by advancing a current pointer, compare the new Node’s value with the existing List. Node’s data to find the correct position and then set the pointers appropriately to insert the new node considering the following 3 cases n n n insert. At. Front insert. In. Between insert. At. Back

Templated Stack n Option 1: Implement a stack class primarily by reusing a list

Templated Stack n Option 1: Implement a stack class primarily by reusing a list class. ¡ n private inheritance of the list class. Option 2: Implement an identically performing stack class through composition ¡ a list object as a private member of a stack class.

List Class is Given

List Class is Given

Templated Stack derived from class List runtime of push, pop, is. Stack. Empty, print.

Templated Stack derived from class List runtime of push, pop, is. Stack. Empty, print. Stack? ?

Templated Stack (cont. )

Templated Stack (cont. )

Queue Class Template runtime of enqueue, dequeue, is. Queue. Empty, print. Queue? ?

Queue Class Template runtime of enqueue, dequeue, is. Queue. Empty, print. Queue? ?

Queue Class Template

Queue Class Template

Trees n n Linked lists, stacks and queues are linear data structures. A tree

Trees n n Linked lists, stacks and queues are linear data structures. A tree is a nonlinear, two-dimensional data structure. ¡ n arrays arrange data linearly, binary trees can be envisioned as storing data in two dimensions Tree nodes contain two or more links. ¡ trees whose nodes all contain two links (none, one or both of which may be null).

Tree. Node Class Template

Tree. Node Class Template

List. Node Member Function

List. Node Member Function

Tree. Node Class Template (cont. )

Tree. Node Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template (cont. )

Tree Class Template preorder Traversal n pre. Order traversal is: root, left, right

Tree Class Template preorder Traversal n pre. Order traversal is: root, left, right

Tree Class Template in. Order Traversal n in. Order traversal is: left, root, right

Tree Class Template in. Order Traversal n in. Order traversal is: left, root, right

Tree Class Template post. Order Traversal n post. Order traversal is: left, right, root

Tree Class Template post. Order Traversal n post. Order traversal is: left, right, root

Other Binary Tree Operations n The level order traversal of a binary tree visits

Other Binary Tree Operations n The level order traversal of a binary tree visits the nodes of the tree row-by-row starting at the root node level. ¡ ¡ n On each level of the tree, the nodes are visited from left to right. The level order traversal is not a recursive algorithm. Implement the level order binary tree traversal using a common data structure we have discussed in the class. ¡ Write the pseudo code of this algorithm.

Level Order Binary Tree Traversal n n Use the Queue data structure to control

Level Order Binary Tree Traversal n n Use the Queue data structure to control the output of the level order binary tree traversal. Algorithm ¡ ¡ Insert/enqueue the root node in the queue While there are nodes left in the queue, n n Get/dequeue the node in the queue Print the node’s value If the pointer to the left child of the node is not null ¡ Insert/enqueue the left child node in the queue If the pointer to the right child of the node is not null ¡ Insert/enqueue the right child node in the queue

Problem in Tress n Given the pre. Order, in. Order or post. Order traversals

Problem in Tress n Given the pre. Order, in. Order or post. Order traversals of a binary tree, draw the binary tree. ¡ ¡ ¡ pre. Order: 27 13 6 17 42 33 48 in. Order: 6 13 17 27 33 42 48 post. Order: 6 17 13 33 48 42 27

Problem in Trees n Draw the a binary search tree after inserting the following

Problem in Trees n Draw the a binary search tree after inserting the following sequence of values using the insert. Node() method from the tree class: ¡ 50, 25, 12, 6, 13, 33, 75, 67, 68, 88

Sorting Algorithms n n n Bubble sort Insertion sort Selection sort Shell sort Merge

Sorting Algorithms n n n Bubble sort Insertion sort Selection sort Shell sort Merge sort Quick sort Worst case Runtime O(? ) Worst case Runtime O(? ) Which one is best at least theoretically? Which one is best in practice and Why?

Comparison of Sorting Algorithms Bubble Sort (N 2) (N) Selection Sort (N 2) Best

Comparison of Sorting Algorithms Bubble Sort (N 2) (N) Selection Sort (N 2) Best Case is quadratic

Enumeration of Sorting Algorithms n Try the insertion sort, selection sort, shell sort, merge

Enumeration of Sorting Algorithms n Try the insertion sort, selection sort, shell sort, merge sort and quick sort, algorithm on the following list of integers for example: ¡ {7, 3, 9, 5, 4, 8, 0, 1}

Standard Template Library (STL) Containers Iterators Algorithms

Standard Template Library (STL) Containers Iterators Algorithms

Abstract Data Types (ADTs) n ADT is a set of objects together with a

Abstract Data Types (ADTs) n ADT is a set of objects together with a set of operations ¡ Abstract n n ¡ n n implementation of operations is not specified in ADT definition E. g. , List Operations on a list: Insert, delete, search, sort C++ class are perfect for ADTs Can change ADT implementation details without breaking code that uses the ADT

Lists Using STL n Two popular implementation of the List ADT ¡ The vector

Lists Using STL n Two popular implementation of the List ADT ¡ The vector provides a growable array implementation of the List ADT n n ¡ The list provides a doubly linked list implementation of the List ADT n n n Advantage: it is indexable in constant time Disadvantage: insertion and deletion are computationally expensive Advantage: insertion and deletion are cheap provided that the position of the changes are known Disadvantage: list is not easily indexable Vector and list are class templates ¡ Can be instantiated with different type of items

Lists Using STL (cont’d) n vector<Object> ¡ ¡ ¡ Array-based implementation find. Kth –

Lists Using STL (cont’d) n vector<Object> ¡ ¡ ¡ Array-based implementation find. Kth – O(1) insert and remove – O(N) n n list<Object> ¡ ¡ ¡ Doubly-linked list with sentinel nodes find. Kth – O(N) insert and remove – O(1) n n Unless change at end of vector If position of change is known Both require O(N) for search

Course Evaluations n Please take a few minutes to complete the online course evaluations

Course Evaluations n Please take a few minutes to complete the online course evaluations available at https: //skylight. wsu. edu/student/ n Thank you for taking Cpt. S 122.

Tentative Final Structure n Part I: Conceptual Questions (45 pts) ¡ n Part II:

Tentative Final Structure n Part I: Conceptual Questions (45 pts) ¡ n Part II: Programming Questions (35 pts) ¡ ¡ n Multiple Choice, Fill-in-the-blank, and True/False ¡ Go though the self-review exercises at the end of each chapter Write Templated C++ code for Linked List, Stack, Queue and Tree Retake Quiz 1 to Quiz 6 Part III: Enumerative and Pseudo code Questions (20 pts) ¡ ¡ Workout all the sorting algorithm on an example input set ¡ Remember the time complexity Practice pre. Order, in. Order, post. Order & level-order traversal algorithm

Good Luck !

Good Luck !