CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000.

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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
CSE332: Data Abstractions Lecture 4: Priority Queues Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Dan Grossman Spring 2010.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
CSE373: Data Structures & Algorithms Lecture 5: Dictionaries; Binary Search Trees Aaron Bauer Winter 2014.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
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
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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 ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2011W2 1.
CPSC 221: Data Structures Lecture #5 Branching Out Steve Wolfman 2014W1 1.
CSE332: Data Abstractions Lecture 6: Dictionaries; Binary Search Trees Tyler Robison Summer
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.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000.
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 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.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees Dan Grossman Fall 2013.
CSE 373: Data Structures and Algorithms Lecture 11: Priority Queues (Heaps) 1.
CSE 326: Data Structures Lecture #14 Whoa… Good Hash, Man Steve Wolfman Winter Quarter 2000.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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 326: Data Structures Lecture #6 Putting Our Heaps Together Steve Wolfman Winter Quarter 2000.
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.
CSE 373 Data Structures Lecture 7
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms Lecture 5: Dictionary ADTs; Binary Trees Linda Shapiro Winter 2015.
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Instructor: Lilian de Greef Quarter: Summer 2017
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CPSC 221: Data Structures Lecture #5 Branching Out
Binary Search Trees.
Source: Muangsin / Weiss
CSE 373 Data Structures Lecture 7
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
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CSE 373: Data Structures and Algorithms
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Trees CSE 373 Data Structures.
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Binary Search Trees
CSE 326: Data Structures Lecture #7 Dendrology
CSE 373 Data Structures and Algorithms
CPSC 221: Data Structures Lecture #5 Branching Out
Binary SearchTrees [CLRS] – Chap 12.
CSE 326: Data Structures Lecture #7 Binary Search Trees
Presentation transcript:

CSE 326: Data Structures Lecture #7 Branching Out Steve Wolfman Winter Quarter 2000

Today’s Outline Talking about HW #2 Some Tree Review Binary Trees Dictionary ADT Binary Search Trees

HW #2 Solutions are online – –navigate to assignments Good work! If you got 5/8 or less, come talk to us If you don’t understand anything on the quiz, come talk to us or us –understand the solution even if you got it right on the quiz! Problems #3 and #8

Problem #3 int Albatross(int n) If n=1 Then Return End If For i=1 to n Print ``Do you see the albatross now?'' End For Albatross(n/2)

Problem #8: How is a Queue like a Stack like a Priority Queue? Similar –store collections of elements –all elements of the same type –support an inserting operator and a removing operator –define a structured ordering on the removal of elements (I.e., not random) Different –a priority queue is not a queue! –very different orderings on elements –pqueues require comparisons on the elements –stacks and queues are highly efficient (pqueues slightly less so) –theoretical computational power (pqueues and queues beat stacks

Tree Calculations Find the longest undirected path in a tree Might be: – the longest path in one of the subtrees – the longest path that goes through t t

Tree Calculations Example A E B DF C G IH KJL M L N

Binary Trees Binary tree is –a root –left subtree (maybe empty) –right subtree (maybe empty) Properties –max # of leaves: –max # of nodes: –average depth for N nodes: Representation: A B DE C F HG JI Data right pointer left pointer

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

What We Can Do So Far Stack –Push –Pop Queue –Enqueue –Dequeue What’s wrong with Lists? Remember decreaseKey? List –Insert –Remove –Find Priority Queue –Insert –DeleteMin

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 Zasha –interesting ID, but not enough ooomph! Bone –More oomph, less high scoring Scrabble action Wolf –the perfect mix of oomph and Scrabble value insert find(Wolf) Darth - formidable Wolf - the perfect mix of oomph and Scrabble value

Search 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

A Modest Few Uses Arrays Sets Dictionaries Router tables Page tables Symbol tables C++ Structures

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

Naïve Implementations Linked list Unsorted array Sorted array insert delete find so close!

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

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

In Order Listing In order listing: 2  5  7  9  10  15  17  20  30

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:

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?

Insert runtime: void insert(Comparable key, Node * root) { Node *& target(find(key, root)); assert(target == NULL); target = new Node(key); }

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 (const Object & foo) –shares parameter –cannot affect actual value –use when the value is too intricate for pass-by-value

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.

Analysis of BuildTree Worst case: O(n 2 ) as we’ve seen Average case assuming all orderings equally likely:

Bonus: FindMin/FindMax Find minimum Find maximum

To Do Start Project II Answer the Quiz questions you missed Read chapter 4 in the book

Coming Up A bit more Binary Search Trees Self-balancing Binary Search Trees Huge Search Tree Data Structure Third homework assignment due (January 26th) Second project due (January 28th)