Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)

Slides:



Advertisements
Similar presentations
Exam Review 3 Chapters 10 – 13, 15 CSC212 Section FG CS Dept, CCNY.
Advertisements

1 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
BST Data Structure A BST node contains: A BST contains
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.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Heaps and Priority Queues CS 3358 – Data Structures.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
A Binary Search Tree Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
Chapter 19 C++ Data Structures By C. Shing ITEC Dept Radford University.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
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.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
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.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
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)
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Programming Abstractions
Programming Abstractions
BST Trees
Programming Abstractions
Efficiency of in Binary Trees
Programming Abstractions
Programming Abstractions
Cse 373 April 26th – Exam Review.
Binary Search Trees.
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.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Tree data structure.
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
Chapter 9 Priority Queues and Sets
AVL-Trees (Part 2).
Presentation transcript:

Programming Abstractions Cynthia Lee CS106X

Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream provided the inspiration for the BST Note: we do NOT actually construct BSTs using this method › BST insert › Big-O analysis of BST › (if we have time) BST balance issues 2

Binary heap insert and delete

Binary heap insert + “bubble up” Size=8, Capacity= … ??…? Size=9, Capacity= … ?…?

[Binary heap insert reference page]

Binary heap delete + “trickle down” Size=8, Capacity= … ??…? Size=9, Capacity= … ?…?

[Binary heap delete + “trickle-down” reference page]

Binary Search Trees Implementing the Map interface with Binary Search Trees

Implementing Map interface with a Binary Search Tree (BST)  Often we think of a hash table as the go-to implementation of the Map interface › Will talk about this on Thursday!  Binary Search Trees are another option › C++’s Standard Template Library (STL) uses a Red-Black tree (a type of BST) for their map › Stanford library also uses a BST Use hash-map.h to get a hashing version

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

Imagine storing sorted data in an array  How long does it take us to find? › Binary search! › O(logn): awesome!  But slow to insert › Scoot 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 the heap, so it doesn’t have to scoot things over

Q. Can we do binary search on a linked list to implement a quick insert? A.No.  The nodes are spread all over the heap, and we must follow “next” pointers one at a time to navigate.  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 as a linked list that has pointers to the middle, again and again (recursively), to form a tree structure

An Idealized Binary Search Tree

TreeMap An implementation of the Map interface

tree-map.h template class TreeMap { public: TreeMap(); ~TreeMap(); bool isEmpty() const { return size() == 0; } 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... private: struct node { Key key; Value value; node *left, *right; }; int count; node *root; };

BST put() Pretty simple!  If key > node’s key › Go right!  If key < node’s key › Go left!  If equal, do update of value— no duplicate keys!  If there is nothing currently in the direction you are going, that’s where you end up Example: insert 2

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 A. None of these B. 1 of these C. 2 of these D. All of these

What is the WORST CASE cost for doing containsKey() in BST? A.O(1) B.O(log n) C.O(n) D.O(n log n) E.O(n 2 )

What is the worst case cost for doing containsKey() in BST if the BST is balanced? O(logN)—awesome! BSTs is that they are great when balanced BST is bad when unbalanced Balance depends on order of insert of elements