Compsci 201 Binary Trees Recurrence Relations

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Advertisements

Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CPS Wordcounting, sets, and the web l How does Google store all web pages that include a reference to “peanut butter with mustard”  What efficiency.
CPS 100, Spring Trees: no data structure lovelier?
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
CompSci 100e 7.1 From doubly-linked lists to binary trees l Instead of using prev and next to point to a linear arrangement, use them to divide the universe.
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.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Search Trees (BST)
CompSci 100E 15.1 What is a Binary Search?  Magic!  Has been used a the basis for “magical” tricks  Find telephone number ( without computer ) in seconds.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CompSci 100e 7.1 Plan for the week l Recurrences  How do we analyze the run-time of recursive algorithms? l Setting up the Boggle assignment.
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Compsci 201, Compare+Analysis
From doubly-linked lists to binary trees
COMP 53 – Week Fourteen Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
CSC 427: Data Structures and Algorithm Analysis
Week 7 - Friday CS221.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Analysis: Algorithms and Data Structures
PFTFBH Binary Search trees Fundamental Data Structure
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
CSC 222: Object-Oriented Programming
Compsci 201 Recursion, Recurrences, & Trees
COSC160: Data Structures Binary Trees
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
PFTWBH More examples of recursion and alternatives
Compsci 201 Trees and Tradeoffs
What’s the Difference Here?
Compsci 201, Analysis+Markov
Trees.
Trees Lecture 12 CS2110 – Fall 2017.
TAFTW (Take Aways for the Week)
Compsci 201 Priority Queues & Autocomplete
Compsci 201 Trees, Tries, Tradeoffs
ITEC 2620M Introduction to Data Structures
Chapter 17 Object-Oriented Data Structures
Binary Search Trees Why this is a useful data structure. Terminology
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 Lecture 12 CS2110 – Spring 2018.
Monday, April 16, 2018 Announcements… For Today…
Rick Mercer, Allison Obourn, Marty Stepp
Algorithm design and Analysis
Search Sorted Array: Binary Search Linked List: Linear Search
CSE 373: Data Structures and Algorithms
Tree Rotations and AVL Trees
CSE 373 Data Structures and Algorithms
CSC 143 Binary Search Trees.
Compsci 201 Binary Trees Stacks+Queues
CSC 143 Java Searching.
Compsci 201 Priority Queues + Heaps Autocomplete
CS203 Lecture 15.
Data Structures Using C++ 2E
CS 261 – Data Structures AVL Trees.
Compsci 201, O-Notation and Maps (Interfaces too)
Presentation transcript:

Compsci 201 Binary Trees Recurrence Relations Owen Astrachan ola@cs.duke.edu October 24, 2018 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences O is for … Object Oriented Programming with inheritance and classes Open Source Copyright meets the Creative Commons O-Notation Measuring in the limit 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Plan for Today Binary Trees, Binary Search Trees APTs, Recursion Trees in Context: TreeSet Comparable: from trees to sorting How to compare one thing to another Recurrence Relations Measuring recursive algorithms/functions 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Motivation for Trees HashSet and HashMap are O(1) average Astonishing! Search, insert, delete No order for keys, sometimes order matters Worst-case? Everything in same locker/bucket Just in case? Use a tree in that locker/bucket Search Trees: TreeSet and TreeMap O(log N) no matter what, average and worst "Alphabetical" order and range queries Find all keys in range [low,high] efficiently 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Why Trees are O(log N) With each query: eliminate half of tree 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 Can ensure trees are balanced: TreeSet/TreeMap Re-balance on add or delete 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Java-isms for comparing We can compare int, double, char Using ==, and !=, and <, <=, >, >= Can write true != false, but not true > false Cannot write "apple" < "zebra" Must compare objects using specific method Objects must be comparable, that is they must implement the Comparable interface 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Strings are Comparable Compare strings lexicographically, natural ordering, dictionary order “zebra” > “aardvark” but “Zebra” < “aardvark” Conceptual, cannot use < or > or == "yak".compareTo(s) returns < 0, == 0, > 0 s is “zebra”, “yak”, and “toad”, respectively The int convention also used in C++, C, others 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Not Everything is Comparable 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2018, Trees + Recurrences Comparable in Java? String implements Comparable<String> "hello".compareTo("goodbye") Integer implements Comparable<Integer> new Integer(5).compareTo(new Integer(6)) Cannot compare ArrayLists or arrays Note: .equals works for ArrayList, not arrays 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Don't do this at home: (x,y) < (z,w) Can we compare Point objects? http://stackoverflow.com/questions/5178092/sorting-a-list-of-points-with-java Let's look at the Java code that makes a Point comparable to another Point Point implements Comparable<Point> public int compareTo(Point other) 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Build on What You Know How does .equals work? Make sure you have the correct type Cast, compare public boolean equals(Object o) { if (o == null || ! (o instanceof Point)) { return false; } Point p = (Point) o; return p.x == x && p.y == y; 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Compare+Analysis Extend what you know This is method in Point class Point implements Comparable<Point> public int compareTo(Point p) { if (this.x < p.x) return -1; if (this.x > p.x) return 1; // what must be true here? if (this.y < p.y) return -1; if (this.y > p.y) return 1 return 0; } 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Useful math trick: Faster? Care? Use subtraction to help with return values http://stackoverflow.com/questions/2654839/rounding-a-double-to-turn-it-into-an-int-java public int compareTo(Point p) { int deltaX = (int) Math.round(x – p.x); int deltaY = (int) Math.round(y – p.y); if (deltaX == 0) return deltaY; return deltaX; }

Compsci 201, Fall 2017, Compare+Analysis Comparable Elements TreeSet<String>, TreeMap<String,Anything> Tree elements must be comparable Must implement Comparable<..> It's possible to supply a Comparator, later Arrays.sort, Collections.sort What algorithm is used in sorting? Can change order of sort: Comparator, later 9/20/17 Compsci 201, Fall 2017, Compare+Analysis

Compsci 201, Fall 2017, Tree and Tradoffs Jan Cuny Program officer at National Science Foundation (NSF) Leading #CSforAll initiatives. 2009 ABI Woman of Vision Award for Social Impact, 2016 Distinguished Educator Award “All of today’s kids will need – along with reading, writing, and arithmetic – a basic understanding of computation and the role it plays across a wide range of disciplines.” 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs

From Recursion to Recurrence Base case for trees: empty tree and maybe leaf if (t == null) or if (t.left == null && t.right == null) Make recursive calls on subtrees Use results to create return value Solve for one node, recursion does the rest 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Tree function: Tree height Compute tree height (longest root-to-leaf path) int height(Tree root) { if (root == null) return 0; else { return 1 + Math.max(height(root.left), height(root.right)); } Find height of left subtree, height of right subtree Use results to determine height of tree 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Complexity/Efficiency Intuitively: visit every node once for height: O(N) How can we analyze this mathematically? Write a recurrence relation describing runtime Eventually we will solve, but for now? Write Let T(n) be time for height to run on n-node tree Then T(0) = O(1) Then T(n) = O(1) + 2*T(n/2)balanced tree 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Balanced Trees and Complexity A tree is height-balanced if Left and right subtrees are height-balanced Left and right heights differ by at most one Which trees are balanced?

Balanced Trees and Complexity A tree is height-balanced if Left and right subtrees are height-balanced Left and right heights differ by at most one public boolean isBalanced(Tree root){ if (root == null) return true; int lh = height(root.left); int rh = height(root.right); return isBalanced(root.left) && isBalanced(root.right) && Math.abs(lh - rh) <= 1; }

Complexity of isBalanced We know that height(root) is O(N) for N-node tree Recurrence is T(N) = 2T(N/2) + O(1) Recurrence for isBalanced (average case) T(N) is time for isBalanced on N-node tree Call height twice: O(N), each tree N/2 nodes Make two recursive calls 2T(N/2) Recurrence: T(N) = 2T(N/2) + O(N) 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Solve recurrence T(n) is time for isBalanced to run on n-node tree T(n) = 2T(n/2) + n = 2 [2(T(n/4) + n/2] + n = 4T(n/4) + n + n = 4T(n/4) + 2n = 4 [2T(n/8) + n/4] + 2n = 8T(n/8) + 3n = 2kT(n/2k) + kn Eureka! Holds n = 1, 2, … Let n = 2k, so k=log2 n = nT(1) + n log(n) We now have solution to recurrence! O(n log n) -- base 2, but base doesn't matter 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Recurrence Relations No need to derive, remember or look up Recurrence Algorithm Solution T(n) = T(n/2) + O(1) binary search O(log n) T(n) = T(n-1) + O(1) sequential search O(n) T(n) = 2T(n/2) + O(1) tree traversal T(n) = T(n/2) + O(n) quicksort partition T(n) = 2T(n/2) + O(n) mergesort, quicksort O(n log n) T(n) = T(n-1) + O(n) selection or bubble sort O(n2) 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences WOTO http://bit.ly/201fall18-oct24-recur 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Tree APTs Must include TreeNode class in project src Similar to ListNode, not submitted as code Helper method often useful: add a parameter Can be ArrayList<.> or String or … For running/testing outside of APT system? Create tree using code in class folder https://github.com/astrachano/classcode201fall18/blob/master/src/TreeDemo.java 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

LeafSum: https://www2.cs.duke.edu/csed/newapt/leafsum.html Sum all the values in leaves of tree Base cases? Recursive calls? What should big-Oh be? N-node tree? Visit each node …. Balanced or stringy … Similar to tree height

Compsci 201, Fall 2017, Tree and Tradoffs LeafSum correct? What do we do with null tree? Why? From base-case to combining recursive calls Recurrence expression? public class LeafSum { public int sum(TreeNode t) { if (t == null) return 0; // something is missing here! return sum(t.left) + sum(t.right); } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs

Compsci 201, Fall 2017, Tree and Tradoffs LeafSum Now Correct! What do we do with a leaf, why? Why else isn’t needed, but ok Why this is still O(n) for ALL TREES! public class LeafSum { public int sum(TreeNode t) { if (t == null) return 0; if (t.left == null && t.right == null) return t.info; return sum(t.left) + sum(t.right); } 11/3/17 Compsci 201, Fall 2017, Tree and Tradoffs

Compsci 201, Fall 2018, Trees + Recurrences AllPaths https://www2.cs.duke.edu/csed/newapt/allpaths.html Use instance variable or parameter to helper Path is parameter to helper method Invariant: path from root to current node 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Compsci 201, Fall 2018, Trees + Recurrences Administrivia Looking at calendar: what's coming Assignments, APTs, APT quizzes APT Quiz1 Redux (system or performance issues) Saturday-Monday Final exam reminder Changes not happening 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences

Using TreeNode internally LinkedList class used doubly-linked list TreeSet and TreeMap use balanced tree https://github.com/astrachano/diyad-treestuff Source code: Balanced Red/Black tree where nodes have left, right, parent pointer http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/TreeMap.java 10/24/2018 Compsci 201, Fall 2018, Trees + Recurrences