cs 241 data organization abstract data types
play

CS 241 Data Organization Abstract Data Types April 3, 2018 - PowerPoint PPT Presentation

CS 241 Data Organization Abstract Data Types April 3, 2018 Abstract Data Types List sequence Set no duplicates Queue First In, First Out (FIFO) Stack Last In, First Out (LIFO) Priority Queue Highest


  1. CS 241 Data Organization Abstract Data Types April 3, 2018

  2. Abstract Data Types • List – sequence • Set – no duplicates • Queue – First In, First Out (FIFO) • Stack – Last In, First Out (LIFO) • Priority Queue – Highest priority first • Map – associates keys with values (some languages call it a dictionary )

  3. Implementation • Arrays • Linked Lists • Trees • Hash tables

  4. Priority Queue • Could implement same as stack or queue, using linked list with ordered elements • Don’t necessarily care about total order, so could use a heap.

  5. Map • For small maps, may make sense to just use a linked list of key,value pairs. • If keys are restricted to small range of integers, may just use an array. (Value for key k is at A[k].) • Binary search tree (see K & R 6.5) • Hash Table (see K & R 6.6)

  6. Hash Table The entries in a hash table are distributed across an array of buckets . In this figure we use a linked list to hold the entries in each bucket. key key value value NULL NULL key NULL value NULL NULL

  7. Hash Function • Given a particular key, compute an index for the bucket where it will be stored. • Often done in two steps 1. Compute integer hash value for key using some hashing function. 2. Find index using hash % array size • Good hash function uniformly distributes keys across the table, reducing collisions . • Perfect hash function would have no collisions. Can be created if keys are known in advance.

  8. Table structure struct TableEntry { char* key; int value; struct TableEntry* next; }; #define HASHSIZE 101 struct TableEntry* hashTable[HASHSIZE ];

  9. Hash function unsigned int hash(char* s) { unsigned int hashval; for(hashval = 0; *s != ’\0’; s++) { hashval = *s + 31 * hashval; } return hashval % HASHSIZE; }

  10. Lookup struct TableEntry* lookup(char* s) { struct TableEntry* entry = hashTable[hash(s)]; while(entry != NULL) { if(strcmp(s, entry ->key) == 0) { return entry; } entry = entry ->next; } return NULL; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend