CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
1 CSE 326: Data Structures Part Four: Trees Henry Kautz Autumn 2002.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
(B+-Trees, that is) Steve Wolfman 2014W1
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act Steve Wolfman 2014W1 1.
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 ),
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
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.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2010W2 1.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees continued Aaron Bauer Winter 2014 CSE373: Data Structures & Algorithms1.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CSE 373 Data Structures Lecture 7
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 373 Binary search trees; tree height and balance
CSE 326: Data Structures Lecture #8 Pruning (and Pruning for the Lazy)
CPSC 221: Data Structures Lecture #5 Branching Out
Binary Search Trees.
Binary Search Trees.
Problems with Linked List (as we’ve seen so far…)
Programming Abstractions
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Revised based on textbook author’s notes.
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 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CMSC 341: Data Structures Priority Queues – Binary Heaps
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE 332: Data Abstractions Binary Search Trees
CSE 326: Data Structures Lecture #7 Dendrology
CPSC 221: Data Structures Lecture #5 Branching Out
CSE 326: Data Structures Lecture #8 Balanced Dendrology
CSE 326: Data Structures Lecture #7 Binary Search Trees
Presentation transcript:

CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1

Today’s Outline Binary Trees Dictionary ADT Binary Search Trees Deletion Some troubling questions 2

Binary Trees Binary tree is either –empty (NULL for us), or –a datum, a left subtree, and a right subtree Properties –max # of leaves: –max # of nodes: Representation: A B DE C F HG JI Data right pointer left pointer 3

Representation A right pointer left pointer A B DE C F B right pointer left pointer C right pointer left pointer D right pointer left pointer E right pointer left pointer F right pointer left pointer struct Node { KTYPE key; DTYPE data; Node * left; Node * right; }; 4

Today’s Outline Binary Trees Dictionary ADT Binary Search Trees Deletion Some troubling questions 5

What We Can Do So Far Stack –Push –Pop Queue –Enqueue –Dequeue What’s wrong with Lists? List –Insert –Remove –Find Priority Queue (skipped!) –Insert –DeleteMin 6

Dictionary ADT Dictionary operations –create –destroy –insert –find –delete Stores values associated with user-specified keys –values may be any (homogenous) type –keys may be any (homogenous) comparable type midterm –would be tastier with brownies prog-project –so painful… who designed this language? wolf –the perfect mix of oomph and Scrabble value insert find(wolf) brownies - tasty wolf - the perfect mix of oomph and Scrabble value 7

Search/Set ADT Dictionary operations –create –destroy –insert –find –delete Stores keys –keys may be any (homogenous) comparable –quickly tests for membership Berner Whippet Alsatian Sarplaninac Beardie Sarloos Malamute Poodle insert find(Wolf) Min Pin NOT FOUND 8

A Modest Few Uses Arrays and “Associative” Arrays Sets Dictionaries Router tables Page tables Symbol tables C++ Structures 9

Desiderata Fast insertion –runtime: Fast searching –runtime: Fast deletion –runtime: 10

Naïve Implementations Linked list Unsorted array Sorted array insert delete find worst one… yet so close! 11

Today’s Outline Binary Trees Dictionary ADT Binary Search Trees Deletion Some troubling questions 12

Binary Search Tree Dictionary Data Structure Binary tree property –each node has  2 children –result: storage is small operations are simple average depth is small * Search tree property –all keys in left subtree smaller than root’s key –all keys in right subtree larger than root’s key –result: easy to find any given key 13 * Technically: a result of both properties.

Example and Counter-Example BINARY SEARCH TREE NOT A BINARY SEARCH TREE

In Order Listing In order listing: 2  5  7  9  10  15  17  20  30 struct Node { KTYPE key; DTYPE data; Node * left; Node * right; }; 15

Finding a Node Node *& find(Comparable key, Node *& root) { if (root == NULL) return root; else if (key key) return find(key, root->left); else if (key > root->key) return find(key, root->right); else return root; } runtime: a.O(1) b.O(lg n) c.O(n) d.O(n lg n) e.None of these 16

Finding a Node Node *& find(Comparable key, Node *& root) { if (root == NULL) return root; else if (key key) return find(key, root->left); else if (key > root->key) return find(key, root->right); else return root; } WARNING: Much fancy footwork with refs (&) coming. You can do all of this without refs... just watch out for special cases. 17

Iterative Find Node * find(Comparable key, Node * root) { while (root != NULL && root->key != key) { if (key key) root = root->left; else root = root->right; } return root; } Look familiar? (It’s trickier to get the ref return to work here.) 18

Insert runtime: // Precondition: key is not // already in the tree! void insert(Comparable key, Node * root) { Node *& target(find(key, root)); assert(target == NULL); target = new Node(key); } Funky game we can play with the *& version. 19

Digression: Value vs. Reference Parameters Value parameters (Object foo) –copies parameter –no side effects Reference parameters (Object & foo) –shares parameter –can affect actual value –use when the value needs to be changed Const reference parameters (Object const & foo) –shares parameter –cannot affect actual value –use when the value is too big for copying in pass-by-value 20

BuildTree for BSTs Suppose the data 1, 2, 3, 4, 5, 6, 7, 8, 9 is inserted into an initially empty BST: –in order –in reverse order –median first, then left median, right median, etc. so: 5, 3, 8, 2, 4, 7, 9, 1, 6 21

Analysis of BuildTree Worst case: O(n 2 ) as we’ve seen Average case assuming all orderings equally likely turns out to be O(n lg n). 22

Bonus: FindMin/FindMax Find minimum Find maximum

Double Bonus: Successor Find the next larger node in this node’s subtree. Node *& succ(Node *& root) { if (root->right == NULL) return root->right; else return min(root->right); } Node *& min(Node *& root) { if (root->left == NULL) return root; else return min(root->left); }

More Double Bonus: Predecessor Find the next smaller node in this node’s subtree. Node *& pred(Node *& root) { if (root->left == NULL) return root->left; else return max(root->left); } Node *& max(Node *& root) { if (root->right == NULL) return root; else return max(root->right); }

Today’s Outline Some Tree Review (here for reference, not discussed) Binary Trees Dictionary ADT Binary Search Trees Deletion Some troubling questions 26

Deletion Why might deletion be harder than insertion? 27

Lazy Deletion Instead of physically deleting nodes, just mark them as deleted (with a “tombstone”) +simpler +physical deletions done in batches +some adds just flip deleted flag –small amount of extra memory for deleted flag –many tombstones slow finds –some operations may have to be modified (e.g., min and max)

Lazy Deletion Delete(17) Delete(15) Delete(5) Find(9) Find(16) Insert(5) Find(17) 29

Deletion - Leaf Case Delete(17) 30

Deletion - One Child Case Delete(15) 31

Deletion - Two Child Case Delete(5) 32

Finally…

Delete Code void delete(Comparable key, Node *& root) { Node *& handle(find(key, root)); Node * toDelete = handle; if (handle != NULL) { if (handle->left == NULL) { // Leaf or one child handle = handle->right; } else if (handle->right == NULL) { // One child handle = handle->left; } else { // Two child case Node *& successor(succ(handle)); handle->data = successor->data; toDelete = successor; successor = successor->right; // Succ has <= 1 child } delete toDelete; } Refs make this short and “elegant”… but could be done without them with a bit more work. 34

Today’s Outline Some Tree Review (here for reference, not discussed) Binary Trees Dictionary ADT Binary Search Trees Deletion Some troubling questions 35

Thinking about Binary Search Trees Observations –Each operation views two new elements at a time –Elements (even siblings) may be scattered in memory –Binary search trees are fast if they’re shallow Realities –For large data sets, disk accesses dominate runtime –Some deep and some shallow BSTs exist for any data 36

Solutions? Reduce disk accesses? Keep BSTs shallow? 37

To Do Continue readings on website! 38

Coming Up cilk_spawn Parallelism and Concurrency cilk_spawn Self-balancing Binary Search Trees cilk_spawn Priority Queues cilk_spawn Sorting (most likely!) Huge Search Tree Data Structure cilk_join Spawns parallel task. Since we have only one classroom, one of these goes first! 39