Programming Abstractions

Slides:



Advertisements
Similar presentations
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.
Advertisements

Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
data ordered along paths from root to leaf
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Data Structures and Design in Java © Rick Mercer
COMP 53 – Week Fourteen Trees.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Week 7 - Friday CS221.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
BST Trees
COSC160: Data Structures Binary Trees
Programming Abstractions
UNIT III TREES.
Week 6 - Wednesday CS221.
October 30th – Priority QUeues
Trees Lecture 12 CS2110 – Fall 2017.
Programming Abstractions
Trees Tree nomenclature Implementation strategies Traversals
Week 11 - Friday CS221.
March 31 – Priority Queues and the heap
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
COMP 103 Binary Search Trees.
Cse 373 April 26th – Exam Review.
November 1st – Exam Review
Heaps.
Red Black Trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
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.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Wednesday, April 18, 2018 Announcements… For Today…
CSE 373: Data Structures and Algorithms
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.
Find in a linked list? first last 7  4  3  8 NULL
AVL Trees: AVL Trees: Balanced binary search tree
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
Heaps and the Heapsort Heaps and priority queues
The DSW Algorithm The building block for tree transformations in this algorithm is the rotation There are two types of rotation, left and right, which.
CSE 373: Data Structures and Algorithms
Lesson 6. Types Equality and Identity. Collections.
Hassan Khosravi / Geoffrey Tien
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.
Trees CMSC 202, Version 5/02.
Tree Rotations and AVL Trees
CMSC 202 Trees.
Binary Search Trees.
CS6045: Advanced Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CSE 12 – Basic Data Structures
CS 367 – Introduction to Data Structures
Yan Shi CS/SE 2630 Lecture Notes
B-Trees.
CS203 Lecture 14.
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Topics: Wednesday: 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 Today: BST balance issues Traversals Pre-order In-order Post-order Breadth-first Applications of Traversals

BST Balance Strategies We need to balance the tree (keep O(logN) instead of O(N)), how can we do that if the tree structure is decided by key insert order?

Red-Black trees One of the most famous (and most tricky) strategies for keeping a BST balanced Not guaranteed to be perfectly balanced, but “close enough” to keep O(log n) guarantee on operations

Red-Black trees Video: http://www.youtube.com/watch?v=vDHFF4wjWYU Every simple path from a given node to any of its descendant leaves contains the same number of black nodes. (This is what guarantees “close” to balance) This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. http://commons.wikimedia.org/wiki/File:Red-black_tree_example.svg

Red-Black trees insert

A few BST balance strategies AVL tree Red-Black tree Treap (BST + heap in one tree! What could be cooler than that, amirite? ♥ ♥ )

Other fun types of BST Splay tree Rather than only worrying about balance, Splay Tree dynamically readjusts based on how often users search for an item. Most commonly-searched items move to the top, saving time Example: if Google did this, “Bieber” would be near the root, and “splay tree” would be further down by the leaves B-Tree Like BST, but a node can have many children, not just two More branching means an even “flatter” (smaller height) tree Used for huge databases

BST and Heap quick recap/cheat sheet

BST and Heap Facts (cheat sheet) BST (Map) Structure: any valid binary tree Order: leftchild.key < self.key < rightchild.key No duplicate keys Because it’s a Map, values go along for the ride w/keys Big-O: log(n) if balanced, but might not be balanced, then O(n) Operations: recursively repeat: start at root and go left if key < root, go right if key > root Heap (Priority Queue) Structure: must be “complete” Order: parent priority must be <= both children This is for min-heap, opposite is true for max-heap No rule about whether left child is > or < the right child Big-O: guaranteed log(n) enqueue and dequeue Operations: always add to end of array and then “bubble up”; for dequeue do “trickle down”

Tree Traversals! These are for any binary trees, but we often do them on BSTs

What does this print? (assume we call traverse on the root node to start) void traverse(Node *node) { if (node != NULL) { cout << node->key << " "; traverse(node->left); traverse(node->right); } A B C D E F A B D E C F D B E F C A D E B F C A Other/none/more A B C D E F

What does this print? (assume we call traverse on the root node to start) void traverse(Node *node) { if (node != NULL) { traverse(node->left); traverse(node->right); cout << node->key << " "; } A B C D E F A B D E C F D B E F C A D E B F C A Other/none/more A B C D E F

What does this print? (assume we call traverse on the root node to start) void traverse(Node *node) { if (node != NULL) { traverse(node->left); cout << node->key << " "; traverse(node->right); } 1 2 4 5 8 9 1 4 2 9 8 5 5 2 1 4 8 9 5 2 8 1 4 9 Other/none/more 5 2 8 1 4 9

How can we get code to print our ABCs in order as shown How can we get code to print our ABCs in order as shown? (note: not BST order) A void traverse(Node *node) { if (node != NULL) { ?? cout << node->key << " "; traverse(node->left); traverse(node->right); } You can’t do it by using this code and moving around the cout—we already tried moving the cout to all 3 possible places and it didn’t print in order You can but you use a queue instead of recursion “Breadth-first” search Again we see this key theme of BFS vs DFS! B C D E F

Applications of Tree Traversals Beautiful little things from an algorithms/theory standpoint, but they have a practical side too!

Traversals a very commonly-used tool in your CS toolkit void traverse(Node *node) { if (node != NULL) { traverse(node->left); // "do something” traverse(node->right); } Customize and move the “do something,” and that’s the basis for dozens of algorithms and applications

Map interface implemented with BST Remember how when you iterate over the Stanford library Map you get the keys in sorted order? (we used this for the word occurrence counting code example in class) void printMap(const Map<string, int>& theMap) { for (string s : theMap) { cout << s << endl; // printed in sorted order } Now you know why it can do that in O(N) time! “In-order” traversal

Applications of the traversals You have a tree that represents evaluation of an arithmetic expression. Which traversal would form the foundation of your evaluation algorithm? Pre-order In-order Post-order Breadth-first * + / 3 4 8 2

Applications of the traversals You are writing the destructor for a BST class. Given a pointer to the root, it needs to free each node. Which traversal would form the foundation of your destructor algorithm? Pre-order In-order Post-order Breadth-first BST size: root: 5 2 8 1 4 9