CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.
CSE 326 Binary Search Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Ch. 13: Balanced Search Trees Symbol table: insert, delete, find, pred, succ, sort,… Binary Search Tree review: What is a BST? binary tree with a key at.
Marc Smith and Jim Ten Eyck
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Splay Trees and B-Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
COSC2007 Data Structures II
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
B+ Trees COMP
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Starting at Binary Trees
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
Internal and External Sorting External Searching
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Kevin Quinn Fall 2015.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2012.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms More Heaps; Dictionaries; Binary Search Trees Riley Porter Winter 2016 Winter 2017.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Spring 2016.
Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Cse 373 April 26th – Exam Review.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Data Structures and Database Applications Binary Trees in C#
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Heaps and the Heapsort Heaps and priority queues
CSE 326: Data Structures Priority Queues & Binary Heaps
Presentation transcript:

CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 2015

Today’s Outline Announcements -Homework 1 due TODAY at 10:59pm -Homework 2 out -Due online next Friday 10:59 pm Today’s Topics Finish Asymptotic Analysis Dictionary ADT (a.k.a. Map): associate keys with values –Extremely common Binary Trees 2

Summary of Asymptotic Analysis Analysis can be about: The problem or the algorithm (usually algorithm) Time or space (usually time) Best-, worst-, or average-case (usually worst) Upper-, lower-, or tight-bound (usually upper) The most common thing we will do is give an O upper bound to the worst-case running time of an algorithm. 3

Addendum: Timing vs. Big-O Summary Big-O –Examine the algorithm itself, not the implementation –Reason about performance as a function of n –For small n, an algorithm with worse asymptotic complexity might be faster Timing –Compare implementations –Focus on data sets other than worst case –Determine what the constants actually are 4

Let’s take a breath So far we’ve covered –Simple ADTs: stacks, queues, lists –Some math (proof by induction) –Algorithm analysis –Asymptotic notation (Big-Oh) Coming up…. –Many more ADTs! Starting with dictionaries 5

The Dictionary (a.k.a. Map) ADT Data: –set of (key, value) pairs –keys must be comparable Operations: –insert(key,value) –find(key) –delete(key) –…–… 6 lauren Lauren Milne OH: Mon … mert Mert Saglam OH: Wed 4-5 … Mauricio Mauricio Hernandez OH: Fri 12:00-1:00 … insert(lauren, ….) find(mert) Mert Saglam, …

A Modest Few Uses Used to store information with some key and retrieve it efficiently –Lots of programs do that! Search: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 ADT 7

Simple implementations For dictionary with n key/value pairs insert find delete Unsorted linked-list Unsorted array Sorted linked list Sorted array * 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 O(1)*O(n) O(1)* O(n) O(log n)

Lazy Deletion A general technique for making delete as fast as find : –Instead of actually removing the item just mark it deleted Pros: –Simpler –Can do removals later in batches –If re-added soon thereafter, just unmark the deletion Cons: –Extra space for the “is-it-deleted” flag –Data structure full of deleted nodes wastes space –May complicate other operations  

Better dictionary data structures There are many good data structures for (large) dictionaries 1.Binary trees 2.AVL trees –Binary search trees with guaranteed balancing 3.B-Trees –Also always balanced, but different and shallower –B-Trees are not the same as Binary Trees B-Trees generally have large branching factor 4.Hashtables –Not tree-like at all Skipping: Other balanced trees (e.g., red-black, splay) 10

Tree terms 11 A E B DF C G IH LJMKN Tree T Root (tree) Leaves (tree) Children (node) Parent (node) Siblings (node) Ancestors (node) Descendents (node) Subtree (node) Depth (node) Height (tree) Degree (node) Branching factor (tree)

More tree terms There are many kinds of trees –Binary trees, linked lists, etc… There are many kinds of binary trees –binary search tree, binary heaps A tree can be balanced or not –A balanced tree with n nodes has a height of O( log n) –Use different “balance conditions” to achieve this 12

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 13 What is the height of a perfect binary tree with n nodes? A complete 14-ary tree?

Binary Trees Binary tree: Each node has at most 2 children (branching factor 2) Binary tree is –A root (with data) –A left subtree (may be empty) –A right subtree (may be empty) Representation: A B DE C F HG JI Data right pointer left pointer For a dictionary, data will include a key and a value 14

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

Calculating height What is the height of a tree with root root ? 17 int treeHeight(Node root) { ??? }

Calculating height What is the height of a tree with root root ? 18 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

Tree Traversals A 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 Post-order:left subtree, right subtree, root + * 24 5 (an expression tree) 19

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 20 = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 21 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 22 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 23 = current node= processing (on the call stack) = completed node A A A ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 24 = current node= processing (on the call stack) = completed node A A A ✓ ✓ = current node= processing (on the call stack) = completed node= element has been processed A A A ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 25 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ ✓ ✓✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 26 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ ✓ ✓✓ ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 27 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ ✓ ✓✓ ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 28 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ ✓ ✓✓ ✓ ✓ ✓

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG 29 = current node= processing (on the call stack) = completed node A A A = current node= processing (on the call stack) = completed node= element has been processed A A A ✓ ✓ ✓✓ ✓ ✓ ✓✓

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