© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

The ADT Hash Table What is a table?
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
Hashing as a Dictionary Implementation
CS202 - Fundamental Structures of Computer Science II
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
Hashing Techniques.
Dictionaries and Their Implementations
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Chapter 13 A Advanced Implementations of Tables. © 2004 Pearson Addison-Wesley. All rights reserved 13 A-2 Balanced Search Trees The efficiency of the.
Hashing Hashing is another method for sorting and searching data.
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
Hashing as a Dictionary Implementation Chapter 19.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Hashing Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Chapter 15 A External Methods. © 2004 Pearson Addison-Wesley. All rights reserved 15 A-2 A Look At External Storage External storage –Exists beyond the.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
© 2006 Pearson Addison-Wesley. All rights reserved15 A-1 Chapter 15 External Methods.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Data Structures Using C++ 2E
Data Abstraction & Problem Solving with C++
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Data Structures Using C++ 2E
Advanced Associative Structures
Hash Table.
Hash Tables.
Dictionaries and Their Implementations
Hash Tables and Associative Containers
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Advanced Implementation of Tables
Ch Hash Tables Array or linked list Binary search trees
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007

© 2006 Pearson Addison-Wesley. All rights reserved13 B-2 AVL Trees An AVL tree –A balanced binary search tree –Can be searched almost as efficiently as a minimum- height binary search tree –Maintains a height close to the minimum –Requires far less work than would be necessary to keep the height exactly equal to the minimum Basic strategy of the AVL method –After each insertion or deletion Check whether the tree is still balanced If the tree is unbalanced, restore the balance

© 2006 Pearson Addison-Wesley. All rights reserved13 B-3 AVL Trees Rotations –Restore the balance of a tree –Two types Single rotation Double rotation Figure a) An unbalanced binary search tree; b) a balanced tree after a single left rotation

© 2006 Pearson Addison-Wesley. All rights reserved13 B-4 AVL Trees Figure a) Before; b) during; and c) after a double rotation

© 2006 Pearson Addison-Wesley. All rights reserved13 B-5 AVL Trees Advantage –Height of an AVL tree with n nodes is always very close to the theoretical minimum Disadvantage –An AVL tree implementation of a table is more difficult than other implementations

© 2006 Pearson Addison-Wesley. All rights reserved13 B-6 Hashing –Enables access to table items in time that is relatively constant and independent of the items Hash function –Maps the search key of a table item into a location that will contain the item Hash table –An array that contains the table items, as assigned by a hash function

© 2006 Pearson Addison-Wesley. All rights reserved13 B-7 Hashing A perfect hash function –Maps each search key into a unique location of the hash table –Possible if all the search keys are known Collisions –Occur when the hash function maps more than one item into the same array location Collision-resolution schemes –Assign locations in the hash table to items with different search keys when the items are involved in a collision Requirements for a hash function –Be easy and fast to compute –Place items evenly throughout the hash table

© 2006 Pearson Addison-Wesley. All rights reserved13 B-8 Hash Functions It is sufficient for hash functions to operate on integers Simple hash functions that operate on positive integers –Selecting digits –Folding –Module arithmetic Converting a character string to an integer –If the search key is a character string, it can be converted into an integer before the hash function is applied

© 2006 Pearson Addison-Wesley. All rights reserved13 B-9 Resolving Collisions Two approaches to collision resolution –Approach 1: Open addressing A category of collision resolution schemes that probe for an empty, or open, location in the hash table –The sequence of locations that are examined is the probe sequence Linear probing –Searches the hash table sequentially, starting from the original location specified by the hash function –Possible problem »Primary clustering

© 2006 Pearson Addison-Wesley. All rights reserved13 B-10 Resolving Collisions Approach 1: Open addressing (Continued) –Quadratic probing Searches the hash table beginning with the original location that the hash function specifies and continues at increments of 1 2, 2 2, 3 2, and so on Possible problem –Secondary clustering –Double hashing Uses two hash functions Searches the hash table starting from the location that one hash function determines and considers every n th location, where n is determined from a second hash function Increasing the size of the hash table –The hash function must be applied to every item in the old hash table before the item is placed into the new hash table

© 2006 Pearson Addison-Wesley. All rights reserved13 B-11 Resolving Collisions Approach 2: Restructuring the hash table –Changes the structure of the hash table so that it can accommodate more than one item in the same location –Buckets Each location in the hash table is itself an array called a bucket –Separate chaining Each hash table location is a linked list

© 2006 Pearson Addison-Wesley. All rights reserved13 B-12 The Efficiency of Hashing An analysis of the average-case efficiency of hashing involves the load factor –Load factor  Ratio of the current number of items in the table to the maximum size of the array table Measures how full a hash table is Should not exceed 2/3 –Hashing efficiency for a particular search also depends on whether the search is successful Unsuccessful searches generally require more time than successful searches

© 2006 Pearson Addison-Wesley. All rights reserved13 B-13 The Efficiency of Hashing Linear probing –Successful search: ½[1 + 1(1-  )] –Unsuccessful search: ½[1 + 1(1-  ) 2 ] Quadratic probing and double hashing –Successful search: -log e (1-  )/  –Unsuccessful search: 1/(1-  ) Separate chaining –Insertion is O(1) –Retrievals and deletions Successful search: 1 + (  /2) Unsuccessful search: 

© 2006 Pearson Addison-Wesley. All rights reserved13 B-14 The Efficiency of Hashing Figure The relative efficiency of four collision-resolution methods

© 2006 Pearson Addison-Wesley. All rights reserved13 B-15 What Constitutes a Good Hash Function? A good hash function should –Be easy and fast to compute –Scatter the data evenly throughout the hash table Issues to consider with regard to how evenly a hash function scatters the search keys –How well does the hash function scatter random data? –How well does the hash function scatter nonrandom data? General requirements of a hash function –The calculation of the hash function should involve the entire search key –If a hash function uses module arithmetic, the base should be prime

© 2006 Pearson Addison-Wesley. All rights reserved13 B-16 Table Traversal: An Inefficient Operation Under Hashing Hashing as an implementation of the ADT table –For many applications, hashing provides the most efficient implementation –Hashing is not efficient for Traversal in sorted order Finding the item with the smallest or largest value in its search key Range query In external storage, you can simultaneously use –A hashing implementation of the tableRetrieve operation –A search-tree implementation of the ordered operations

© 2006 Pearson Addison-Wesley. All rights reserved5 B-17 The JCF Hashtable and TreeMap Classes JCF Hashtable implements a hash table –Maps keys to values –Large collection of methods JCF TreeMap implements a red-black tree –Guarantees O(log n) time for insert, retrieve, remove, and search –Large collection of methods

© 2006 Pearson Addison-Wesley. All rights reserved13 B-18 Data With Multiple Organizations Many applications require a data organization that simultaneously supports several different data- management tasks –Several independent data structures do not support all operations efficiently –Interdependent data structures provide a better way to support a multiple organization of data

© 2006 Pearson Addison-Wesley. All rights reserved13 B-19 Summary A 2-3 tree and a tree are variants of a binary search tree in which the balanced is easily maintained The insertion and deletion algorithms for a tree are more efficient than the corresponding algorithms for a 2-3 tree A red-black tree is a binary tree representation of a tree that requires less storage than a tree An AVL tree is a binary search tree that is guaranteed to remain balanced Hashing as a table implementation calculates where the data item should be rather than search for it

© 2006 Pearson Addison-Wesley. All rights reserved13 B-20 Summary A hash function should be extremely easy to compute and should scatter the search keys evenly throughout the hash table A collision occurs when two different search keys hash into the same array location Hashing does not efficiently support operations that require the table items to be ordered Hashing as a table implementation is simpler and faster than balanced search tree implementations when table operations such as traversal are not important to a particular application Several independent organizations can be imposed on a given set of data