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.

Slides:



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

EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSIT 402 Data Structures II
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
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.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
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.
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.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
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.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Trees Chapter 15.
Programming Abstractions
BST Trees
Week 6 - Wednesday CS221.
Section 8.1 Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Lecture 12 CS203 1.
CSE 12 – Basic Data Structures
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics 1. Wrap up traversals from Tuesday  Level-order traversal  Also known as Breadth-First Search (BFS) 2. Binary Search Trees (BSTs)  Note: These are different from plain vanilla Binary Trees! They are a special kind of Binary Tree! 2

Level-order traversal AKA Breadth-first search

Level-order traversal  Also commonly called Breadth-First Search or BFS  As opposed to something like pre-order or post-order, which are Depth-First Search (DFS) algorithms

Level-order challenges  How do we know where to go next?  1, 2, 3—but there is no edge from 2 to 3!  While we’re still at 1, we must “remember” to come back to 3 after we’re done with 2  At 2, remember to come back to 4 and 5 after 3  At 8, remember to come back to 16 and 17 after 15  Etc…

How to “remember”  When we visit a node, add references (pointers) to its children to a queue  To know which node to visit next, remove next element from the queue

Tracing the queue in BFS Which shows the state of the queue at the end of “visiting” node #10? (“head” of the queue, i.e. the next element that will be removed, is the leftmost) A. 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 B. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 C. 20, 21 D. 1, 2, 5 E. Other/none/more

BFS code BFS(node) { Queue q = new Queue (); q.add(node); while (!q.isEmpty()) { current = q.remove(); System.out.println(current.getData()); } A. BFS(current.getLeftChild()); B. BFS(current.getRightChild()); C. q.add(current.getLeftChild()); D. q.add(current.getRighChild()); E. Other/none/more What goes here?

Reading quiz!

1. A total order must apply to data elements in a Binary Search Tree. A. TRUE B. FALSE Reading quiz!

2. An inorder traversal of a binary search tree visits the tree’s elements in sorted order. A. TRUE B. FALSE Reading quiz!

Binary Search Trees

Binary Search Tree (BST) RULE:  For every node:  Everything in its left subtree is less than it  Everything in its right subtree is greater than it

Binary Search Tree (BST) FACT:  The “in-order” traversal method, when applied to a BST, gives sorted order FACT:  Easy to find things: just recursively check if you should go left or right based on > or <

BST add() Create two BSTs by inserting the elements, one by one, in the order given below for the first letter of your last name :  A-F: {18, 9, 34, 3, 22}  G-L: {3, 18, 22, 9, 34}  M-R: {22, 9, 34, 3, 18}  S-Z: {34, 3, 9, 18, 22}  EVERYBODY do this for a 2 nd BST: {3, 9, 18, 22, 34}

What is the BEST CASE cost for doing find() 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 find() 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 find() in BST if the BST is complete ? A. O(1) B. O(log n) C. O(n) D. O(n log n) E. O(n 2 )

BALANCE!!!  The #1 issue to remember with BSTs is that they are great when balanced (O(log n) operations), and horrible when unbalanced (O(n) operations)  Balance depends on order of insert of elements  Over the years, people have devised many ways of making sure BSTs stay balanced no matter what order the elements are inserted

BST Balance Strategies

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  In addition to the requirements imposed on a binary search trees, red–black trees must meet these:  A node is either red or black.  The root is black.  All leaves (null children) are black.  Both children of every red node are black.  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)

Red-Black trees  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)

Insert procedure must maintain the invariants (this gets tricky)  Video:

Other BST balance strategies  Red-Black tree  AVL tree  Treap (BST + heap in one tree!) Other fun types of BST:  Splay tree  Rather than only worrying about balance, Splay Tree dynamically readjusts based on how often a particular item is searched for  Commonly-searched items move to the top, saving time  B-Tree  Like BST, but a node can have many children, not just 2

More practice: find()

 Practice finding each of these: 6, 25, 11

More practice: find()  How do we know that 12 is NOT in the tree?