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

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

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

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

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

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

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

CS24 Week 8 Lecture 1

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

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Binomial Queue deletemin ADTs Seen So Far

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

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

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

CSE 326: Data Structures Binary Search Trees

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

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

Tree traversals and binary trees

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

3137 Data Structures and Algorithms in C++

Data Structure Advanced

Balanced Binary Search Trees. Victor Gao

Programming Abstractions

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Recall: Properties of B-Trees

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

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

Section 4 SOLUTION: AVL Trees & B-Trees

CSE 326: Data Structures Lecture #8 Binary Search Trees

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

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

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

TREES. Trees - Introduction

CSE 214 Computer Science II Heaps and Priority Queues

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

3137 Data Structures and Algorithms in C++

Programming Abstractions

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

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

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

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

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Programming II (CS300)

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

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Priority Queues Heaps Heapsort

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

UNIT III BALANCED SEARCH TREES AND INDEXING

Dictionaries. Priority Queues

Trees. CSE 373 Data Structures

Lecture 23: Binary Search Trees

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

Why Do We Need Trees?

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

Programming II (CS300)

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

8. Binary Search Tree

Binary Trees. CSE 326: Data Structures Lecture #7 Binary Search Trees. Naïve Implementations. Dictionary & Search ADTs

CS301 - Data Structures Glossary By

CmpSci 187: Programming with Data Structures Spring 2015

CMSC 341 Lecture 14: Priority Queues, Heaps

CMSC 341 Lecture 10 Binary Search Trees

Fall, 2015 Prof. Jungkeun Park

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

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

Binary Search Trees Treesort

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

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Need Backtracking! L 0 A 1 B 2 K 3 P 4 C 5 I 6 Z 7 S 8 A 9 N 10 X 11 A 12 N 13 X 14 P 15

9. Heap : Priority Queue

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

Binary Trees, Binary Search Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Trees. A tree is a directed graph with the property

TREES Lecture 12 CS2110 Spring 2019

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Binary Search Trees. Analysis of Algorithms

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Algorithms. AVL Tree

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

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

Data Structures in Java

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

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

CSE 530A. B+ Trees. Washington University Fall 2013

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

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

ECE 242. Data Structures

CSE373: Data Structures & Algorithms Priority Queues

Postfix (and prefix) notation

CS350: Data Structures Red-Black Trees

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

NET/JRF-COMPUTER SCIENCE & APPLICATIONS. Time: 01 : 00 Hour Date : M.M. : 50

FINAL EXAM REVIEW CS 200 RECITATIOIN 14

Transcription:

CSE373: Data Structures & lgorithms Lecture 5: Dictionaries; Binary Search Trees aron Bauer Winter 2014

Where we are Studying the absolutely essential DTs of computer science and classic data structures for implementing them DTs so far: 1. Stack: push, pop, isempty, 2. Queue: enqueue, dequeue, isempty, Next: 3. Dictionary (a.k.a. Map): associate keys with values Extremely common 2

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(awb,.) find(miley) Miley, Cyrus, Will tend to emphasize the keys; don t forget about the stored values awb aron Bauer ljames Lebron James miley Miley Cyrus 3

Comparison: The Set DT The Set DT is like a Dictionary without any values key is present or not (no repeats) For find, insert, delete, there is little difference In dictionary, values are just along for the ride So same data-structure ideas work for dictionaries and sets But if your Set DT has other important operations this may not hold union, intersection, is_subset Notice these are binary operators on sets 4

Dictionary data structures There are many good data structures for (large) dictionaries 1. VL trees Binary search trees with guaranteed balancing 2. B-Trees lso always balanced, but different and shallower B!=Binary; B-Trees generally have large branching factor 3. Hashtables Not tree-like at all Skipping: Other balanced trees (e.g., red-black, splay) But first some applications and less efficient implementations 5

Modest Few Uses 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 6

Simple implementations For dictionary with n key/value pairs Unsorted linked-list insert find delete Unsorted array Sorted linked list Sorted array 7

Simple implementations For dictionary with n key/value pairs insert find delete Unsorted linked-list 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 8

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 find O(log m) time where m is data-structure size (okay) May complicate other operations 9

Tree terms (review?) Tree T root(tree) leaves(tree) depth(node) height(tree) B C children(node) parent(node) degree(node) branching factor(tree) D E F G siblings(node) ancestors(node) H I descendents(node) subtree(node) J K L M N 10

Some tree terms (mostly review) 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 11

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

Binary Trees Binary tree is empty or root (with data) left subtree (may be empty) right subtree (may be empty) Representation: B C Data D E F left pointer right pointer G H For a dictionary, data will include a key and a value I J 13

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: max # of nodes: min # of leaves: min # of nodes: Winter 2014 CSE373: Data Structures & lgorithms 14

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: min # of nodes: 15

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: max # of nodes: 2 h 2 (h + 1) - 1 min # of leaves: min # of nodes: 16

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: max # of nodes: min # of leaves: 2 h 2 (h + 1) - 1 1 min # of nodes: 17

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: max # of nodes: min # of leaves: min # of nodes: 2 h 2 (h + 1) - 1 1 h + 1 For n nodes, we cannot do better than O(log n) height, and we want to avoid O(n) height 18

Calculating height What is the height of a tree with root root? int treeheight(node root) {??? 19

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 20

Tree Traversals traversal is an order for visiting all the nodes of a tree Pre-order: root, left subtree, right subtree + In-order: left subtree, root, right subtree * 5 Post-order: left subtree, right subtree, root 2 4 (an expression tree) 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 * 5 Post-order: left subtree, right subtree, root 2 4 (an expression tree) 22

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 Post-order: left subtree, right subtree, root * 2 4 5 (an expression tree) 23

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

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 29

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 30

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 31

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 32

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E C F G = current node = processing (on the call stack) = completed node = element has been processed 33

More on traversals void inordertraversal(node t){ if(t!= null) { inordertraversal(t.left); process(t.element); inordertraversal(t.right); D B E C F G = current node = processing (on the call stack) = completed node = element has been processed 34