Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.

Similar presentations


Presentation on theme: "Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm."— Presentation transcript:

1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 1

2 Midterm Exam 1 When: Wednesday (02/29) 12:10 -1pm Where: In Class Closed book, Closed notes Comprehensive Material for preparation:  Lecture Slides  Homeworks and Programming assignments  Weiss book

3 Course Overview Math and C++ Review (Chapter 1) Algorithm development and analysis (Chapter 2)  Running time Abstract Data Types (Chapter 3)  Linked Lists, Stacks and Queues  Insert, delete, search, sort Trees (Chapter 4)  Tree Traversals  Binary Trees  Binary Search Trees  AVL Trees  Splay Trees

4 Math Review (Chapter 1) Floors, ceilings, exponents and logarithms  Definitions and manipulations Series: Arithmetic and Geometric series  Definitions and manipulations

5 Math Review (Chapter 1) Proof Techniques: Read the definition, components, and how to use the following  Proof by induction  Proof by counterexample  Proof by contradiction Recursion  Read the definition and rules  Analyze running time of recursive algorithm

6 C++ Review (Chapter 1) Classes and Objects and Methods  Default Parameters, Initializer List, explicit Constructor, Destructor, Constant Member Function  Copy constructor, operator overloading, operator= Call by value, Call by reference Call by constant reference Templates  Function templates and Class templates STL for different data structures

7 Algorithm Analysis (Chapter 2) Asymptotic analysis of an algorithm Best-case, worst-case and average-case analysis Rate of growth: Definitions and Notation (O, Ω, Θ, o)  Proofs for specific examples

8 Asymptotic Notations O() – upper bound  () – lower bound  () – tight bound o() – strict upper bound

9 Asymptotic Analysis (Chapter 2) O-notation gives an upper bound for a function to within a constant factor  - notation gives an lower bound for a function to within a constant factor  -notation bounds a function to within a constant factor  The value of f(n) always lies between c 1 g(n) and c 2 g(n) inclusive

10 Big-Theta and Little-Theta “Big-Theta” notation; 2N 2 =  (N 2 )  Suppose T(N) = 2N 2 then T(N) = O(N 4 ); T(N) = O(N 3 ); T(N) = O(N 2 ) all are technically correct, but last one is the best answer. Now writing T(N)=  (N 2 ) says not only that T(N)= O(N 2 ), but also the result is as good (tight) as possible The definition of O-notation and o-notation are similar  The main difference is that in T(N)=O(g(N)), the bound 0  T(N)  cg(N) holds for some constant c > 0, but in T(N)=o(g(N)), the bound 0  T(N)  cg(N) holds for all constants c > 0  For example 2N 2 ≠ o(N 2 )

11 Maximum Subsequence Sum Problem (Chapter 2) Maximum subsequence sum problem  Given (possibly negative) integers A 1, A 2, …, A N, find the maximum value (≥ 0) of:  E.g.  Solution # 1:  Solution # 2:  Solution # 3: Recursive, “divide and conquer”  Solution # 4 : Online Algorithm The maximum sum is 16 T(N) = O(N 3 ) T(N) = O(N 2 ) T(N) = O(N logN) T(N) = O(N)

12 Abstract Data Types (Chapter 3) Lists  Operations: Insert, Delete, Search  Implementations: vectors, singly-linked lists, double- linked lists  Analysis of operations for each implementation Stacks (LIFO)  Operations: Push, Pop, Top  Implementations: linked-list, vector (tradeoffs)  Analysis of operations for each implementation

13 Abstract Data Types (Chapter 3) Queues (FIFO)  Operations: Enqueue, dequeue  Implementations: linked-list, vector (tradeoffs)  Analysis of operations for each implementation Standard Template Library (STL)  Use of vector, list, stack and queue template classes  Use of iterators Understand all the tradeoffs (in time and space) between all these data structures Given the class information and few methods write the pseudo code of the remaining methods

14 Trees (Chapter 4) Binary Tree Binary Tree Traversals  Pre-order traversal: root, left, right The work at a node is performed before (pre) its children are processed  In-order traversal: left, root, right  Post-order traversal: left, right, root The work at a node is performed after (post) its children are evaluated Constructing a Binary Tree form pre-order, in-order and post-order traversals

15 Binary Search Tree (BST)  Binary search tree (BST) is a tree where: For any node n, items in left subtree of n  item in node n  items in right subtree of n  Average depth of a BST is O(logN)  Operations: Insert, Delete, Search, FindMin, FindMax, traversals  Worst-case and Average-case analysis

16 Insert operation on a BST insert(T, x)  Inserts x into the BST T  E.g. insert 5

17 Remove operation on a BST (Case 1) remove(T, x)  Removes x from the BST T  Case 1: Node to remove has 0 or 1 child If a node is a leaf, just delete it If the node has one child, the node can be deleted after its parent adjust a link to bypass the node E.g. remove 4

18 Remove operation on a BST (Case 2) remove(T, x)  Removes x from the BST T  Case 2: Node to remove has 2 children Replace the data of this node with the smallest data of the right subtree and recursively delete that node (which is now empty)  Smallest node in the right subtree cannot have a left child, the second remove is an easy one First Replace node element with successor Remove successor (Case 1) E.g. remove 2 Construct a BST from a sequence of values using insert and remove operation

19 AVL Trees Balanced BST (AVL trees)  The height of the left and right subtrees at every node in the BST differ by at most 1  Maintained via rotations operations  Depth always O(log 2 N)  Operations: Insert, Search, FindMin, FindMax  AVL Insert (Remember 4 Cases)  Work out how to perform these on an AVL tree and show the resulting AVL tree from a given sequence

20 AVL Trees Which of the following is an AVL tree? AVL tree?

21 AVL Insert Insert can violate the AVL balance condition Can be fixed by a rotation Inserting 6 violates AVL balance condition Rotating 7-8 restores balance

22 AVL Insert (4 Cases) Assume node k needs to be rebalanced Four cases leading to violation 1. An insertion into the left subtree of the left child of k 2. An insertion into the right subtree of the left child of k 3. An insertion into the left subtree of the right child of k 4. An insertion into the right subtree of the right child of k  Cases 1 and 4 are handled by single rotation  Cases 2 and 3 are handled by double rotation

23 Exercise AVL Insert Case 3 + Case 4 + Case 1 + Case 2: Example Start with an empty AVL tree and insert the items 3, 2, 1 and then 4 through 7 in sequential order  Insert 10 through 16 in reverse order, followed by 8 and then 9 CASE 3: Right-left double rotation 4 2 6 3 7 1 5 CASE 1: Single right rotation CASE 2: Left-right double rotation

24 Splay Tree Splay trees  After a node is accessed, push it to the root via AVL rotations  Average depth per operation is O(log 2 N) Splaying  Selective rotations based on node, its parent, and its grandparent  If X is a child of the root, then rotate X with the root  Otherwise, follow Zig-zag and Zig-zig

25 Splay Tree: Splaying (Zig-zag) Solution  If node X is a right-child of parent P, which is a left-child of grandparent G (or vice-versa)  Perform double rotation (left, right)  “Zig-zag”

26 Splay Tree: Splaying (Zig-zig) Solution  If node X is a left-child of parent P, which is a left-child of grandparent G (or right-right)  Perform double rotation (right-right)  “Zig-zig”

27 Splay Tree: Splaying Work out an Example: Insert node 1 in the following Splay tree CASE: Zig-zig

28 Splay Tree Remove Access the node to be removed (so now it is at the root) Remove node leaving two subtrees T L and T R Access largest element in T L  Now at root; no right child Make T R right child of root of T L

29 Splay Tree: Remove 6 7 4 2 1 5 6 7 4 2 1 5 4 2 1 5 7 5 7 4 2 1 Example: Remove 6

30 Templates Templates are for designing type-independent data structures and algorithms Type-independent means that the logic of the data structures and algorithms does not depend on the type of objects (i.e. the same logic works for any data types) Templates prevent recoding a piece of code for each different type 2 types  Function templates  Class templates

31 Function Templates void output(const vector & v) { for (int i = 0; i < v.size(); i++) cout << v[i] << endl; } void output(const vector & v) { for (int i = 0; i < v.size(); i++) cout << v[i] << endl; } void output(const vector & v) { for (int i = 0; i < v.size(); i++) cout << v[i] << endl; } template void output(const vector & v) { for (int i = 0; i < v.size(); i++) cout << v[i] << endl; } Untemplatized VersionTemplatized Version int main() { vector v1(37); vector v2(40); vector v3(10); //additional codes to fill in the vector are not shown output(v1); output(v2); output(v3); // illegal; operator < undefined return 0; }

32 Class Templates template class MemoryCell { public: explicit MemoryCell(const Temp & initialValue = Temp()) : storedValue(initialValue) { } const Temp & read() const { return storedValue; } void write(const Temp & x) { storedValue = x; } private: Temp storedValue; }; int main() { MemoryCell m1; MemoryCell m2(“hello”); m1.write(37); m2.write(m2.read() + “ world”); cout<<m1.read()<<endl<<m2.read()<< endl; return 0; } MemoryCell is like IntCell class but works for any type called Temp MemoryCell is NOT a class; it is a class template MemoryCell and MemoryCell are the actual classes

33 BST Template Syntax template Node*BinarySearchTree ::findMin(Node *t) const { } //Not Correct Syntax template Node * BinarySearchTree ::findMin(Node *t) const{ } //Correct Syntax Returning a pointer to a node in a templatized BinarySearchTree class template Class BinarySearchTree{… struct Node{…. } Node *root; }

34 Good Luck !


Download ppt "Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm."

Similar presentations


Ads by Google