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

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
Trees, Binary Trees, and Binary Search Trees COMP171.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Final.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Final Exam Review COP4530.
CSE 373 Data Structures Lecture 7
Chapter 12 – Data Structures
CC 215 Data Structures Trees
UNIT III TREES.
CSE 373 Data Structures Lecture 7
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Cse 373 April 26th – Exam Review.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
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.
Trees 3: The Binary Search Tree
AVL Trees CENG 213 Data Structures.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees CSE 373 Data Structures.
Final Exam Review COP4530.
CMSC 341 Binary Search Trees.
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

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/29) 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  Splay 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  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

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 )

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 Stacks (LIFO)  Operations: Push, Pop, Top  Implementations: linked-list, vector (tradeoffs)  Analysis of operations for each implementation

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

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 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

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

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

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”

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”

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

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

Splay Tree: Remove Example: Remove 6

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

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; }

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

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; }

Good Luck !