Download presentation

Presentation is loading. Please wait.

Published byKrystal Borum Modified over 2 years ago

1
1 Symbol Tables Chapter 12.1-12.3 Sedgewick

2
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
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
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
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
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
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
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
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
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
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
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
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
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
15 Symbol Tables …ST_keyIndexed.c //Exercise void stDelete(STab st, Item i){ } //Exercise Item stSelect(STab st, int n){ }

16
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
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
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
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
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
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
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
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
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
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
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
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
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
29 Symbol Tables Symbol Table Implementation 4: Binary Search Tree items

30
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
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)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google