Data Structures.  Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: 000000000 – 999999999  A fast search:

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Array Representation
DATA STRUCTURES USING C++ Chapter 5
Backup Slides. An Example of Hash Function Implementation struct MyStruct { string str; string item; };
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Queue Overview Queue ADT Basic operations of queue
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Using arrays – Example 2: names as keys How do we map strings to integers? One way is to convert each letter to a number, either by mapping them to 0-25.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 10 Sept 29 Goals: hashing dictionary operations general idea of hashing hash functions chaining closed hashing.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
hashing1 Hashing It’s not just for breakfast anymore!
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 Standard Containers: Lists Gordon College Resource:
CS2420: Lecture 33 Vladimir Kulyukin Computer Science Department Utah State University.
Searching Arrays Linear search Binary search small arrays
Aree Teeraparbseree, Ph.D
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
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.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
C++ Classes and Data Structures Jeffrey S. Childs
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Hashing as a Dictionary Implementation Chapter 19.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Hash Tables CSIT 402 Data Structures II. Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions.
Chapter 11 Hash Anshuman Razdan Div of Computing Studies
“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.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Dictionaries and Hashing CSCI 3333 Data Structures.
Associative Containers Sets Maps Section 4.8. Associative Containers.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
1 Designing Hash Tables Sections 5.3, 5.4, 5.5, 5.6.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Searching Arrays Linear search Binary search small arrays
Stacks.
Stack and Queue APURBO DATTA.
Hash Tables.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Data structures in C++.
Presentation transcript:

Data Structures

 Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: –  A fast search: SSN is index of an array  E.g.  myItem is at position  myItem = myArray[ ]  Characteristics  Good--Efficiency of O(1)  Bad-- Requires array of size 10 9, most of which is unoccupied  Can we have something that  Has efficiency of nearly O(1) and  Requires reasonable amount of space

 Keys (e.g., SSN) are “hashed” (mish-mashed) to another number, which can be used as index for a smaller array. E.g., item1 key:  89 myArray[89] = item1 Item2 key:  75 myArray[75] = item2 Item3 key:  21 myArray[21] = item3 Item4 key:  21 myArray[?] = item4  Last 2 cases: collision, when two keys hash to the same hash value

item1:  89 item2:  75 item3:  21 item4:  21 Item5:  75 Item6:  24 Item7:  20 … … 21 … myTable 24 Ω Ω Ω Ω Ω Ω Ω Ω Ω item7 item6 item4 item3 item5item2 item1 … Data KeyHashed Index

item1:  89 item2:  75 item3:  21 item4:  21 Item5:  75 Item6:  24 Item7:  20 … … 21 … myTable 24 Data KeyHashed Index Item 7 Item3 22 Item4 Item 6 Item 2 Item 5 Item 1

 Given names as keys:  amy  tammy  frank  billy  Convert keys to array indices.  Max size of array: 10  name  (hashF1) integer  (hashF2) index  E.g.,  amy  Ascii(a) + Ascii(m) + Ascii(y)  = = % 10 = 7  tammy  Ascii(t) + Ascii(a) + Ascii(m) + Ascii(m) + Ascii(y) = = % 10 = 2 … … myTable 7 Ω Ω Ω Ω Ω Ω Ω tammy amy …

 Given: key =  h(key) = key % 100   67   21  Given: key = beth  h 1 (key) = ascii(b) + ascii(e) + ascii(t) + ascii(h) = = 419  h 2 (419) = 415 % 100 = 19  h(key) = h2(h1(key)) = 19  Thus, “beth” -> 19

 void insert (elemType &item);  Inserts item in the table,  bool remove (keyType key);  Removes data item corresponding to key. Returns true when successful; false, otherwise.  bool search (keyType key);  Searches the table for item corresponding to key. Returns true when match found; false, otherwise.  elemType search(keyType key);  Searches and return the value corresponding to key  book isEmpty();  void clear();

 bool search (keyType key); 1. hash(key)  anIndex T(n) = c1 2. IF array[anIndex] is not empty THEN T(n) = c2 + c3 search the linked list ELSE value is not present END IF 3. return true/false T(n) = c4 1. T(n) = c1 + c2 + c3 + c4 O(1) almost

1. Describe the data structure to represent a hash table 2. Write a C++ code for the structure, to be included in the private section of the (hash) Table class.

Ω Ω Ω Ω Ω Ω Ω Ω Ω Ω ArrayStruct

 To represent a group of data elements of different types as a single unit  Contrast this with an array, whose elements must of the same type.  Is like a class, but every part is public by default. No operations is a struct.

Definition : struct athlete_t { string name; int weight; // in kg int height; // in cm }; Usage: athlete_t ath; ath.name = “John”; ath.weight = 200; ath.height = 190; cout << ath.name << endl; cout << ath.weight << endl; cout << ath.height << endl; athlete_t team[MAX]; team[0] = ath;

private: struct nodeType { elemType data; nodeType *next; }; static const int MAX = 10; nodeType* list[MAX]; };

class Table { public: Table(); void insert(elemType item); bool remove(keyType key); bool retrieve(keyType key, elemType &item); void clear(); void print(); private:... };

1. Constructor 1. What needs to be done in the constructor? 2. Write a C++ code to implement Table(). 2. Insert 1. What are the steps necessary to insert an item into the Hash table? 2. Write a C++ code to implement insert(elemType item) in the Table class.