Chapter 10.1 Binary Search Trees

Slides:



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

Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
© 2004 Goodrich, Tamassia Binary Search Trees   
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
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 (2,4) Trees
Lecture12: Tree II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2004 Goodrich, Tamassia Red-Black Trees v z.
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.
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.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CHAPTER 10 SEARCH TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND.
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.
© 2004 Goodrich, Tamassia BINARY SEARCH TREES Binary Search Trees   
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 < > =
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Search Trees.
Binary Search Trees < > =
Binary Search Trees Rem Collier Room A1.02
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Binary Search Tree (BST)
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.
Binary Search Trees < > = Binary Search Trees
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Copyright © Aiman Hanna All rights reserved
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Dictionaries < > = /9/2018 3:06 AM Dictionaries
TREES– Interesting problems
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Dictionaries < > = /17/2019 4:20 PM Dictionaries
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
CH 9 : Maps And Dictionary
(2,4) Trees (2,4) Trees (2,4) Trees.
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
1 Lecture 13 CS2013.
Binary Search Trees < > = Dictionaries
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Dictionaries 二○一九年九月二十四日 ADT for Map:
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

Chapter 10.1 Binary Search Trees 6 9 2 4 1 8 < > = Chapter 10.1 Binary Search Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) and slides from Nancy M. Amato And Jory Denny 1 1

Binary Search Trees A binary search tree is a binary tree storing entries (𝑘, 𝑒) (i.e., key-value pairs) at its internal nodes and satisfying the following property: Let 𝑢, 𝑣, and 𝑤 be three nodes such that 𝑢 is in the left subtree of 𝑣 and 𝑤 is in the right subtree of 𝑣. Then 𝑘𝑒𝑦 𝑢 ≤𝑘𝑒𝑦 𝑣 ≤𝑘𝑒𝑦 𝑤 External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8

Search Dictionaries 9/11/2018 11:31 PM < > = 6 9 2 4 1 8 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 Example: find(4) Algorithms for floorEntry and ceilingEntry are similar Algorithm Search(𝑘, 𝑣) if 𝑣.isExternal return 𝑣 if 𝑘<𝑣.key return Search 𝑘, 𝑣.left else if 𝑘=𝑣.key return v else //𝑘>𝑣.key() return Search 𝑘, 𝑣.right

Exercise Show the search paths for the following keys: 8,3,2 6 9 2 4 1

Insertion 6 9 2 4 1 8 < > w To perform operation put(𝑘, 𝑣), we search for key 𝑘 (using Search(𝑘)) Assume 𝑘 is not already in the tree, and let 𝑤 be the leaf reached by the search We insert 𝑘 at node 𝑤 and expand 𝑤 into an internal node Example: insert 5 6 9 2 4 1 8 5 w

Exercise Binary Search Trees Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 55, 68, 12, 9, 30, 59, 73

Deletion < To perform operation erase 𝑘 , we search for key 𝑘 > 6 9 2 4 1 8 5 v w < > To perform operation erase 𝑘 , we search for key 𝑘 Assume key 𝑘 is in the tree, and let 𝑣 be the node storing 𝑘 If node 𝑣 has a leaf child 𝑤, we remove 𝑣 and 𝑤 from the tree with operation removeAboveExternal 𝑤 , which removes 𝑤 and its parent Example: remove 4 6 9 2 5 1 8

Deletion (cont.) 3 1 8 6 9 5 v w z 2 We consider the case where the key 𝑘 to be removed is stored at a node 𝑣 whose children are both internal we find the internal node 𝑤 that follows 𝑣 in an inorder traversal we copy 𝑤.key into node 𝑣 we remove node 𝑤 and its left child 𝑧 (which must be a leaf) by means of operation removeAboveExternal(𝑧) Example: remove 3 5 1 8 6 9 v 2

Exercise Binary Search Trees Insert into an initially empty binary search tree items with the following keys (in this order). Draw the resulting binary search tree 30, 40, 24, 58, 48, 26, 11, 13 Now, remove the item with key 30. Draw the resulting tree Now remove the item with key 48. Draw the resulting tree.

Performance Consider an ordered map with 𝑛 items implemented by means of a binary search tree of height ℎ Space used is 𝑂 𝑛 Methods find 𝑘 , floorEntry(𝑘), ceilingEntry 𝑘 , put 𝑘, 𝑣 , and erase 𝑘 take 𝑂 ℎ time The height ℎ is 𝑂(𝑛) in the worst case and 𝑂 log 𝑛 in the best case