Programming Abstractions

Slides:



Advertisements
Similar presentations
BST Data Structure A BST node contains: A BST contains
Advertisements

Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Big Java Chapter 16.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
CSCE 210 Data Structures and Algorithms
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
CS 106X – Programming Abstractions in C++
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Programming Abstractions
CS 201 Data Structures and Algorithms
Priority Queues (Heaps)
Week 7 - Friday CS221.
Programming Abstractions
COMP 53 – Week Eleven Hashtables.
Efficiency of in Binary Trees
Programming Abstractions
Programming Abstractions
Week 15 – Monday CS221.
October 30th – Priority QUeues
Programming Abstractions
Week 11 - Friday CS221.
Source: Muangsin / Weiss
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Cse 373 April 26th – Exam Review.
Priority Queues (Heaps)
Tree data structure.
Programming Abstractions
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.
structures and their relationships." - Linus Torvalds
Monday, April 16, 2018 Announcements… For Today…
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Wednesday, April 18, 2018 Announcements… For Today…
Tree data structure.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Search Sorted Array: Binary Search Linked List: Linear Search
Programming Abstractions
B-Tree Insertions, Intro to Heaps
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
ITEC 2620M Introduction to Data Structures
Lesson 6. Types Equality and Identity. Collections.
Hassan Khosravi / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Programming Abstractions
CSE 373: Data Structures and Algorithms
CE 221 Data Structures and Algorithms
Priority Queues (Chapter 6.6):
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Programming Abstractions
Searching CLRS, Sections 9.1 – 9.3.
CO4301 – Advanced Games Development Week 5 Red-Black Trees
Priority Queues (Heaps)
Mark Redekopp David Kempe
Priority Queues (Chapter 6):
Chapter 9 The Priority Queue ADT
structures and their relationships." - Linus Torvalds
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: Monday: Priority Queue implemented as a binary heap Enqueue (“bubble up”) Dequeue (“trickle down”) Today: Map implemented as a Binary Search Tree (BST) Starting with a dream: binary search in a linked list? How our dream provided the inspiration for the BST BST insert Big-O analysis of BST Friday: BST balance issues

Binary Search Trees Implementing the Map interface with Binary Search Trees

Implementing Map interface with a Binary Search Tree (BST) Binary Search Trees is one option for implementing Map C++’s Standard Template Library (STL) uses a Red-Black tree (a type of BST) for their map Stanford library also uses a BST Another Map implementation is a hash table We will talk about this later! This is what Stanford’s HashMap uses

Binary Search in a Linked List?   Exploring a good idea, finding way to make it work

Imagine storing sorted data in an array 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 How long does it take us to find? Binary search! O(logn): awesome! But slow to insert Move everyone over to make space O(n): not terrible, but pretty bad compared to log(n) or O(1) In contrast, linked list stores its nodes scattered all over memory, so it doesn’t have to move things over

Q. Can we do binary search on a linked list to implement a quick insert? No. The nodes are spread all over memory, and we must follow “next” pointers one at a time to navigate (the treasure hunt). Therefore cannot jump right to the middle. Therefore cannot do binary search. Find is O(N): not terrible, but pretty bad compared to O(logn) or O(1) Binary Search Tree can be thought of roughly like a linked list that has pointers to the middle, again and again (recursively), to form a tree structure

An Idealized Binary Search Tree

TreeMap   This is basically the same as Stanford Map

tree-map.h template <typename Key, typename Value> class TreeMap { public: TreeMap(); ~TreeMap(); bool isEmpty() const; int size() const { return count; } bool containsKey(const Key& key) const; void put(const Key& key, const Value& value); Value get(const Key& key) const; Value& operator[](const Key& key); ...

tree-map.h TreeMap root: size: node key: value: left: right: node key: // class TreeMap continued... private: struct node { Key key; Value value; node *left, *right; }; int size; node *root; key: value: left: right: node key: value: left: right: node

BST put() Example: insert 2 Pretty simple! If key > node’s key Go right! If key < node’s key Go left! If there is nothing currently in the direction you are going, that’s where you end up Example: put(23)

BST put() Insert: 22, 9, 34, 18, 3 How many of these result in the same tree structure as above? 22, 34, 9, 18, 3 22, 18, 9, 3, 34 22, 9, 3, 18, 34 None of these 1 of these 2 of these All of these

Question about our put() algorithm: Pretty simple! If key > node’s key Go right! If key < node’s key Go left! Q. What do we do if the key is equal to the node’s key? Stanford Map example: Map<int, string> mymap; mymap.put(5, "five"); mymap.put(5, "cinco"); // what should happen? cout << mymap.get(5) << endl; // what should print?

Performance analysis: What is the WORST CASE cost for doing containsKey() in BST? O(log n) O(n) O(n log n) O(n2)

What is the worst case cost for doing containsKey() in BST if the BST is balanced? O(logN)—awesome! BSTs are great when balanced BSTs are bad when unbalanced …and Balance depends on order of insert of elements… …but user controls this, not “us” (author of the Map class) …no way for “us” (author of Map class) to ensure our Map doesn’t perform terribly!  

Ok, so, long-chain BSTs are bad, should we worry about it Ok, so, long-chain BSTs are bad, should we worry about it? [math puzzle time] One way to create a bad BST is to insert the elements in decreasing order: 34, 22, 9, 3 That’s not the only way… How many distinctly structured BSTs are there that exhibit the worst case height (height equals number of nodes) for a tree with the 4 nodes listed above? 2-3 4-5 6-7 8-9 More than 9 Bonus question: general formula for any BST of size n? Extra bonus question (CS109): what is this as a fraction of all trees (i.e., probability of worst-case tree).

Midterm Questions? Are you nervous? What can I do to help?