Compsci 201 Midterm 2 Review Jimmy Wei Make sure you sign in on the paper being passed around!

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Chapter 4: Trees Part II - AVL Tree
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Fundamentals of Python: From First Programs Through Data Structures
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
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.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
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 ),
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
CSC 211 Data Structures Lecture 13
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.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
+ 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,
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
(c) University of Washington20-1 CSC 143 Java Trees.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Chapter 12 – Data Structures
Trees.
ENEE150 Discussion 13 Section 0101 Adam Wang.
Chapter 20: Binary 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.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 21: Binary Trees.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Find in a linked list? first last 7  4  3  8 NULL
B-Tree Insertions, Intro to Heaps
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
A Robust Data Structure
Lecture 12 CS203 1.
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Compsci 201 Midterm 2 Review Jimmy Wei Make sure you sign in on the paper being passed around!

List of Topics Linked Lists – Comparison with ArrayList – Linked List practice Trees – Traversals – BSTs – Tree practice Recursion – Three steps of recursion – Recursion practice Recursive Backtracking – The extra steps of backtracking – Recursive backtracking practice Big O Overview – Significance – Big O for common operations

Linked Lists How do we build a LinkedList? public class Node { public int value; public Node next; public Node ( int v, Node n ) { value = v; next = n; } Traverse by storing pointer to HEAD and referencing its NEXT pointer

Linked Lists Comparison with ArrayList: – ArrayList works by storing an array and resizing it as necessary, hence why it is so fast—we can access different indices and make changes just as we would with an array – Removal in ArrayList is a bit slower since we need to recopy the entire array to eliminate an element

Linked Lists Big OArrayListLinkedList Access Insertion Removal What are the runtime differences in these two implementations?

Linked Lists Big OArrayListLinkedList AccessO(1)O(n) Insertion Removal What are the runtime differences in these two implementations? – Access is just grabbing an element at an index.

Linked Lists Big OArrayListLinkedList AccessO(1)O(n) InsertionO(1) Removal What are the runtime differences in these two implementations? – Access is just grabbing an element at an index. – Insertion is adding at the end of the list

Linked Lists Big OArrayListLinkedList AccessO(1)O(n) InsertionO(1) RemovalO(n) What are the runtime differences in these two implementations? – Access is just grabbing an element at an index. – Insertion is adding at the end of the list – Removal is removing from anywhere in the list How can ArrayList insert be O(1)? – Insert is O(1) amortized When would you want to use Linked Lists?

Linked Lists Linked List practice! Write the following method: public Node removeDuplicates(Node head) { // your code here } removeDuplicates takes in the head of a Linked List and removes all duplicate values from that list, returns the head

Trees Linked Lists on steroids Now instead of one node having one next pointer, it has multiple children; we will focus on binary trees (two children) What do we change from the LinkedList node? public class Node { public int value; public Node left; public Node right; public Node ( int v, Node l, Node r ) { value = v; left = l; right = r; } }

Trees How do we navigate across a tree? Three types of traversals: 1)Preorder (my favorite) : Root Left Right 2)Inorder : Left Root Right 3)Postorder : Left Right Root

Trees How do we express a tree in terms of its traversal? Think of every node as the root of a subtree, and things become much easier. Lets find the preorder traversal of this tree on the right

Preorder traversal: [ Root ][ Left ] [ Right ] Trees Preorder traversal is [Root] [Left] [Right]

Preorder traversal: 10 [ Left ] [ Right ] Trees Preorder traversal is [Root] [Left] [Right] Root is easy; it’s

Preorder traversal: 10 [[Root][Left][Right]] [ Right ] Trees Preorder traversal is [Root] [Left] [Right] Root is easy; it’s 10 Replace [Left] with the preorder traversal of the left subtree

Preorder traversal: [ Right ] Trees Preorder traversal is [Root] [Left] [Right] Root is easy; it’s 10 Replace [Left] with the preorder traversal of the left subtree. Now we can view this subtree as a subset of the main traversal and find its preorder

Preorder traversal: Trees Preorder traversal is [Root] [Left] [Right] Root is easy; it’s 10 Replace [Left] with the preorder traversal of the left subtree. Now we can view this subtree as a subset of the main traversal and find its preorder

Trees What makes a binary search tree special? – The tree is sorted: All left children are less than root and all right children are greater than root, repeated values are not allowed – Allows us to search the tree easily – Inserting also not any harder than before, we just move left or right by comparing our new value to the old value at each node

Trees What are the runtime complexities? – Depends on whether or not the tree is balanced. OperationBalancedUnbalanced Insert Search Remove

Trees What are the runtime complexities? – Depends on whether or not the tree is balanced. OperationBalancedUnbalanced InsertO(log n)O(n) Search Remove

Trees What are the runtime complexities? – Depends on whether or not the tree is balanced. OperationBalancedUnbalanced InsertO(log n)O(n) SearchO(log n)O(n) Remove

Trees What are the runtime complexities? – Depends on whether or not the tree is balanced. OperationBalancedUnbalanced InsertO(log n)O(n) SearchO(log n)O(n) RemoveO(log n)O(n)

Trees Tree practice! Write the following method: public boolean validate(Node root) { // your code here } Validate takes in the root of a binary search tree and returns true if that tree is a valid BST, and false otherwise

Trees Tree practice! Need a harder question? Write the following method: public Node remove(Node root, int value) { // your code here } Remove takes in a Node root and an int value and removes the node with that value from the tree with root, if it exists. This is tricky! Just finding the node isn’t enough, we must also resort the tree to still be a valid BST afterwards! – Hint: if you replace a removed node with a node from its left subtree, you must use the rightmost node in that subtree! On the other hand, if you replace from the right subtree, you must use the leftmost node in that subtree!

Recursion What is recursion? – See: – Strategy for solving harder problems by breaking them down into smaller subproblems – Three easy steps: 1)Recursion step : how do we break a problem into easier subproblems? 2)Resolution step : how do we combine the answers to our subproblems into an answer for our harder problem? 3)Base step : when do we stop recursing?

Recursion Let’s start with an easy example: finding the nth fibonacci number. We will write the following method: public int fibonacci(int n) { // returns the nth fibonacci number }

Recursion public int fibonacci(int n) { } What is the “hard” problem here? – Finding the nth fibonacci number. How can we break this down to easier subproblems? – Finding the n-1th and n-2th fibonacci number.

Recursion public int fibonacci(int n) { int a = fibonacci(n-1); int b = fibonacci(n-2); } Let’s start by adding our recursion step. How do we break the hard problem down in code?

Recursion public int fibonacci(int n) { int a = fibonacci(n-1); int b = fibonacci(n-2); int result = a + b; } Next comes the resolution step. How do we combine these subproblem answers into our hard problem answer?

Recursion public int fibonacci(int n) { if (n == 1 || n == 2) { return 1; } int a = fibonacci(n-1); int b = fibonacci(n-2); int result = a + b; } Finally, the base step. When do we stop recursing?

Recursion Recursion practice! Write the following method: public String countDown(int n) { // your code here } countDown takes in an int and prints out a String counting down from that int to 0 and then back up to that int. – For example, countDown(3) should return “3…2…1…0…1…2…3”

Recursion Recursion practice! Need a harder problem? Write the following method: public ArrayList factorize(int n) { // your code here } Factorize takes in an int and returns an ArrayList containing all of that number’s prime factors. Assume that the number is not prime and its prime factors all belong to the set [2, 3, 5, 7, 9, 11].

Recursive Backtracking Time to add in backtracking (shit just got real). Let’s face it, recursion can be super inefficient. Backtracking refers to the idea that while we are recursing, we might not always want to recurse to the end, so that we can save time. Requires us to add some steps to our three easy steps of recursion.

Recursive Backtracking Let’s amend our steps to recursion as such: 1)Recursion step : how do we break a problem into easier subproblems? 2)Memorization step : how do we save our current state in memory? 3)Resolution step : how do we combine the answers to our subproblems into an answer for our harder problem? 4)Backtrack step : how do we go back to our old state 5)Base step : when do we stop recursing?

Recursive Backtracking Let’s solve a backtracking problem! Simplified sudoku: one nxn board We will write the following method: public boolean solve(int[][] board) { // your code here } Solve takes in a 2D int array representing an incomplete sudoku board, and returns true if it can be solved – Empty spaces are represented by -1 – This method is DESTRUCTIVE!!

Recursive Backtracking public boolean solve(int[][] board) { } What is the “hard” problem here? – Solving the sudoku board. How can we break this down to easier subproblems? – Solving the sudoku board with one more space filled in. What makes this problem harder than regular recursion? – We must be able to revert our board to a previous state if we find ourselves filling in the wrong answers!

Recursive Backtracking public int[] findNextOpenSpace(int[][] board) { // returns int array representing coordinates // of next open space } public boolean checkValid(int[][] board) { // returns true if board is valid, false otherwise } To help you solve this problem, here’s a present—two black- box helper methods!

Recursive Backtracking public boolean solve(int[][] board) { int[] next = findNextOpenSpace(board); int i = next[0]; int j = next[1]; for (int v=1; v<=board.length; v++) { board[i][j] = v; boolean newBoardValid = solve(board); } } Let’s start by adding our recursion step. How do we break the hard problem down in code?

Recursive Backtracking public boolean solve(int[][] board) { int[] next = findNextOpenSpace(board); int i = next[0]; int j = next[1]; for (int v=1; v<=board.length; v++) { board[i][j] = v; boolean newBoardValid = solve(board); } Let’s next add our memorization step. How do we save our current state so that we can revert to it later if needed?

Recursive Backtracking public boolean solve(int[][] board) { int[] next = findNextOpenSpace(board); int i = next[0]; int j = next[1]; for (int v=1; v<=board.length; v++) { board[i][j] = v; boolean newBoardValid = solve(board); if (newBoardValid) { return true; } } return false; } Now the resolution step. How do we take our subproblem answer and turn it into a hard problem answer?

Recursive Backtracking public boolean solve(int[][] board) { int[] next = findNextOpenSpace(board); int i = next[0]; int j = next[1]; for (int v=1; v<=board.length; v++) { board[i][j] = v; boolean newBoardValid = solve(board); if (newBoardValid) { return true; } board[i][j] = -1; } return false; } Almost done! Next comes the backtrack step. How do we revert to our old state if we find our answer is not correct so far?

Recursive Backtracking public boolean solve(int[][] board) { if (!checkValid(board)) { return false; } int[] next = findNextOpenSpace(board); int i = next[0]; int j = next[1]; if (i == -1 && j == -1) { return checkValid(board); } for (int v=1; v<=board.length; v++) { board[i][j] = v; boolean newBoardValid = solve(board); if (newBoardValid) { return true; } board[i][j] = -1; } return false; } Last step! The base step. How do we know when to stop recursing?

Recursive Backtracking Recursive backtracking practice! So SimpleSudoku was too easy, huh? Extend the logic such that we only return true if there exists a *unique* solution: The method should remain destructive, i.e. it will change the underlying 2D array when it is run. – Hint: the method should return an int representing the number of solutions – Hint: you should consider a nonrecursive solve() method and a recursive helper

Recursive Backtracking Recursive backtracking practice! Need an even harder problem? Let’s try a 2-player backtracking game. Write the following method: public int whoWinsPilesList(ArrayList piles) { // your code here } whoWinsPilesList takes in an ArrayList of Integers that represents the piles list and returns who wins, assuming both play perfectly – During their turn, each player removes either 1 or 2 tokens from 1 pile in the piles list – The first player who cannot move loses

Big O What is the point of Big O? – Analyze runtime complexity of algorithms – Tells us how our algorithm’s runtime changes as our input size grows – Smaller Big O is better (but an O(1) algorithm will not always run faster than an O(n)!) Is it even important? – Yes! This is one of the most important things you will learn in this class!

Big O Data StructureBest-CaseWorst-CaseExpected Array ArrayList LinkedList HashSet BST Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayList LinkedList HashSet BST Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayListO(1) LinkedList HashSet BST Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayListO(1) LinkedListO(1)O(n) HashSet BST Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayListO(1) LinkedListO(1)O(n) HashSetO(1) BST Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayListO(1) LinkedListO(1)O(n) HashSetO(1) BSTO(1)O(log n) Heap Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(1) ArrayListO(1) LinkedListO(1)O(n) HashSetO(1) BSTO(1)O(log n) HeapO(1)O(n) Access – looking up an element in the data structure

Big O Data StructureBest-CaseWorst-CaseExpected Array ArrayList LinkedList HashSet BST Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayList LinkedList HashSet BST Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(1)O(n)O(1) LinkedList HashSet BST Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(1)O(n)O(1) LinkedListO(1)O(n) HashSet BST Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(1)O(n)O(1) LinkedListO(1)O(n) HashSetO(1) BST Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(1)O(n)O(1) LinkedListO(1)O(n) HashSetO(1) BSTO(log n)O(n)O(log n) Heap Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(1)O(n)O(1) LinkedListO(1)O(n) HashSetO(1) BSTO(log n)O(n)O(log n) HeapO(log n) Add – adding an element to the data structure

Big O Data StructureBest-CaseWorst-CaseExpected Array ArrayList LinkedList HashSet BST Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayList LinkedList HashSet BST Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(n) LinkedList HashSet BST Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(n) LinkedListO(1)O(n) HashSet BST Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(n) LinkedListO(1)O(n) HashSetO(1) BST Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(n) LinkedListO(1)O(n) HashSetO(1) BSTO(log n)O(n)O(log n) Heap Remove – removing an element from the data structure

Big O Data StructureBest-CaseWorst-CaseExpected ArrayO(n) ArrayListO(n) LinkedListO(1)O(n) HashSetO(1) BSTO(log n)O(n)O(log n) HeapO(log n) Remove – removing an element from the data structure

Big O Few notes on Big O: – Don’t forget that big O tells us the rate at which our runtime grows as our input grows! – O(1) algorithms can take ages to run if they are written poorly: public int findSum(int a, int b) { int sum = 0; for (int i=0; i< ; i++) { sum += a; sum += b; } int result = sum / ; return result; } – The above algorithm is O(1)! But it sucks majorly!

Good luck!