1 Symbol Tables Chapter 12.1-12.3 Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

2. Getting Started Heejin Park College of Information and Communications Hanyang University.
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables III.
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
COSC2007 Data Structures II Chapter 11 Tables & Priority Queues I.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Chapter 3: Linked List Tutor: Angie Hui
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
CS16: Introduction to Data Structures & Algorithms
Chapter 7: Arrays In this chapter, you will learn about
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Information Systems Today: Managing in the Digital World
Chapter 17 Linked Lists.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked Lists.
David Luebke 1 6/7/2014 ITCS 6114 Skip Lists Hashing.
Linked Lists Chapter 4.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
11 Data Structures Foundations of Computer Science ã Cengage Learning.
ITEC200 Week04 Lists and the Collection Interface.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Abstract Data Types and Algorithms
Data Structures Using C++
LIST PROCESSING.
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
CSCI2100B Linked List Jeffrey
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Hash Tables.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 2Creating Relationships between Tables.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
© 2012 National Heart Foundation of Australia. Slide 2.
ITEC200 Week10 Sorting. pdp 2 Learning Objectives – Week10 Sorting (Chapter10) By working through this chapter, students should: Learn.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
25 seconds left…...
Januar MDMDFSSMDMDFSSS
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 12 Binary Search Trees
©2004 Brooks/Cole FIGURES FOR CHAPTER 12 REGISTERS AND COUNTERS Click the mouse to move to the next page. Use the ESC key to exit this chapter. This chapter.
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Data Structures Using C++ 2E
Garfield AP Computer Science
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
Symbol Tables Computing 2 COMP s1. S YMBOL T ABLES Searching: like sorting, searching is a fundamental element of many computational tasks data.
Presentation transcript:

1 Symbol Tables Chapter Sedgewick

2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name in a phone book selecting records in databases searching for pages on the web Characteristics of searching: typically, very large amount of data (very many items ) "information need" specified by keys (search terms) effective keys identify a small proportion of data

3 Symbol Tables …Searching In our context (DS&A), we abstract the problem to: we have a large collection of items each item contains key values and other data The search problem: input: a key value output: item(s) containing that key

4 Symbol Tables …Searching We assume: keys are unique each item has one key How do we represent a key in an item? Extend Item.h to handle complex items. Items now have a key and data

5 Symbol Tables Complex Items – Item.h typedef int Key; //Can change key type struct record{ Key keyVal; char value[10]; //Can change value type }; typedef struct record * Item; #define NULLitem NULL //This indicates no item #define key(A) ((A)->keyVal) #define compare(A,B) ((A) – (B)) void itemShow(Item);

6 Symbol Tables A symbol table (dictionary) is a collection of items with unique keys that has operations to Insert a new item Return an item with a particular key Applications of symbol tables: programming language processors (e.g. compilers, interpreters) text processing systems (spell-checkers, document retrieval)

7 Symbol Tables Symbol Table Interface SymbolTable.h typedef struct sTabRep * STab; // Create a new Symbol Table STab stInit(); // insert an item in symbol table void stInsert(STab s,Item i); // return item with given key in table // return NULLItem if key is not in the table Item stSearch(STab s,Key k);

8 Symbol Tables..First Class Symbol Table Interface SymbolTable.h // return the number of items in the table int stCount(STab s); // Delete the given item from table void stDelete(STab s,Item i); // Find the nth item in table Item stSelect(STab s,int n); // Traverse items in key order void stSort(STab s);

9 Symbol Tables Insertion into the SymbolTable What does insert do if key already exists in table? approach A: do nothing (insertion fails silently) approach B: return an error indication approach C: replace existing item associated with key We use approach A and provide a replace function if necessary

10 Symbol Tables Example: Symbol Table Client Using a symbol table Generate an ordered list of random numbers with no duplicates Use stSearch to check if it is already in the table If not use stInsert to insert it! Use stSort to print out the numbers in order Only really care about the key of the item See stClient1.c for full implementation

11 Symbol Tables Exercise: Random Number Checker Write a program that uses a symbol table to generate many random #'s in the range 1..N count frequency of occurrence of each Expectation: all frequencies roughly equal What are the items? What does the key represent What does the value represent

12 Symbol Tables Symbol Table Implementation 1: Key Indexed Array Use key to determine the position of the item in the array Requires dense keys (ie. Few gaps) Keys must be integral (or easy to map to integral values) NULLitem [0][1][2][7][6][5][3][4] 1,data3,data4,data5,data7,data items

13 Symbol Tables ST_keyIndexed.c struct sTabRep{ Item *items; int count; int size; }; //Assume keys are from 0 – (max-1) and are unique STab stInit(){ int i; Stab st = malloc(struct sTabRep); STab st->items = malloc((max)* sizeof(Item)); for(i=0;i< max;i++) st->items[i] = NULLitem; } st->count = 0; st->size = max; return st; }

14 Symbol Tables …ST_keyIndexed.c int stCount(STab st){ assert(st != NULL); return st->count; } void stInsert(STab st, Item i){ assert(st != NULL); if(compare(key(i), st->size) < 0 && compare(st->items[key(i)],NULLitem) == 0){ st->items[key(i)] = item; st->count++; } //Exercise: Item stSearch(STab st, Key k);

15 Symbol Tables …ST_keyIndexed.c //Exercise void stDelete(STab st, Item i){ } //Exercise Item stSelect(STab st, int n){ }

16 Symbol Tables …ST_keyIndexed.c //Traverse all items in sorted order void stSort(STab st){ int i; assert(st != NULL); for(i = 0; i size; i++){ if(compare(st->items[i],NULLitem) != 0){ showItem(st->items[i]); }

17 Symbol Tables Properties: Key Indexed Array Implementation Insert, search and delete and count are O(1) Init, select and sort are O(n) Problem: May have large gaps in array due to sparse keys. Not suitable for all types of data Large range of keys Key cannot easily be mapped to unique index

18 Symbol Tables Symbol Table Implementation2: Ordered Array Enter items into array without leaving gaps Put items in key order Can use linear or binary search to find items items

19 Symbol Tables ST_orderedArray.c //Data structure representation is the same struct sTabRep{ Item *items; int count; int size; }; STab stInit(){ int i; Stab st = malloc(struct sTabRep); assert(st != NULL); STab st->items = malloc((max)* sizeof(Item)); st->count = 0; st->size = max; return st; }

20 Symbol Tables …ST_orderedArray.c Item search(STab st, Key k) { int i; Item returnVal; assert(st != NULL && st->items != NULL); i = findInArray(k, st->items, 0, st->count-1); if( i count && compare(key(st->items[i]),k) == 0){ returnVal = st->items[i]; }else{ returnVal = NULLitem; } return returnVal; }

21 Symbol Tables …ST_orderedArray.c void stInsert(STab st, Item it) { assert(st != NULL && st->items != NULL); assert(st->count size); int i = findInArray(key(it),st->items,0,st->count -1); if (i count && compare(key(st->items[i]),key(it)) != 0){ int j; for (j = st->count; j > i; j--) st->items[j] = st->items[j-1]; } st->items[i] = it; st->count++; }

22 Symbol Tables Linear Search for findInArray() //does not indicate if k is there or not //return value 0 indicates k all keys in array //return value N for array of size N indicates that //k is larger than all keys in array int findInArray(Key k, Item a[], int lo, int hi) { int i, diff; for (i = lo; i 0; i++) { diff = compare(k, key(a[i])); } return i; }

23 Symbol Tables Binary Search for findInArray() int findInArray(Key k, Item a[], int lo, int hi) { int returnVal; if (hi <= lo) { returnVal = lo; }else{ int mid = (hi+lo)/2; int diff = compare(k, key(a[mid])); if (diff < 0){ returnVal = findInArray(k, a, lo, mid); } else if (diff > 0){ returnVal = findInArray(k, a, mid+1, hi); }else{ returnVal = mid; } return returnVal; }

24 Symbol Tables Cost Analysis of Searching Linear Search: best case: key is min key (1 comparison) worst case: key is not in array ( N comparisons) average case: key is in middle ( N/2 comparisons) Binary Search: best case: key is mid key (1 comparison) worst case: key is not in array ( log 2 N comparisons) average case: find key part-way through partitioning

25 Symbol Tables Properties: Ordered Array Implementation Init, Count O(1) Search O(logn) Assuming binary search Insertion, Deletion O(n) Need to shuffle items along to fill gaps

26 Symbol Tables Symbol Table Implementation 3: Linked Lists Linked list of items, maintained in key order No real need for max size Must use linear search items

27 Symbol Tables ST_LinkedList.c typedef struct sTabRep *STab; typedef typedef struct node Node; struct node { Item data; Node *next; }; struct STabRep { Node *items; int count; int max; };

28 Symbol Tables Properties: Ordered Linked List Implementation Init, count O(1) Search, Insert, Delete O(n) best case: key is min key (1 comparison) worst case: key is max key ( n comparisons) average case: key is in middle ( n/2 comparisons)

29 Symbol Tables Symbol Table Implementation 4: Binary Search Tree items

30 Symbol Tables ST_bst.c typedef struct sTabRep *STab; typedef typedef struct node Node; typedef struct node *Tree; struct node { Item data; Node *left; Node *right; }; struct sTabRep { Node *items; int count; int size; };

31 Symbol Tables Properties Binary Search Tree Implementation Init, Count O(1) We use a counter to keep track of how many items in the tree Insert, Search, Delete Average height worst case O(n) Max height worst case O(nlogn)