S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES 22-23 Binary Search Trees Algorithms and Data Structures.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

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.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
BST Data Structure A BST node contains: A BST contains
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Binary Search Trees Chapter 7 Objectives
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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 ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Priority Queues and Binary Heaps Algorithms.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
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 Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Binary Search Trees … From
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
Binary Search Trees Lecture 6 Prof. Dr. Aydın Öztürk.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees What is a binary search tree?
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Data Structures Review Session 2
Tree.
Analysis of Algorithms
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Binary SearchTrees [CLRS] – Chap 12.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures CMPSC 465

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. Heaps: Review Heap-leap-jeep-creep(A): 1. A.heap-size = 0 2. For i=1 to len(A) tmp = A[i] Heap-Insert(A,tmp) 3. For i=len(A) down to 1 tmp = Heap-Extract-Max(A) A[i]=tmp What does this do? What is the running time of this operation? 2

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 3 Trees Rooted Tree: collection of nodes and edges  Edges go down from root (from parents to children)  No two paths to the same node  Sometimes, children have “ names ” (e.g. left, right, middle, etc)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 4 Binary Search Trees Binary tree: every node has 0, 1 or 2 children BST property:  If y is in left subtree of x, then key[y] <= key[x]  same for right

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 5 Binary Search Trees Binary tree: every node has 0, 1 or 2 children BST property:  If y is in left subtree of x, then key[y] <= key[x]  same for right

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 6 Implementation Keep pointers to parent and both children Each NODE has  left: pointer to NODE  right: pointer to NODE  p: pointer to NODE  key: real number (or other type that supports comparisons)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 7 Drawing BST’s To help you visualize relationships:  Drop vertical lines to keep nodes in right order For example: picture shows the only legal location to insert a node with key

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 8 Height of a BST can be as little as log(n)  full balanced tree... as much as n -1  unbalanced tree n n

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 9 Searching a BST Running time: Θ(height)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 10 Insertion Find location of insertion, keep track of parent during search Running time: Θ(height)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 11 Tree Min and Max Running time: Θ(height)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 12 Tree Successor Running time: Θ(height)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. Traversals (not just for BSTs) Traversal: an algorithm that visits every node in the tree to perform some operation, e.g.,  Printing the keys  Updating some part of the structure 3 basic traversals for trees  Inorder  Preorder  Postorder 13

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 14 Inorder traversal Running time: Θ(n)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 15 Postorder traversal Write pseudocode that computes  height of a BST  number of nodes in a BST  average depth ... Example: height  Find-height(T) if T==NIL return -1 else –h1 := Find-height(T.left) –h2 := Find-height(T.left) –return max(h1, h2) + 1

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 16 Preorder Traversal Recall that insertion order affects the shape of a BST  insertion in sorted order: height n-1  random order: height O(log n) with high probability (we will prove that later in the course) Write pseudocode that prints the elements of a binary search tree in a plausible insertion order  that is, an insertion order that would produce this particular shape (print nodes in a preorder traversal)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 17 Exercise on insertion order Exercise: Write pseudocode that takes a sorted list and produces a “ good ” insertion order (that would produce a balanced tree) (Hint: divide and conquer: always insert the median of a subarray before inserting the other elements)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 18 Deletion Cases analyzed in book Running time: Θ(height)

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine Binary-search-tree sort T ← ∅  ⊳ Create an empty BST for i = 1 to n do T REE -I NSERT ( T, A [ i ]) Perform an inorder tree walk of T. Example: A = [ ] Tree-walk time = O ( n ), but how long does it take to build the BST?

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 20 Analysis of BST sort BST sort performs the same comparisons as quicksort, but in a different order! The expected time to build the tree is asymptot- ically the same as the running time of quicksort.

S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 21 Node depth The depth of a node = the number of comparisons made during T REE -I NSERT. Assuming all input permutations are equally likely, we have Average node depth. (quicksort analysis)