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

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.
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
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.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
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.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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 ),
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
data ordered along paths from root to leaf
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Starting at Binary Trees
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
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.
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Lauren Milne Summer 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.
Instructor: Lilian de Greef Quarter: Summer 2017
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
CSE373: Data Structures & Algorithms
Instructor: Lilian de Greef Quarter: Summer 2017
Trees Chapter 15.
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.
Programming Abstractions
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Hunter Zahn Summer 2016 Summer 2016.
Tree data structure.
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.
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
Tree data structure.
CSE332: Data Abstractions Lecture 4: Priority Queues
CSE 332: Data Abstractions Binary Search Trees
Presentation transcript:

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

Where we are Studying the absolutely essential ADTs of computer science and classic data structures for implementing them ADTs 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 Winter 20142CSE373: Data Structures & Algorithms

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) –…–… awb Aaron Bauer … ljames Lebron James … miley Miley Cyrus … insert(awb, ….) find(miley) Miley, Cyrus, … Will tend to emphasize the keys; don’t forget about the stored values Winter 20143CSE373: Data Structures & Algorithms

Comparison: The Set ADT The Set ADT is like a Dictionary without any values –A 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 ADT has other important operations this may not hold –union, intersection, is_subset –Notice these are binary operators on sets Winter 20144CSE373: Data Structures & Algorithms

Dictionary data structures There are many good data structures for (large) dictionaries 1.AVL trees –Binary search trees with guaranteed balancing 2.B-Trees –Also 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… Winter 20145CSE373: Data Structures & Algorithms

A Modest Few Uses Any 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 … Winter 20146CSE373: Data Structures & Algorithms

Simple implementations For dictionary with n key/value pairs insert find delete Unsorted linked-list Unsorted array Sorted linked list Sorted array Winter 20147CSE373: Data Structures & Algorithms

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 Winter 20148CSE373: Data Structures & Algorithms

Lazy Deletion A 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: –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 Winter 20149CSE373: Data Structures & Algorithms  

Tree terms (review?) Winter CSE373: Data Structures & Algorithms 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)

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: A binary heap is a different kind of binary tree A tree can be balanced or not –A balanced tree with n nodes has a height of O( log n) –Different tree data structures have different “balance conditions” to achieve this Winter CSE373: Data Structures & Algorithms

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 Winter CSE373: Data Structures & Algorithms What is the height of a perfect binary tree with n nodes? A complete binary tree?

Binary Trees Binary tree is empty or –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 Winter CSE373: Data Structures & Algorithms

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: –max # of nodes: –min # of leaves: –min # of nodes: Winter 2014

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: 2h2h Winter CSE373: Data Structures & Algorithms

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: 2h2h 2 (h + 1) - 1 Winter CSE373: Data Structures & Algorithms

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: 2h2h 2 (h + 1) Winter CSE373: Data Structures & Algorithms

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: 2h2h 2 (h + 1) h + 1 For n nodes, we cannot do better than O( log n) height, and we want to avoid O(n) height Winter CSE373: Data Structures & Algorithms

Calculating height What is the height of a tree with root root ? Winter CSE373: Data Structures & Algorithms int treeHeight(Node root) { ??? }

Calculating height What is the height of a tree with root root ? Winter CSE373: Data Structures & Algorithms 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) Winter CSE373: Data Structures & Algorithms

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) Winter CSE373: Data Structures & Algorithms

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 + * 24 5 (an expression tree) Winter CSE373: Data Structures & Algorithms

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) Winter CSE373: Data Structures & Algorithms

More on traversals void inOrderTraversal(Node t){ if(t != null) { inOrderTraversal(t.left); process(t.element); inOrderTraversal(t.right); } A B DE C FG Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 Winter CSE373: Data Structures & Algorithms = 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 ✓ ✓ ✓✓ ✓ ✓ ✓✓