1 Searching the dictionary ADT binary search binary search trees.

Slides:



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

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
6/14/2015 6:48 AM(2,4) Trees /14/2015 6:48 AM(2,4) Trees2 Outline and Reading Multi-way search tree (§3.3.1) Definition Search (2,4)
© 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.
1/51 Dictionaries, Tables Hashing TCSS 342 2/51 The Dictionary ADT a dictionary (table) is an abstract model of a database like a priority queue, a dictionary.
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
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
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. 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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
© 2004 Goodrich, Tamassia Trees
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Dictionaries. Reading Weiss Chap. 5, Sec
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)
© 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 < > =
Search Trees.
Binary Search Trees < > =
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
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 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
Heaps and Priority Queues
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Priority Queues and Heaps
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Heaps 11/27/ :05 PM Heaps Heaps.
Dictionaries < > = /3/2018 8:58 AM Dictionaries
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Heaps and Priority Queues
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
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Heaps and Priority Queues
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Binary Search Trees < > = Dictionaries
Dictionaries 二○一九年九月二十四日 ADT for Map:
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

1 Searching the dictionary ADT binary search binary search trees

2 The Dictionary ADT A dictionary is an abstract model of a database –Like a priority queue, a dictionary stores key-element pairs –The main operation supported by a dictionary is searching by key simple container methods:size() isEmpty() elements() query methods:findElement(k) findAllElements(k) update methods:insertItem(k, e) removeElement(k) removeAllElements(k) special element NO_SUCH_KEY, returned by an unsuccessful search

3 Implementing a Dictionary with a Sequence searching and removing takes O(n) time inserting takes O(1) time applications to log files (frequent insertions, rare searches and removals)

4 Implementing a Dictionary with a Sequence searching takes O(log n) time (binary search) inserting and removing takes O(n) time application to look-up tables (frequent searches, rare insertions and removals)

5 Binary Search narrow down the search range in stages “high-low” game findElement(22)

6 Pseudocode for Binary Search Algorithm BinarySearch(S, k, low, high) if low > high then return NO_SUCH_KEY elsemid  (low+high) / 2 if k = key(mid) then return key(mid) else if k < key(mid) then return BinarySearch(S, k, low, mid-1) else return BinarySearch(S, k, mid+1, high)

7 Running Time of Binary Search The range of candidate items to be searched is halved after each comparison In the array-based implementation, access by rank takes O(1) time, thus binary search runs in O(log n) time

8 Binary Search Trees A binary search tree is a binary tree T such that –each internal node stores an item (k, e) of a dictionary. –keys stored at nodes in the left subtree of v are less than or equal to k. –keys stored at nodes in the right subtree of v are greater than or equal to k. –external nodes do not hold elements but serve as place holders.

9 Search A binary search tree T is a decision tree, where the question asked at an internal node v is whether the search key k is less than, equal to, or greater than the key stored at v. Pseudocode: Algorithm TreeSearch(k, v):Input: A search key k and a node v of a binary search tree T. Ouput: A node w of the subtree T(v) of T rooted at v, such that either w is an internal node storingkey k or w is the external node encountered in the inorder traversal of T(v) after all the internal nodes with keys smaller than k and before all the internal nodes with keys greater than k. if v is an external node then return v if k = key(v) then return v else if k < key(v) then return TreeSearch(k, T.leftChild(v)) else { k > key(v) } return TreeSearch(k, T.rightChild(v))

10 Search Example I A successful search traverses a path starting at the root and ending at an internal node How about findAllelements(k)? Successful findElement(76) 76>44 76<88 76>65 76<82

11 Search Example II An unsuccessful search traverses a path starting at the root and ending at an external node Unsuccessful findElement(25) 25<44 25>17 25<32 25<28 leaf node

12 Insertion To perform insertItem(k, e), let w be the node returned by TreeSearch(k, T.root()) If w is external, we know that k is not stored in T. We call expandExternal(w) on T and store (k, e) in w

13 Insertion II If w is internal, we know another item with key k is stored at w. We call the algorithm recursively starting at T.rightChild(w) or T.leftChild(w)

14 Removal I We locate the node w where the key is stored with algorithm TreeSearch If w has an external child z, we remove w and z with removeAboveExternal(z)

15 Removal II If w has an no external children: –find the internal node y following w in inorder –move the item at y into w –perform removeAboveExternal(x), where x is the left child of y (guaranteed to be external)

16 Time Complexity A search, insertion, or removal, visits the nodes along a root-to leaf path, plus possibly the siblings of such nodes Time O(1) is spent at each node The running time of each operation is O(h), where h is the height of the tree The height of binary serch tree is in n in the worst case, where a binary search tree looks like a sorted sequence To achieve good running time, we need to keep the tree balanced, i.e., with O(logn) height. Various balancing schemes will be explored in the next lectures