© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees 1 6 9 2 4 1 8   

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Binary Search Trees
Advertisements

1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 2004 Goodrich, Tamassia Maps1. © 2004 Goodrich, Tamassia Maps2 A map models a searchable collection of key-value entries The main operations of a map.
Dictionaries1 © 2010 Goodrich, Tamassia m l h m l h m l.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 2004 Goodrich, Tamassia Dictionaries   
© 2004 Goodrich, Tamassia Binary Search Trees   
Goodrich, Tamassia Search Trees1 Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Binary Search Trees1 Part-F1 Binary Search Trees   
Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
© 2004 Goodrich, Tamassia Trees1 Lecture 04 Trees Topics Trees Binary Trees Binary Search trees.
© 2004 Goodrich, Tamassia (2,4) Trees
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSC401 – Analysis of Algorithms Lecture Notes 6 Dictionaries and Search Trees Objectives: Introduce dictionaries and its diverse implementations Introduce.
CS 221 Analysis of Algorithms Ordered Dictionaries and Search Trees.
Search Trees Last Update: Nov 5, 2014 EECS2011: Search Trees1 “Grey Tree”, Piet Mondrian, 1912.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
1 Searching the dictionary ADT binary search binary search trees.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Advanced Data Structures By: Nikhil Bhargava. Outline Introduction to ADT. Introduction to ADT. Binary Search Tree (BST) Binary Search Tree (BST) –Definition.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014
Algorithms Design Fall 2016 Week 6 Hash Collusion Algorithms and Binary Search Trees.
Part-D1 Binary Search Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
COMP9024: Data Structures and Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Search Trees < > =
Search Trees.
Binary Search Trees < > =
Binary Search Trees Rem Collier Room A1.02
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Trees < > = Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Chapter 2: Basic Data Structures
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Dictionaries < > = /3/2018 8:58 AM Dictionaries
© 2013 Goodrich, Tamassia, Goldwasser
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Binary Search Trees < > =
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Binary Search Trees < > =
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
Presentation transcript:

© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   

© 2004 Goodrich, Tamassia Ordered Dictionaries Binary Search Trees 2  Keys are assumed to come from a total order.  New operations:  first(): first entry in the dictionary ordering  last(): last entry in the dictionary ordering  successors(k): iterator of entries with keys greater than or equal to k; increasing order  predecessors(k): iterator of entries with keys less than or equal to k; decreasing order

© 2004 Goodrich, Tamassia Binary Search (§ 8.3.3) Binary Search Trees 3  Binary search can perform operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key  similar to the high-low game  at each step, the number of candidate items is halved  terminates after O(log n) steps  Example: find(7) m l h m l h m l h l  m  h

© 2004 Goodrich, Tamassia Search Tables  A search table is a dictionary implemented by means of a sorted sequence  We store the items of the dictionary in an array-based sequence, sorted by key  We use an external comparator for the keys  Performance:  find takes O(log n) time, using binary search  insert takes O(n) time since in the worst case we have to shift n  2 items to make room for the new item  remove take O(n) time since in the worst case we have to shift n  2 items to compact the items after the removal  The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations) 4 Binary Search Trees

© 2004 Goodrich, Tamassia Binary Search Trees (§ 9.1)  A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property:  Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u)  key(v)  key(w)  External nodes do not store items  An inorder traversal of a binary search trees visits the keys in increasing order 5 Binary Search Trees

© 2004 Goodrich, Tamassia Search (§ 9.1.1) Binary Search Trees 6  To search for a key k, we trace a downward path starting at the root  The next node visited depends on the outcome of the comparison of k with the key of the current node  If we reach a leaf, the key is not found and we return nukk  Example: find(4):  Call TreeSearch(4,root) Algorithm TreeSearch(k, v) if T.isExternal (v) return v if k  key(v) return TreeSearch(k, T.left(v)) else if k  key(v) return v else { k  key(v) } return TreeSearch(k, T.right(v))   

© 2004 Goodrich, Tamassia Insertion Binary Search Trees 7  To perform operation inser(k, o), we search for key k (using TreeSearch)  Assume k is not already in the tree, and let let w be the leaf reached by the search  We insert k at node w and expand w into an internal node  Example: insert    w w

© 2004 Goodrich, Tamassia Deletion Binary Search Trees 8  To perform operation remove( k ), we search for key k  Assume key k is in the tree, and let let v be the node storing k  If node v has a leaf child w, we remove v and w from the tree with operation removeExternal( w ), which removes w and its parent  Example: remove v w  

© 2004 Goodrich, Tamassia Deletion (cont.) Binary Search Trees 9  We consider the case where the key k to be removed is stored at a node v whose children are both internal  we find the internal node w that follows v in an inorder traversal  we copy key(w) into node v  we remove node w and its left child z (which must be a leaf) by means of operation removeExternal( z )  Example: remove v w z v 2