RED-BLACK TREE SEARCH THE FOLLOWING METHOD IS IN TreeMap.java:

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Queues and Stacks.  Can receive multiple requests from multiple sources ◦ How do we services these requests?  First come, first serve processing 
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Hashing as a Dictionary Implementation
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.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Implementation of Linear Probing (continued) Helping method for locating index: private int findIndex(long key) // return -1 if the item with key 'key'
Maps & Hashing Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
HASHING CSC 172 SPRING 2002 LECTURE 22. Hashing A cool way to get from an element x to the place where x can be found An array [0..B-1] of buckets Bucket.
hashing1 Hashing It’s not just for breakfast anymore!
Tirgul 9 Hash Tables (continued) Reminder Examples.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 8 Hash Tables (continued) Reminder Examples.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
Hashing as a Dictionary Implementation Chapter 20 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
Chapter 6 Array-Based Lists.
EECE 310: Software Engineering Iteration Abstraction.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
CHAPTER 5 ArrayLists.
Hashing Hashing is another method for sorting and searching data.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Hashing as a Dictionary Implementation Chapter 19.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
The Map ADT and Hash Tables. 2 The Map ADT  Map: An abstract data type where a value is "mapped" to a unique key  Need a key and a value to insert new.
CSC 427: Data Structures and Algorithm Analysis
Hashing - 2 Designing Hash Tables Sections 5.3, 5.4, 5.4, 5.6.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Foundations of Data Structures Practical Session #10 Hash Tables.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
1 CSC 427: Data Structures and Algorithm Analysis Fall 2011 Space vs. time  space/time tradeoffs  hashing  hash table, hash function  linear probing.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
October 6, Algorithms and Data Structures Lecture VII Simonas Šaltenis Aalborg University
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Hashing O(1) data access (almost) -access, insertion, deletion, updating in constant time (on average) but at a price… references: Weiss, Goodrich & Tamassia,
CMSC 341 Hashing Readings: Chapter 5. Announcements Midterm II on Nov 7 Review out Oct 29 HW 5 due Thursday CMSC 341 Hashing 2.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
1 Hash Tables Chapter Motivation Many applications require only: –Insert –Search –Delete Examples –Symbol tables –Memory management mechanisms.
“Sense shines with a double luster when it is set in humility. An able and yet humble man is a jewel worth a kingdom.” – William Penn Thought for the Day.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Chapter 12: The Map Abstract Data Type
Lecture 15: More Iterators
Hashing as a Dictionary Implementation
ITI Introduction to Computing II Lab-12
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Hashing in java.util
Collision Handling Collisions occur when different elements are mapped to the same cell.
Podcast Ch21b Title: Collision Resolution
Presentation transcript:

RED-BLACK TREE SEARCH THE FOLLOWING METHOD IS IN TreeMap.java:

IMPLEMENTATION OF THE HashMap CLASS

public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException ("Illegal Initial Capacity: "+ initialCapacity); if (loadFactor <= 0) throw new IllegalArgumentException ("Illegal Load factor: "+ loadFactor); if (initialCapacity==0) initialCapacity = 1; this.loadFactor = loadFactor; table = new Entry[initialCapacity]; threshold = (int)(initialCapacity * loadFactor); } // constructor

FOR THE containsKey, get, put, AND remove METHODS, THE INITIAL STRATEGY IS THE SAME: HASH key TO index ; SEARCH LINKED LIST AT table [index].

TIME ESTIMATES: LET n = count, LET m = table.length. ASSUME THE UNIFORM HASHING ASSUMPTION HOLDS.

THE AVERAGE SIZE OF EACH LIST IS n / m

FOR THE containsKey METHOD, averageTime S (n, m)  n / 2m iterations. BUT n / m <= loadFactor, A CONSTANT (ASSIGNED IN THE CONSTRUCTOR) SO averageTime S (n, m) < A CONSTANT. averageTime S (n, m) IS CONSTANT.

FIELDS IN THE HashIterator CLASS: Entry[ ] table = HashMap.this.table; int index = table.length; START AT BACK Entry entry = null; Entry lastReturned = null; // where the iterator is // currently positioned int type; // 0 for keys, 1 for values, 2 for entries private int expectedModCount = modCount;

public boolean hasNext() { while (entry==null && index>0) entry = table[--index]; return entry != null; } // method hasNext

public Object next( ) { if (modCount != expectedModCount) throw new ConcurrentModificationException( ); while (entry==null && index>0) entry = table[--index]; if (entry != null) { Entry e = lastReturned = entry; entry = e.next; return type == KEYS ? e.key : (type == VALUES ? e.value : e); } // non-null entry throw new NoSuchElementException( ); } // method next

KEYS null PART OF table null

remove (new Integer ( )); 260 null 261 null null containsKey ( new Integer ( )) WILL RETURN false BECAUSE INDEX 261 HAS null.

SOLUTION: boolean markedForRemoval; put SETS markedForRemoval TO false; remove SETS markedForRemoval TO true.

AFTER INITIAL PUTS: 260 null false false false false 265 null

AFTER remove ( new Integer ( )); 260 null true false false false 265 null

HERE IS PART OF remove : Entry e = table [index]; if (!e.markedForRemoval && e.hash == hash && key.equals(e.key)) { count--; Object oldValue = e.value; e.markedForRemoval = true; return oldValue; } // if match index = (index + 1) % table.length; OFFSET OF 1

THIS SOLUTION LEADS TO ANOTHER PROBLEM: SUPPOSE table.length = 1000 put remove ( put AND remove A TOTAL OF 950 TIMES) … put (REPEATED 40 TIMES) count = 40, SO NO NEED TO RE-HASH

TOO MANY!

SOLUTION: KEEP TRACK OF REMOVALS: private transient int countPlus; // the value of count + the // number of removals since // table.length was last changed public Object put(Object key, Object value) {... if (countPlus >= threshold) rehash( );...

private void rehash( ) { … for (int i = 0; i < oldCapacity ; i++) if (oldMap [i] != null && !oldMap [i].markedForRemoval) put (oldMap [i].key, oldMap [i].value); …

private void rehash( ) { DON’T REHASH IF … MARKED FOR REMOVAL for (int i = 0; i < oldCapacity ; i++) if (oldMap [i] != null && !oldMap [i].markedForRemoval) put (oldMap [i].key, oldMap [i].value); …

CLUSTER: A SEQUENCE OF NON-EMPTY LOCATIONS 260 null false false false false 265 null KEYS THAT HASH TO 261 FOLLOW THE SAME PATH AS KEYS THAT HASH TO 262, …

SOLUTION: DOUBLE HASHING, THAT IS, OBTAIN BOTH INDICES AND OFFSETS BY HASHING: index = hash & 0x7FFFFFFF % table.length; offset = hash & 0x7FFFFFFF / table.length; NOW THE OFFSET DEPENDS ON THE KEY, SO DIFFERENT KEYS WILL USU- ALLY HAVE DIFFERENT OFFSETS, SO NO MORE PRIMARY CLUSTERING!

TO GET A NEW INDEX: index = (index + offset) % table.length;

EXAMPLE: table.length = 11 key index offset WHERE WOULD THESE KEYS GO IN table ?

index key

PROBLEM: WHAT IF OFFSET IS MULTIPLE OF table.length ? EXAMPLE: table.length = 11 key index offset // BUT 15 IS AT INDEX 4 FOR KEY 246, NEW INDEX = (4 + 22) % 11 = 4. OOPS!

SOLUTION : if (offset % table.length == 0) offset = 1; ON AVERAGE, offset % table.length WILL EQUAL 0 ONLY ONCE IN EVERY table.length TIMES.

PROBLEM: WHAT IF table.length HAS SEVERAL FACTORS? EXAMPLE: table.length = 20 key index offset // BUT 30 IS AT INDEX 10 FOR KEY 110, NEW INDEX = (10 + 5) % 20 = 15, WHICH IS OCCUPIED, SO NEW INDEX = (15 + 5) % 20, WHICH IS OCCUPIED, SO NEW INDEX =...

SOLUTION: MAKE table.length A PRIME.