CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Similar documents
Today s Outline. CS 561, Lecture 8. Direct Addressing Problem. Hash Tables. Hash Tables Trees. Jared Saia University of New Mexico

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Binary Trees. Recursive definition. Is this a binary tree?

Binary search trees :

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Binary Search Trees, etc.

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Lecture 6: Analysis of Algorithms (CS )

Searching: Introduction

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

12 Binary Search Tree

Lecture: Analysis of Algorithms (CS )

Binary search trees (BST) Binary search trees (BST)

CS102 Binary Search Trees

x-fast and y-fast Tries

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

Data Structures. Dynamic Sets

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

x-fast and y-fast Tries

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Lecture 5: Scheduling and Binary Search Trees

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

SFU CMPT Lecture: Week 9

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

Search Trees: BSTs and B-Trees

Augmenting Data Structures

Friday Four Square! 4:15PM, Outside Gates

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

Dictionaries. Priority Queues

Binary Search Trees Treesort

Lecture 23: Binary Search Trees

CS 561, Lecture 2 : Randomization in Data Structures. Jared Saia University of New Mexico

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary Trees, Binary Search Trees

COMP Analysis of Algorithms & Data Structures

Algorithms, Spring 2014, CSE, OSU Lecture 4: Binary search trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary Search Trees. Nearest Neighbor Binary Search Trees Insertion Predecessor and Successor Deletion Algorithms on Trees.

Binary Search Trees. Analysis of Algorithms

Binary Search Trees. Binary Search Trees. Nearest Neighbor. Nearest Neighbor

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS 315 Data Structures mid-term 2

Data Structures and Algorithms

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

A set of nodes (or vertices) with a single starting point

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

Trees. (Trees) Data Structures and Programming Spring / 28

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Balanced Trees Part Two

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Programming II (CS300)

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Chapter 2: Basic Data Structures

Binary Search Trees. See Section 11.1 of the text.

Search Trees. Undirected graph Directed graph Tree Binary search tree

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

COMP Analysis of Algorithms & Data Structures

Garbage Collection: recycling unused memory

Algorithms. AVL Tree

COMP Analysis of Algorithms & Data Structures

Different binary search trees can represent the same set of values (Fig.2). Vladimir Shelomovskii, Unitech, Papua New Guinea. Binary search tree.

Priority Queues and Heaps


TREES. Trees - Introduction

H.O.#12 Fall 2015 Gary Chan. Binary Tree (N:12)

Balanced Search Trees

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Analysis of Algorithms

1 A node in a tree. Trees. Datatypes ISC

Self-Balancing Search Trees. Chapter 11

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Module 4: Dictionaries and Balanced Search Trees

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

CSCI Trees. Mark Redekopp David Kempe

Data Structures in Java

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Lecture 7. Binary Search Trees / AVL Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Data Structures and Algorithms for Engineers

INF2220: algorithms and data structures Series 1

Programming Languages and Techniques (CIS120)

If you took your exam home last time, I will still regrade it if you want.

Advanced Set Representation Methods

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

CS 350 : Data Structures Binary Search Trees

Dictionaries. 2/17/2006 Dictionaries 1

Multiway Search Trees. Multiway-Search Trees (cont d)

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Transcription:

HW Difficulty CS 361, Lecture 21 Jared Saia University of New Mexico The HW in this class is inherently difficult, this is a difficult class. You need to be able to solve problems as hard as the problems in the book to be competitive with students from other schools Some of these problems require deep thinking However there are things we can do to make things easier 3 Outline Things you can do Class Evaluation Binary Trees Start the hws early!! You have three resources you can use to do well on the hws: Other students - use email,class list, or phone Lab Sections - bring specific questions to lab section Office Hours - come to these 1 4 Evaluation Results Things I will do Vast majority of students said class pace is just right, so pace will stay the same as it is now Major other problem is hw is too difficult Answer any HW questions at the beginning of class Answer any HW questions emailed to the class mailing list Note: You need to start hw early in order to be able to ask me questions about problems you are having 2 5

HW Questions Red-Black Trees Red-Black trees (a kind of binary tree) also implement the Dictionary ADT, namely: Are there any questions on the current HW? Insert(x) - O(log n) time Lookup(x) - O(log n) time Delete(x) - O(log n) time 6 9 Binary Search Trees Why BST? Q: What is a search tree? A1: It s yet another data structure for implementing the dictionary ADT Q: Don t we already know enough of those? A: No Q: When would you use a Search Tree? A1: When need a hard guarantee on the worst case run times (e.g. mission critical code) A2: When want something more dynamic than a hash table (e.g. don t want to have to enlarge a hash table when the load factor gets too large) A3: Search trees can implement some other important operations... 7 10 Hash Tables Search Tree Operations Hash Tables implement the Dictionary ADT, namely: Insert(x) - O(1) expected time, Θ(n) worst case Lookup(x) - O(1) expected time, Θ(n) worst case Delete(x) - O(1) expected time, Θ(n) worst case Insert Lookup Delete Minimum/Maximum Predecessor/Successor 8 11

What is a BST? Inorder Walk It s a binary tree Each node holds a key and record field, and a pointer to left and right children Binary Search Tree Property is maintained BSTs are arranged in such a way that we can print out the elements in sorted order in Θ(n) time Inorder Tree-Walk does this 12 15 Binary Search Tree Property Inorder Tree-Walk Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key(y) key(x). If y is a node in the right subtree of x then key(x) key(y) Inorder-TW(x){ if (x is not nil){ Inorder-TW(left(x)); print key(x); Inorder-TW(right(x)); 13 16 Example BST Example Tree-Walk 14 17

Analysis In-Class Exercise Correctness? Run time? Q1: What is the loop invariant for Tree-Search? Q2: What is Initialization? Q3: Maintenance? Q4: Termination? 18 21 Search in BT Tree Min/Max Tree-Search(x,k){ if (x=nil) or (k = key(x)){ return x; if (k<key(x)){ return Tree-Search(left(x),k); else{ return Tree-Search(right(x),k); Tree Minimum(x): rooted at x Tree Maximum(x): rooted at x Return the leftmost child in the tree Return the rightmost child in the tree 19 22 Analysis Tree-Successor Let h be the height of the tree The run time is O(h) Correctness??? Tree-Successor(x){ if (right(x)!= null){ return Tree-Minimum(right(x)); y = parent(x); while (y!=null and x=right(y)){ x = y; y = parent(y); return y; 20 23

Successor Intuition Case 3 Case 1: If right subtree of x is non-empty, successor(x) is just the leftmost node in the right subtree Case 2: If the right subtree of x is empty and x has a successor, then successor(x) is the lowest ancestor of x whose left child is also an ancestor of x. Case 3: The node, x to be deleted has two children 1. Swap x with Successor(x) (Successor(x) has no more than 1 child (why?)) 2. Remove x, using the procedure for case 1 or case 2. 24 27 Insertion Analysis Insert(T,x) 1. Let r be the root of T. 2. Do Tree-Search(r,key(x)) and let p be the last node processed in that search 3. If p is nil (there is no tree), make x the root of a new tree 4. Else if key(x) p, make x the left child of p, else make x the right child of p All of these operations take O(h) time where h is the height of the tree If n is the number of nodes in the tree, in the worst case, h is O(n) However, if we can keep the tree balanced, we can ensure that h = O(log n) Next time, we ll see how Red-Black trees can maintain a balanced BST 25 28 Deletion Code is in book, basically there are three cases, two are easy and one is tricky Case 1: The node to delete has no children. Then we just delete the node Case 2: The node to delete has one child. Then we delete the node and splice together the two resulting trees 26