Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 1
Midterm Exam 1 When: Wednesday (02/27) 12:10 -1pm Where: In Class Closed book, Closed notes Comprehensive Material for preparation: Lecture Slides Homeworks and Programming assignments Weiss book
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
Math Review (Chapter 1) Floors, ceilings, exponents and logarithms Definitions and manipulations Series: Arithmetic and Geometric series Definitions and manipulations
Math Review (Chapter 1) Proof Techniques: Read the definition, components, and how to use the following Proof by induction Example: Prove (is a perfect square) Proof by counterexample Proof by contradiction Recursion Read the definition and rules Analyze running time of recursive algorithm
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
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
Asymptotic Notations O() – upper bound () – lower bound () – tight bound o() – strict upper bound
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
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)
Abstract Data Types (Chapter 3) Lists Operations: Insert, Delete, Search Implementations: vectors, singly-linked lists, double- linked lists Analysis of operations for each implementation Time complexity Stacks (LIFO) Operations: Push, Pop, Top Implementations: linked-list, vector (tradeoffs) Analysis of operations for each implementation Time complexity
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
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
Tree Traversal Problem Construct a Binary Tree from given in-order and pre- order tree traversals output: The inOrder traversal of the tree is: left, root, right The preOrder traversal of the tree is: root, left, right
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
Insert operation on a BST insert(T, x) Inserts x into the BST T E.g. insert 5
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
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
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
AVL Trees Which of the following is an AVL tree? AVL tree?
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
AVL Insert (4 Cases) Assume node k needs to be rebalanced Four cases leading to violation Case 1: An insertion into the left subtree of the left child of k Case 2: An insertion into the left subtree of the right child of k Case 3: An insertion into the right subtree of the left child of k Case 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
Identifying Cases for AVL Insert k Let this be the node with the violation (i.e, imbalance) (nearest to the last insertion site) Insert CASE 1 Insert CASE 2 Insert CASE 3 Insert CASE 4 right child left child left subtree right subtree
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 CASE 1: Single right rotation CASE 2: Left-right double rotation
Tentative Midterm Exam#1 Structure Part I (45 pts): Coding Questions Given the class information and few methods write the pseudo code of the remaining methods Provide the runtime complexity of different methods Part II (55 pts): Conceptual & Enumerative Questions Mathematical Proof Binary Search Tree/Binary Tree Traversal AVL Tree
Good Luck !