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

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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.
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
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)
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Dijkstra/Prim 1 make-heap |V| insert |V| delete-min |E| decrease-key Priority Queues make-heap Operation insert find-min delete-min union decrease-key.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Binomial Heaps Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Binary and Binomial Heaps These lecture slides are adapted from CLRS, Chapters.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
CS 3610 Midterm Review.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
+ 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,
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
CSC317 1 Binary Search Trees (binary because branches either to left or to right) Operations: search min max predecessor successor. Costs? Time O(h) with.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Divide-and-Conquer 6/30/2018 9:16 AM
Hashing Exercises.
Binary Trees, Binary Search Trees
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Divide-and-Conquer 7 2  9 4   2   4   7
Fundamental Structures of Computer Science
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
Binary and Binomial Heaps
Divide-and-Conquer 7 2  9 4   2   4   7
Binary SearchTrees [CLRS] – Chap 12.
Amortized Analysis and Heaps Intro
Priority Queues Supports the following operations. Insert element x.
Binary Trees, Binary Search Trees
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

+ 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, move on and come back Must take it by Friday at 6pm You may use: your book your notes the class notes ONLY these things Do NOT discuss it with anyone until after Friday at 6pm

+ Midterm General what is an algorithm algorithm properties pseudocode proving correctness run time analysis memory analysis

+ Midterm Big O proving bounds ranking/ordering of functions Amortized analysis Recurrences solving recurrences substitution method recursion-tree master method

+ Midterm Sorting insertion sort merge sort quick sort partition function bubble sort heap sort

+ Midterm Divide and conquer divide up the data (often in half) recurse possibly do some work to combine the answer Calculating order statistics/medians Basic data structures set operations array linked lists stacks queues

+ Midterm Heaps binary heaps binomial heaps Search trees BSTs B-trees

+ Midterm Other things to know: run-times (you shouldn’t have to look all of them up, though I don’t expect you to memorize them either) when to use an algorithm proof techniques look again an proofs by induction

+ Data structures so far When would we use: Arrays get and set particular indices in constant time linked list insert and delete in constant time stack LIFO queue FIFO

+ Data structures so far When would we use: binary heap max/min in log time binomial heap max/min in log time supports the union operation BST search in log time B-Tree search on disk in log disk accesses

+ Recurrences: three approaches Substitution method: when you have a good guess of the solution, prove that it ’ s correct Recursion-tree method: If you don ’ t have a good guess, the recursion tree can help. Then solve with substitution method. Master method: Provides solutions for recurrences of the form:

+ Substitution method Guess the form of the solution Then prove it’s correct by induction Halves the input then constant amount of work Similar to binary search: Guess:O(log 2 n)

+ Proof? Proof by induction! -Assume it’s true for smaller T(k) -prove that it’s then true for current T(n)

+ Assume T(k) = O(log 2 k) for all k < n Show that T(n) = O(log 2 n) From our assumption, T(n/2) = O(log 2 n): From the definition of O: T(n/2) ≤ c log 2 (n/2)

+ To prove that T(n) = O(log 2 n) we need to identify the appropriate constants: if c ≥ d i.e. some constant c such that T(n) ≤ c log 2 n residual

+ Guess the solution? At each iteration, does a linear amount of work (i.e. iterate over the data) and reduces the size by one at each step O(n 2 ) Assume T(k) = O(k 2 ) for all k < n again, this implies that T(n-1) ≤ c(n-1) 2 Show that T(n) = O(n 2 ), i.e. T(n) ≤ cn 2

+ if residual which holds for any c ≥1 for n ≥1

+ Changing variables Guesses? We can do a variable change: let m = log 2 n (or n = 2 m ) Now, let S(m)=T(2 m )

+ Guess? substituting m=log n Changing variables

+ Recurrences

+ Binary Search Trees BST – A binary tree where a parent ’ s value is greater than all values in the left subtree and less than or equal to all the values in the right subtree the left and right children are also binary trees Why not? Can be implemented with with pointers or an array

+ Example

+ Visiting all nodes In sorted order

+ Visiting all nodes In sorted order

+ Visiting all nodes In sorted order , 8

+ Visiting all nodes In sorted order , 8, 9

+ Visiting all nodes In sorted order , 8, 9, 12

+ Visiting all nodes What ’ s happening? , 8, 9, 12

+ Visiting all nodes In sorted order , 8, 9, 12, 14

+ Visiting all nodes In sorted order , 8, 9, 12, 14, 20

+ Visiting all nodes in order

+ any operation

+ Is it correct? Does it print out all of the nodes in sorted order?

+ Running time? How much work is done for each call? How many calls? Θ (n)

+ What about?

+ Preorder traversal , 8, 5, 9, 14, 20

+ What about?

+ Postorder traversal , 9, 8, 20, 14, 12

+ Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 B k is a binomial tree B k-1 with the addition of a left child with another binomial tree B k-1

+ Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 Height? H(B o ) = 1 H(B k ) = 1 + H(B k-1 ) = k B1B1 B k-1 BkBk B2B2 B0B0

+ Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 What are the children of the root? k binomial trees: B k-1, B k-2, …, B 0 B1B1 B k-1 BkBk B2B2 B0B0

+ Binomial Heap Binomial heap Vuillemin, Sequence of binomial trees that satisfy binomial heap property: each tree is min-heap ordered top level: full or empty binomial tree of order k which are empty or full is based on the number of elements B4B4 B0B0 B1B

+ Binomial Heap: Properties B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = How many heaps? O(log n) – binary number representation

+ Binomial Heap: Properties B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = Where is the max/min? Must be one of the roots of the heaps

+ Binomial Heap: Properties B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = Runtime of max/min? O(log n)

+ Binomial Heap: Properties B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = Height? floor(log 2 n) - largest tree = B log n - height of that tree is log n

+ Binomial Heap: Union How can we merge two binomial tree heaps of the same size (2 k )? connect roots of H' and H'' choose smaller key to be root of H H'' H' Runtime? O(1)

+ Binomial Heap: Union What if they’re not they’re not the simple heaps of size 2 k ?

+ Binomial Heap: Union = Go through each tree size starting at 0 and merge as we go

+ Binomial Heap: Union

+ Binomial Heap: Union

+ Binomial Heap: Union Analogous to binary addition Running time? Proportional to number of trees in root lists 2 O(log 2 N) O(log N) = 26

H Binomial Heap: Delete Min/Max We can find the min/max in O(log n). How can we extract it? Hint: B k consists of binomial trees: B k-1, B k-2, …, B 0

H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. Find root x with min key in root list of H, and delete H'  broken binomial trees H  Union(H', H)

x H Binomial Heap: Decrease Key Just call Decrease-Key/Increase-Key of Heap Suppose x is in binomial tree B k Bubble node x up the tree if x is too small Running time: O(log N) Proportional to depth of node x depth = 3

+ Binomial Heap: Delete Delete node x in binomial heap H Decrease key of x to -  Delete min Running time: O(log N)

+ Binomial Heap: Insert Insert a new node x into binomial heap H H'  MakeHeap(x) H  Union(H', H) Running time. O(log N) H x H'

+ Heaps