CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees. Linda Shapiro Spring 2016

Similar documents
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees. Aaron Bauer Winter 2014

Where we are. CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. The Dictionary (a.k.a. Map) ADT

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees. Linda Shapiro Spring 2016

Trees. (Trees) Data Structures and Programming Spring / 28

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 100 Advanced Data Structures

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE332: Data Abstractions Lecture 7: B Trees. James Fogarty Winter 2012

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Trees. CSE 373 Data Structures

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CS301 - Data Structures Glossary By

Binomial Queue deletemin ADTs Seen So Far

Algorithms. AVL Tree

ECE250: Algorithms and Data Structures Midterm Review

Friday Four Square! 4:15PM, Outside Gates

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CSE 332: Data Structures & Parallelism Lecture 3: Priority Queues. Ruth Anderson Winter 2019

CPSC 221: Data Structures Lecture #5. CPSC 221: Data Structures Lecture #5. Learning Goals. Today s Outline. Tree Terminology.

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

INF2220: algorithms and data structures Series 1

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Data Structures Question Bank Multiple Choice

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Why Do We Need Trees?

Recall: Properties of B-Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CS24 Week 8 Lecture 1

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Course Review. Cpt S 223 Fall 2009

TREES Lecture 12 CS2110 Spring 2019

Priority Queues Heaps Heapsort

Binary Trees, Binary Search Trees

Dictionaries. Priority Queues

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

INF2220: algorithms and data structures Series 1

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

Binary Trees

Data Structures and Algorithms

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Linda Shapiro Spring 2016

Binary Search Trees Treesort

Binary Heaps in Dynamic Arrays

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CMSC 341 Lecture 14: Priority Queues, Heaps

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Balanced Search Trees Date: 9/20/18

CSE 5311 Notes 4a: Priority Queues

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues. Aaron Bauer Winter 2014

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CSE373: Data Structures & Algorithms Lecture 9: Priority Queues and Binary Heaps. Nicki Dell Spring 2014

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

Programming II (CS300)

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 326: Data Structures Lecture #8 Binary Search Trees

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

A set of nodes (or vertices) with a single starting point

Balanced Binary Search Trees. Victor Gao

Recitation 9. Prelim Review

CSE 326: Data Structures Binary Search Trees

TREES. Trees - Introduction

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Section 05: Solutions

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

Analysis of Algorithms

CSE 373: Data Structures and Algorithms

UNIT III BALANCED SEARCH TREES AND INDEXING

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Algorithms and Data Structures

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

CSE373 Fall 2013, Midterm Examination October 18, 2013

Lecture 6: Analysis of Algorithms (CS )

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

Section 4 SOLUTION: AVL Trees & B-Trees

Programming Abstractions

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Lecture 23: Binary Search Trees

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Transcription:

CSE373: Data Structures & lgorithms Lecture 5: Dictionary DTs; Binary Trees Linda Shapiro Spring 2016

Today s Outline nnouncements - Homework 1 due TODY at 11:59 pm - Homework 2 out (paper and pencil assignment) - Due in class Wednesday pril 13 at the STRT of class Today s Topics Finish symptotic nalysis Dictionary DT (a.k.a. Map): associate keys with values Extremely common Binary Trees Spring 2016 CSE 373 lgorithms and Data Structures 2

Summary of symptotic nalysis nalysis can be about: The problem or the algorithm (usually algorithm) Time or space (usually time) Or power or dollars or Best-, worst-, or average-case (usually worst) Upper-, lower-, or tight-bound (usually upper) g(n) f(n) O g(n) f(n) Ω both θ The most common thing we will do is give an O upper bound to the worst-case running time of an algorithm. Spring 2016 CSE 373 lgorithms and Data Structures 3

How to apply the definition easily Theory (mine) Let g(n) = c1*g1(n) + c2*g2(n) +... + ck*gk(n) + c0 Suppose the functions g1, g2,... gk are already arranged in order with highest complexity at the far left. Select the LRGEST of the constants C = max(c1, c2,..ck, c0) Then g(n) C*g1(n) + C*g2(n) +... + C*gk(n) + C Or g(n) C(g1(n) + g2(n) +... + gk(n) + 1) But g1(n) is bigger than g2(n) and all the others beyond some known n0. So g(n) C(g1(n) + g1(n) +... +g1(n) + g1(n)) Or g(n) C(k+1)*g1(n) = C *g1(n) for all n greater than n0. Spring 2016 CSE 373 lgorithms and Data Structures 4

Example g(n) = 25 n 4 + 30 n 2 + 100 nlogn + 54 g(n) 100 n 4 + 100n 2 + 100 nlogn + 100 g(n) 100(n 4 + n 2 + nlogn + 1) g(n) 100(n 4 + n 4 + n 4 + n 4 ) g(n) 100*4*n 4 g(n) 400*n 4 for all n 1 Spring 2016 CSE 373 lgorithms and Data Structures 5

Big-Oh Caveats symptotic complexity focuses on behavior for large n and is independent of any computer / coding trick But you can abuse it to be misled about trade-offs Example: n 1/10 vs. log n symptotically n 1/10 grows more quickly But the cross-over point is around 5 * 10 17 So if you have input size less than 2 58, prefer n 1/10 For small n, an algorithm with worse asymptotic complexity might be faster If you care about performance for small n then the constant factors can matter Spring 2016 CSE 373 lgorithms and Data Structures 6

ddendum: Timing vs. Big-Oh Summary Big-oh is an essential part of computer science s mathematical foundation Examine the algorithm itself, not the implementation Reason about (even prove) performance as a function of n Timing also has its place Compare implementations Focus on data sets you care about (versus worst case) Determine what the constant factors really are Spring 2016 CSE 373 lgorithms and Data Structures 7

Let s take a breath So far we ve covered Some simple DTs: stacks, queues, lists Some math (proof by induction) How to analyze algorithms symptotic notation (Big-Oh) Coming up. Many more DTs Starting with dictionaries Spring 2016 CSE 373 lgorithms and Data Structures 8

The Dictionary (a.k.a. Map) DT Data: set of (key, value) pairs keys must be comparable Operations: insert(key,value) find(key) delete(key) insert(ezgi,.) find(bran) Bran Hagger, Will tend to emphasize the keys; don t forget about the stored values ezgi Ezgi Mercan OH: Thurs 10.30-11.30 mert Mert Sagalm OH: TTH 3.30-4.30 bran Bran Hagger OH: Mon 10.00-11.00 Spring 2016 CSE 373 lgorithms and Data Structures 9

Modest Few Uses key attr1 attr2 attr3 k1 v11 v12 v13 k2 v21 v22 v23 ny time you want to store information according to some key and be able to retrieve it efficiently Lots of programs do that! Search: inverted indexes, phone directories, Networks: router tables Operating systems: page tables Compilers: symbol tables Databases: dictionaries with other nice properties Biology: genome maps Possibly the most widely used DT Spring 2016 CSE 373 lgorithms and Data Structures 10

Simple implementations For dictionary with n key/value pairs Unsorted linked-list insert find delete O(1)* O(n) O(n) Unsorted array O(1)* O(n) O(n) Sorted linked list O(n) O(n) O(n) Sorted array O(n) O(log n) O(n) * Unless we need to check for duplicates We ll see a Binary Search Tree (BST) probably does better but not in the worst case (unless we keep it balanced) Spring 2016 CSE 373 lgorithms and Data Structures 11

Lazy Deletion general technique for making delete as fast as find: Instead of actually removing the item just mark it deleted Plusses: Simpler Can do removals later in batches If re-added soon thereafter, just unmark the deletion Minuses: 10 12 24 30 41 42 44 45 50 Extra space for the is-it-deleted flag Data structure full of deleted nodes wastes space May complicate other operations Spring 2016 CSE 373 lgorithms and Data Structures 12

Better dictionary data structures There are many good data structures for (large) dictionaries 1. Binary trees 2. VL trees Binary search trees with guaranteed balancing 3. B-Trees lso always balanced, but different and shallower B-Trees are not the same as Binary Trees B-Trees generally have large branching factor 4. Hash Tables Not tree-like at all Skipping: Other balanced trees (e.g., red-black, splay) Spring 2016 CSE 373 lgorithms and Data Structures 13

Tree terms (review?) Tree T Depth 0 Root (tree) Leaves (tree) Depth (node) Height (tree) 4 B C Depth 1 Children (node) Parent (node) Degree (node) Branching factor (tree) D E F G Depth 2 Siblings (node) ncestors (node) Depth 3 H I Descendents (node) Subtree (node) Depth 4 J K L M N Spring 2016 CSE 373 lgorithms and Data Structures 14

More tree terms There are many kinds of trees Every binary tree is a tree Every list is kind of a tree (think of next as the one child) There are many kinds of binary trees Every binary search tree is a binary tree Later: binary heap is a different kind of binary tree tree can be balanced or not balanced tree with n nodes has a height of O(log n) Different tree data structures have different balance conditions to achieve this Spring 2016 CSE 373 lgorithms and Data Structures 15

Kinds of trees Certain terms define trees with specific structure Binary tree: Each node has at most 2 children (branching factor 2) n-ary tree: Each node has at most n children (branching factor n) Perfect tree: Each row completely full Complete tree: Each row completely full except maybe the bottom row, which is filled from left to right What is the height of a perfect binary tree with n nodes? complete binary tree? log 2 n Spring 2016 CSE 373 lgorithms and Data Structures 16

Binary Trees Binary tree: Each node has at most 2 children (branching factor 2) Binary tree is root (with data) left subtree that s a binary tree right subtree that s a binary tree These subtrees may be empty. Representation: D B E C F left pointer Data right pointer G H For a dictionary, data will include a key and a value I J Spring 2016 CSE 373 lgorithms and Data Structures 17

Binary Tree Representation Spring 2016 CSE 373 lgorithms and Data Structures 18

Binary Trees: Some Numbers Recall: height of a tree = longest path from root to leaf (count edges) For binary tree of height h: max # of leaves: 2 h max # of nodes: min # of leaves: 2 (h + 1) - 1 1 min # of nodes: h + 1 For n nodes, we cannot do better than O(log n) height and we want to avoid O(n) height Spring 2016 CSE 373 lgorithms and Data Structures 19

Calculating height What is the height of a tree with root root? int treeheight(node root) {??? Spring 2016 CSE 373 lgorithms and Data Structures 20

Calculating height What is the height of a tree with root root? int treeheight(node root) { if(root == null) return -1; return 1 + max(treeheight(root.left), treeheight(root.right)); Running time for tree with n nodes: O(n) single pass over tree Note: non-recursive is painful need your own stack of pending nodes; much easier to use recursion s call stack Spring 2016 CSE 373 lgorithms and Data Structures 21

Tree Traversals traversal is an order for visiting all the nodes of a tree Pre-order: root, left subtree, right subtree + * 2 4 5 + In-order: left subtree, root, right subtree 2 * 4 + 5 * 5 Post-order: left subtree, right subtree, root 2 4 * 5 + 2 4 (an expression tree) Spring 2016 CSE 373 lgorithms and Data Structures 22

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 23

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 24

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 25

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 26

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 27

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 28

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B C E F G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 29

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B C E F G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 30

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B C E F G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 31

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B C E F G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 32

More on traversals void preordertraversal(node t){ if(t!= null) { process(t.element); preordertraversal(t.left); preordertraversal(t.left) D B E F C G = current node = processing (on the call stack) = completed node = element has been processed Spring 2016 CSE 373 lgorithms and Data Structures 33

Preorder Exercise Q R S T U v w Spring 2016 CSE 373 lgorithms and Data Structures 34