Symbol Tables. Gianpiero Cabodi and Paolo Camurati Dip. Automatica e Informatica Politecnico di Torino
|
|
- Arron Hall
- 6 years ago
- Views:
Transcription
1 Symbol Tables Gianpiero Cabodi and Paolo Camurati Dip. Automatica e Informatica Politecnico di Torino
2 Symbol Tables Definition: A Symbol Table is a data structure with records including a key basic and allowing operations such as: Insertion of a new record Search of a record with a given key. Sometimes symbol tables are denoted with the term dictionary. A.Y. 2016/17 08 Symbol Tables 2
3 Applications of Symbol Tables Applications Target, i.e., searching Key Return Value Dictionary Definition Word Definition Book index Relevant pages Word Page list DNS IP address given its URL URL IP address Invers DNS URL given its IP address IP address URL File system File on disk File name Disk location Web search Web page Keyword Page list A.Y. 2016/17 08 Symbol Tables 3
4 Symbol Table ADT Operations: Insert a new element Search an element of a given key Delete an element of a given key Select the r-th smallest element Order the table using the key Union of two tables. A.Y. 2016/17 08 Symbol Tables 4
5 Search algorithm = implementation of the symbol table ADT. Record of type Item including keys of type Key. Operations on the Key type: key KEYget(Item); key KEYscan(); int KEYcompare(key, key); Compare 2 keys Get key from element Read key from standard input A.Y. 2016/17 08 Symbol Tables 5
6 Operations on the Item type: Item ITEMscan(); void ITEMshow(Item); int ITEMcheckvoid(Item); Item ITEMsetvoid(); void ITEMfree(Item); int ITEMless(Item, Item); int ITEMgreater(Item, Item); Read an element from standard input Print-out an element on standard output Check element emptyness Free an element Create an empty element Compare 2 elements given their keys A.Y. 2016/17 08 Symbol Tables 6
7 I Category ADT Item.h #define MAXC 3 typedef struct item *Item; typedef char *Key; Item ITEMscan(); void ITEMshow(Item data); int ITEMcheckvoid(Item data); Item ITEMsetvoid(); void ITEMfree(Item data); int ITEMless(Item data1, Item data2); int ITEMgreater(Item data1, Item data2); Key KEYget(Item data); Key KEYscan(); int KEYcompare(Key k1, Key k2); A.Y. 2016/17 08 Symbol Tables 7
8 Item.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Item.h" Direct Access Table: letters A..Z item Other Tables: string name value struct item { char *name; int value; ; Given a letter, this function int getindex(key key) { if (key == NULL) return -1; computes the corresponding return (key[0] -'A'); integer. For direct access tables only. Item ITEMscan() { char name[maxc]; int value; Item data=(item) malloc(sizeof(struct item)); printf("name = "); scanf("%s", name); data->name = strdup(name); printf("value = "); scanf("%d", &value); data->value = value; return data; A.Y. 2016/17 08 Symbol Tables 8
9 void ITEMshow(Item data) { printf("\nname = %s", data->name); printf("\nvalue = %d \n", data->value); int ITEMcheckvoid(Item data) { Key k1, k2=""; k1 = KEYget(data); if (KEYcompare(k1,k2)==0) return 1; else return 0; Item ITEMsetvoid() { char name[maxc]=""; Item tmp = (Item) malloc(sizeof(struct item)); if (tmp!= NULL) { tmp->name = strdup(name); tmp->value = -1; return tmp; A.Y. 2016/17 08 Symbol Tables 9
10 void ITEMfree(Item data) { if (data == NULL) return; if (data->name!= NULL) free(data->name); free(data); int ITEMless (Item data1, Item data2) { Key k1 = KEYget(data1), k2 = KEYget(data2); if (KEYcompare(k1, k2) == -1) return 1; else return 0; int ITEMgreater(Item data1, Item data2) { Key k1 = KEYget(data1), k2 = KEYget(data2); if (KEYcompare(k1, k2) == 1) return 1; else return 0; A.Y. 2016/17 08 Symbol Tables 10
11 Key KEYscan() { char name[maxc]; Key k; scanf("%s", name); k = strdup(name); return k; int KEYcompare(Key k1, Key k2) { return strcmp(k1, k2); Key KEYget(Item data) { return data->name; A.Y. 2016/17 08 Symbol Tables 11
12 I Category ADT ST ST.h typedef struct symboltable *ST; ST STinit(int) ; void STfree(ST) ; int STcount(ST) ; int STinsert(ST,Item) ; int STsearch(ST,Key) ; void STdelete(ST,Key) ; Item STdisplay_item(ST, int) ; int STselect(ST,int) ; void STdisplay(ST) ; A.Y. 2016/17 08 Symbol Tables 12
13 Implementations Linear structures: Direct Access Tables Arrays: Unordered Ordered Lists: Unordered Ordered A.Y. 2016/17 08 Symbol Tables 13
14 Tree structures: Binary Search Trees (BSTs) Balanced Trees: RB-tree B-tree Hash Tables A.Y. 2016/17 08 Symbol Tables 14
15 Direct Access Tables maxn: Number of elements in the entire universe U Key k U Function getindex(k) returns an integer from 0 to maxn-1, acting as an array index Examples of getindex: If keys are capital letters in the English alphabet A..Z, getindex(k) = k- A ; If keys are integers from 0 to maxn-1, getindex(k) = k; A.Y. 2016/17 08 Symbol Tables 15
16 array st->a[maxn]: If the key k is in the table, its position st- >a[getindex(k)], otherwise st- >a[getindex(k)] stores an empty element Store a set K of K keys ( K = st->size). A.Y. 2016/17 08 Symbol Tables 16
17 A B H E G N M P O Y Q C D I Z T U V K (Used Keys) U (Keys Universe) K J X F L W S R A.Y. 2016/17 08 Symbol Tables st C D F I
18 ST.c #include <stdio.h> #include <stdlib.h> #include "Item.h" #include "ST.h" struct symboltable {Item *a; int maxn; int size;; ST STinit(int maxn) { ST st; int i; st = malloc(sizeof(*st)); st->a = malloc(maxn * sizeof(item) ); for (i = 0; i < maxn; i++) st->a[i] = ITEMsetvoid(); st->maxn = maxn; st->size = 0; return st; void STfree(ST st) { int i; for(i=0; i<st->maxn; i++) ITEMfree(st->a[i]); if (st->a!= NULL) free(st->a); free(st); A.Y. 2016/17 08 Symbol Tables 18
19 int STcount(ST st) { return st->size; int STinsert(ST st, Item data) { int index = getindex(keyget(data)); st->a[index] = data; st->size++; return index; int STsearch(ST st, Key k) { int index = getindex(k); if (ITEMcheckvoid(st->a[index])) return -1; return index; void STdelete(ST st, Key k) { st->a[getindex(k)] = ITEMsetvoid(); st->size--; A.Y. 2016/17 08 Symbol Tables 19
20 int STselect(ST st, int r) { int i; for (i = 0; i < st->maxn; i++) if ( (ITEMcheckvoid(st->a[i])==0) && (r-- == 0)) return getindex(keyget(st->a[i])); return -1; void STdisplay(ST st){ int i; for (i = 0; i < st->maxn; i++) if (ITEMcheckvoid(st->a[i])==0) ITEMshow(st->a[i]); Item STdisplay_item(ST st, int j) { return st->a[j]; A.Y. 2016/17 08 Symbol Tables 20
21 Pros & Cons Insert, search, and delete complexity: T(n) = (1) Init and select complexity: T(n) = (maxn) Memory usage: S(n) = ( U ) = (maxn) Applicable only for small maxn Memory loss for K << maxn Often used to convert keys into integers (and viceversa) with a cost equal to 1. A.Y. 2016/17 08 Symbol Tables 21
22 Unordered Array Function to compare keys Tail insertion Use realloc to resize tables during insert Search through linear visit Select has no sens, as it supposes an order Delete with a left-shift of one position for all subsequent elements. A.Y. 2016/17 08 Symbol Tables 22
23 ST.c #include <stdio.h> #include <stdlib.h> #include "Item.h" #include "ST.h" struct symboltable { Item *a; int maxn; int size;; ST STinit(int maxn) { ST st; st = malloc(sizeof(*st)); if (st == NULL) { printf("memory allocation error\n"); return NULL; st->a = malloc(maxn * sizeof(item) ); for (i = 0; i < maxn; i++) st->a[i] = NULL; st->maxn = maxn; st->size = 0; return st; A.Y. 2016/17 08 Symbol Tables 23
24 void STfree(ST st) { int i; for(i=0; i<st->size; i++) ITEMfree(st->a[i]); if (st->a!= NULL) free(st->a); free(st); int STcount(ST st) { return st->size; int STinsert(ST st, Item data) { int i = st->size; if (st->size >= st->maxn) { st->a = realloc(st->a, (2*st->maxN)*sizeof(Item)); if (st->a == NULL) return -1; st->maxn = 2*st->maxN; st->a[i] = data; st->size++; return i; A.Y. 2016/17 08 Symbol Tables 24
25 int STsearch(ST st, Key k) { int i; if (st->size == 0) return -1; for (i = 0; i < st->size; i++) if (KEYcompare(k, KEYget(st->a[i]))==0) return i; return -1; void STdelete(ST st, Key k) { int i, j=0; while (KEYcompare(KEYget(st->a[j]), k)!=0) j++; for (i = j; i < st->size-1; i++) st->a[i] = st->a[i+1]; st->size--; void STdisplay(ST st){ int i; for (i = 0; i < st->size; i++) ITEMshow(st->a[i]; A.Y. 2016/17 08 Symbol Tables 25
26 Pros & Cons Init and insert complexity: T(n) = (1) Search and delete complexity: T(n) = O(n) Memory usage S(n) = (maxn) Applicable only for small maxn Memory loss for K << maxn Often used to transform keys into integers with a cost equal to 1. The inverse operation has a linear cost. A.Y. 2016/17 08 Symbol Tables 26
27 Ordered Arrays Ordering pro & cons: Ordered insertions have a linear cost T(n) = O(n), the overall cost is quadratic for n insertions T(n) = O(n 2 ) Dicothomic searches have a logarithmic cost T(n) = O(logn) Linear searches (by stopping as soon as possible) have cost equal to T(n) = O(n) Deletes have with linear cost T(n) = O(n) Select is straighforward: Position and index do coincide. A.Y. 2016/17 08 Symbol Tables 27
28 Context Dependent Implementation: Dynamic with many insert/delete: ordered insertions (by shifting all larger elements) have a quadratic cost Static with insertions only while reading from file phase, no delete, and many searches: Insert on tail and order once with a cost equal to O(nlogn). A.Y. 2016/17 08 Symbol Tables 28
29 Dynamic Situation ST.c #include <stdio.h> #include <stdlib.h> #include "Item.h" #include "ST.h" struct symboltable { Item *a; int maxn; int size;; ST STinit(int maxn) { ST st; st = malloc(sizeof(*st)); if (st == NULL) { printf("memory allocation error\n"); return NULL; st->a = malloc(maxn * sizeof(item) ); st->maxn = maxn; st->size = 0; return st; A.Y. 2016/17 08 Symbol Tables 29
30 void STfree(ST st) { int i; for(i=0; i<st->size; i++) ITEMfree(st->a[i]); if (st->a!= NULL) free(st->a); free(st); int STcount(ST st) { return st->size; int STselect(ST st, int r) { return r; void STdelete(ST st, Key k) { int i, j = 0; while (KEYcompare(KEYget(st->a[j]), k)!=0) j++; for (i = j; i < st->size-1; i++) st->a[i] = st->a[i+1]; st->size--; A.Y. 2016/17 08 Symbol Tables 30
31 int STinsert(ST st, Item data) { int i = st->size++; if (st->size > st->maxn) { st->a = realloc(st->a, (2*st->maxN)*sizeof(Item)); if (st->a == NULL) return -1; st->maxn = 2*st->maxN; while( (i>0) && ITEMless(data, st->a[i-1]) ) { st->a[i] = st->a[i-1]; i--; st->a[i] = data; return i; void STdisplay(ST st){ int i; for (i = 0; i < st->size; i++) ITEMshow(st->a[i]); A.Y. 2016/17 08 Symbol Tables 31
32 Item STdisplay_item(ST st, int j) { return st->a[j]; int STlinsearch(ST st, Key k) { int i; if (st->size == 0) return -1; for (i = 0; i < st->size; i++) { if (KEYcompare(k, KEYget(st->a[i]))==0) return i; if (KEYcompare(k, KEYget(st->a[i]))==-1) break; return -1; A.Y. 2016/17 08 Symbol Tables 32
33 int searchr(st st, int l, int r, Key k) { int m = (l + r)/2; if (l > r) return -1; if (KEYcompare(k, KEYget(st->a[m]))==0) return m; if (l == r) return -1; if (KEYcompare(k, KEYget(st->a[m]))==-1) return searchr(st, l, m-1, k); else return searchr(st, m+1, r, k); int STbinsearch(ST st, Key k) { return searchr(st, 0, st->size-1, k) ; A.Y. 2016/17 08 Symbol Tables 33
34 Unordered Lists Head insertion Search through sequential visit (returning the element) Select has no sense, as it supposes an ordering Delete, once found the element, move only pointers Recursive search and delete. A.Y. 2016/17 08 Symbol Tables 34
35 ST.c #include <stdio.h> #include <stdlib.h> #include "Item.h" #include "ST.h" typedef struct STnode* link; struct STnode { Item data; link next; ; struct symboltable { link head; int size; link z; ; link NEW( Item data, link next) { link x = malloc(sizeof *x); x->data = data; x->next = next; return x; A.Y. 2016/17 08 Symbol Tables 35
36 ST STinit(int maxn) { ST st; st = malloc(sizeof(*st)); if(st == NULL) { printf("memory allocation error\n"); return NULL; st->size = 0; st->head = (st->z = NEW(ITEMsetvoid(), NULL)); return st; void STfree(ST st) { if (st == NULL) return; free(st->head); free(st); int STcount(ST st) { return st->size; A.Y. 2016/17 08 Symbol Tables 36
37 void STinsert(ST st, Item data) { st->head = NEW(data, st->head); st->size++; Item searchr(link t, Key k, link z) { if (t == z) return ITEMsetvoid(); if (KEYcompare(KEYget(t->data), k)==0) Wrapper return t->data; for searchr return searchr(t->next, k, z); Item STsearch(ST st,key k){ return searchr(st->head,k,st->z); void STdisplay(ST st) { link x = st->head; while (x!= st->z) { ITEMshow(x->data); x = x->next; A.Y. 2016/17 08 Symbol Tables 37
38 link deleter(link x, Key k) { if ( x == NULL ) return NULL; if (KEYcompare(KEYget(x->data), k)==0) { link t = x->next; free(x); return t; x->next = deleter(x->next, k); return x; void STdelete(ST st, Key k) { st->head = deleter(st->head, k); st->size--; A.Y. 2016/17 08 Symbol Tables 38
39 Pros & Cons Init and insert complexity: T(n) = (1) Search and delete complexity: T(n) = O(n) Memory usage: S(n) = (n) A.Y. 2016/17 08 Symbol Tables 39
40 Ordered Lists Ordered insertion Searches through sequential visits (returning an element) Selections through sequential visits would suppose an ordering Delete, once found an element, only pointers are moved Iterative insert, search, select, and delete. A.Y. 2016/17 08 Symbol Tables 40
41 ST.c #include <stdio.h> #include <stdlib.h> #include "Item.h" #include "ST.h" typedef struct STnode* link; struct STnode { Item data; link next; ; struct symboltable { link head; int size; link z; ; link NEW( Item data, link next) { link x = malloc(sizeof *x); x->data = data; x->next = next; return x; A.Y. 2016/17 08 Symbol Tables 41
42 ST STinit(int maxn) { ST st = malloc(sizeof(*st)); if(st == NULL) { printf("memory allocation error\n"); return NULL; st->size = 0; st->z = NEW(ITEMsetvoid(), NULL); st->head = NEW(ITEMsetvoid(), st->z); return st; void STfree(ST st) { if (st == NULL) return; free(st->head); free(st); int STcount(ST st) { return st->size; Lists with sentinels on the head and tail A.Y. 2016/17 08 Symbol Tables 42
43 void STinsert(ST st, Item data) { link x, t; t = malloc(sizeof(link)); t->data = data; t->next = NULL; for (x = st->head; x->next!= st->z; x = x->next) if (ITEMgreater(x->next->data, data)) break; t->next = x->next; x->next = t; st->size++; Item STsearch(ST st, Key k) { link x; for (x = st->head->next; x!= st->z; x = x->next) if (KEYcompare( KEYget(x->data), k) ==0) return x->data; return ITEMsetvoid(); A.Y. 2016/17 08 Symbol Tables 43
44 void STdelete(ST st, Key k) { link x; for (x = st->head; x->next!= st->z; x = x->next) if (KEYcompare( KEYget(x->next->data), k) ==0) x->next = x->next->next; return; Item STselect(ST st, int r) { int i; link x = st->head; for (i = r; i>=0; i--) x = x->next; return x->data; void STdisplay(ST st) { link x; for (x = st->head->next; x!= st->z; x = x->next) ITEMshow(x->data); A.Y. 2016/17 08 Symbol Tables 44
45 Pros & Cons Init complexity: T(n) = (1) Insert, search, select, and delete complexity: T(n) = O(n) Memory usage: S(n) = (n) A.Y. 2016/17 08 Symbol Tables 45
46 Complexity Worst Case Insert Search Select Direct Acc. Table 1 1 maxn Unordered A. 1 n Order. A. Linear S. n n 1 Order. A. Bin. S. N logn 1 Unordered List 1 n Ordered List n n n BST n n n RB-tree logn logn nlogn Hashing 1 n A.Y. 2016/17 08 Symbol Tables 46
47 Average Case Insert Hit Miss Direct Acc. Table Unordered A. 1 n/2 n/2 Order. A. Linear S. n/2 n/2 n/2 Order. A. Bin. S. n/2 logn logn Unordered List 1 n/2 N Ordered List n/2 n/2 n/2 BST logn logn logn RB-tree logn logn logn Hashing A.Y. 2016/17 08 Symbol Tables 47
48 References Symbol Tables Sedgewick 12.1 Direct Access Tables Cormen 12.1 Sedgewick 12.2 Ordered Tables on Arrays Sedgewick 12.3, 12.4 Unorderd Tables on Lists Sedgewick 12.3 A.Y. 2016/17 08 Symbol Tables 48
Binary Search Trees. Manolis Koubarakis. Data Structures and Programming Techniques
Binary Search Trees Manolis Koubarakis 1 Search The retrieval of a particular piece of information from large volumes of previously stored data is a fundamental operation, called search. Search is an important
More informationSymbol Tables. Computing 2 COMP x1
Symbol Tables Computing 2 COMP1927 16x1 SYMBOL TABLES Searching: like sorting, searching is a fundamental element of many computational tasks data bases dictionaries compiler symbol tables Symbol table:
More informationAdvanced Programming. Lists. A list is a data structure based on usage of pointers and dynamic allocation of memory.
Intro A list is a data structure based on usage of pointers and dynamic allocation of memory. With respect to other ADT (like arrays), a list: provides more flexibility in memory usage but it is less efficient.
More informationSolution for Data Structure
Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT
More informationLecture P9: Trees. Overview. Searching a Database. Searching a Database
Overview Lecture P9: Trees Culmination of the programming portion of this class. Solve a database searching problem. Trees Versatile and useful data structure. A naturally recursive data structure. Application
More informationC Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 3 Pointers (revisited) int i = 4, j = 6, *p = &i, *q = &j, *r; if (p == &i)...; if (p == (& i))...;... = **&p;... = *(*(& p));... = 9 * *p / *q + 8;... = (((9*(*p)))/(*q)) + 8; *(r = &i)
More informationLecture Notes on Binary Search Trees
Lecture Notes on Binary Search Trees 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 15 March 6, 2014 1 Introduction In this lecture, we will continue considering associative
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 2. O(n) 2. [1 pt] What is the solution to the recurrence T(n) = T(n/2) + n, T(1)
More informationECE 122. Engineering Problem Solving Using Java
ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function
More informationComputer Science Foundation Exam
Computer Science Foundation Exam May 19, 2018 Section I A DATA STRUCTURES SOLUTION NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max
More informationECE264 Fall 2013 Exam 3, November 20, 2013
ECE264 Fall 2013 Exam 3, November 20, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More information8. Binary Search Tree
8 Binary Search Tree Searching Basic Search Sequential Search : Unordered Lists Binary Search : Ordered Lists Tree Search Binary Search Tree Balanced Search Trees (Skipped) Sequential Search int Seq-Search
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More informationAbstract Data Types (ADTs) & Design
Abstract Data Types (ADTs) & Design Readings: CP:AMA 19.5, 17.7 (qsort) CS 136 Fall 2017 12: Abstract Data Types 1 Selecting a data structure In Computer Science, every data structure is some combination
More informationWriting an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and
Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()
More informationAbstract Data Types (ADTs) & Design. Selecting a data structure. Readings: CP:AMA 19.5, 17.7 (qsort)
Abstract Data Types (ADTs) & Design Readings: CP:AMA 19.5, 17.7 (qsort) CS 136 Spring 2018 12: Abstract Data Types 1 Selecting a data structure In Computer Science, every data structure is some combination
More informationreturn return else return
compare0.c 1 // Compares two strings' addresses 4 #include 5 6 int main(void) 7 { 8 // get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // compare strings'
More informationC Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008
C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct
More informationBinary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39
Binary Search Tree 1.0 Generated by Doxygen 1.7.1 Mon Jun 6 2011 16:12:39 Contents 1 Binary Search Tree Program 1 1.1 Introduction.......................................... 1 2 Data Structure Index 3
More informationLecture 15 Binary Search Trees
Lecture 15 Binary Search Trees 15-122: Principles of Imperative Computation (Fall 2017) Frank Pfenning, André Platzer, Rob Simmons, Iliano Cervesato In this lecture, we will continue considering ways to
More informationESC101N: Fundamentals of Computing End-sem st semester
ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.
More informationStructured programming
Exercises 9 Version 1.0, 13 December, 2016 Table of Contents 1. Remainders from lectures.................................................... 1 1.1. What is a pointer?.......................................................
More informationAlgorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.1 SYMBOL TABLES Algorithms F O U R T H E D I T I O N API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationNYU SCPS X Section 1 Unix System Calls. Fall 2004 Handout 8. Source code on the Web at mm64/x /src/doubly2.
Fall 2004 Handout 8 Source code on the Web at http://i5.nyu.edu/ mm64/x52.9232/src/doubly2.c 1 /* 2 Let the user type in positive numbers, not necessarily unique. Store them in 3 ascending order in a dynamically
More informationENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review
ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27 Midterm #2 Review 1 Time and Location The midterm will be given in class during normal class hours, 11:00a.m.-12:15p.m.,
More informationSOFTWARE Ph.D. Qualifying Exam Fall 2017
(i) (4 pts.) SOFTWARE Ph.D. Qualifying Exam Fall 2017 Consider the following C program. #include #define START 2 #define LIMIT 60 #define STEP 7 #define SIZE 3 int main(void) { int i = START,
More informationWeek 6. Data structures
1 2 3 4 5 n Week 6 Data structures 6 7 n 8 General remarks We start considering Part III Data Structures from CLRS. As a first example we consider two special of buffers, namely stacks and queues. Reading
More informationDynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to
More informationelse return for return
addresses.c 1 // Prints two strings' addresses 4 #include 5 6 int main(void) 7 { 8 // Get two strings 9 string s = get_string("s: "); 10 string t = get_string("t: "); 11 1 // Print strings' addresses
More informationSearch Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17
Search Trees Data and File Structures Laboratory http://www.isical.ac.in/~dfslab/2017/index.html DFS Lab (ISI) Search Trees 1 / 17 Binary search trees. Definition. Binary tree in which following property
More informationECE264 Spring 2014 Exam 2, March 11, 2014
ECE264 Spring 2014 Exam 2, March 11, 2014 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationCSCI 104 Log Structured Merge Trees. Mark Redekopp
1 CSCI 10 Log Structured Merge Trees Mark Redekopp Series Summation Review Let n = 1 + + + + k = σk i=0 n = k+1-1 i. What is n? What is log (1) + log () + log () + log (8)++ log ( k ) = 0 + 1 + + 3+ +
More informationSample Examination. Family Name:... Other Names:... Signature:... Student Number:...
Family Name:... Other Names:... Signature:... Student Number:... THE UNIVERSITY OF NEW SOUTH WALES SCHOOL OF COMPUTER SCIENCE AND ENGINEERING Sample Examination COMP1917 Computing 1 EXAM DURATION: 2 HOURS
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationCS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2
CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.
More informationComputer Science Foundation Exam
Computer Science Foundation Exam August 26, 2017 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts Category
More informationImplementing an abstract datatype. Linked lists and queues
Computer Programming Implementing an abstract datatype. Linked lists and queues Marius Minea marius@cs.upt.ro 19 December 2016 Review: compilation basics Briefly: Compiler translates source code to executable
More informationProgramming in C. Lecture 5: Aliasing, Graphs, and Deallocation. Dr Neel Krishnaswami. Michaelmas Term University of Cambridge
Programming in C Lecture 5: Aliasing, Graphs, and Deallocation Dr Neel Krishnaswami University of Cambridge Michaelmas Term 2017-2018 1 / 20 The C API for Dynamic Memory Allocation void *malloc(size_t
More informationQueues. Manolis Koubarakis. Data Structures and Programming Techniques
Queues Manolis Koubarakis 1 The ADT Queue A queue Q of items of type T is a sequence of items of type T on which the following operations are defined: Initialize the queue to the empty queue. Determine
More informationCP2 Revision. theme: dynamic datatypes & data structures
CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;
More informationComp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette
Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette Name: Student No.: Duration : 50 minutes This midterm contains 18 questions on 4 pages This midterm will be marked out of 50. There are 60 total marks
More informationSolutions to Assessment
Solutions to Assessment [1] What does the code segment below print? int fun(int x) ++x; int main() int x = 1; fun(x); printf( %d, x); return 0; Answer : 1. The argument to the function is passed by value.
More informationTables. The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries
1: Tables Tables The Table ADT is used when information needs to be stored and acessed via a key usually, but not always, a string. For example: Dictionaries Symbol Tables Associative Arrays (eg in awk,
More informationIntroduction to Data Structures. Systems Programming
Introduction to Data Structures Systems Programming Intro to Data Structures Self-referential Structures Dynamic Memory Allocation A Simple malloc Example Linear Lists Linked Lists Insertion Example Linked
More informationOverview of Sorting Algorithms
Unit 7 Sorting s Simple Sorting algorithms Quicksort Improving Quicksort Overview of Sorting s Given a collection of items we want to arrange them in an increasing or decreasing order. You probably have
More informationIS 2610: Data Structures
IS 2610: Data Structures Searching March 29, 2004 Symbol Table A symbol table is a data structure of items with keys that supports two basic operations: insert a new item, and return an item with a given
More informationAssignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:
Assignment 6 Q1. Create a database of students using structures, where in each entry of the database will have the following fields: 1. a name, which is a string with at most 128 characters 2. their marks
More informationMid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:
Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering Mid-term Exam Name: This exam is closed book and notes. Read the questions carefully and focus your answers on what has
More informationLinked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.
Linked Data Structures Readings: CP:AMA 17.5 The primary goal of this section is to be able to use linked lists and trees. CS 136 Winter 2018 11: Linked Data Structures 1 Linked lists Racket s list type
More informationSignature: ECE 551 Midterm Exam
Name: ECE 551 Midterm Exam NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual work.
More informationMTH 307/417/515 Final Exam Solutions
MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =
More informationINDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming
More informationUnited States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017
United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 1. Do a page check: you should have 8 pages including this cover sheet. 2. You have 50 minutes
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. Final Examination
University of Illinois at Urbana-Champaign Department of Computer Science Final Examination CS 225 Data Structures and Software Principles Spring 2010 7-10p, Wednesday, May 12 Name: NetID: Lab Section
More informationTrees, Part 1: Unbalanced Trees
Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more
More informationComputer Science Foundation Exam
Computer Science Foundation Exam December 16, 2016 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts
More information2. Pointer to Array of Structure stores the Base address of the Structure array.
Pointer to Array of Structure 1. Pointer Variable can also Store the Address of the Structure Variable. 2. Pointer to Array of Structure stores the Base address of the Structure array. 3. Suppose struct
More informationProgramming in C Quick Start! Biostatistics 615 Lecture 4
Programming in C Quick Start! Biostatistics 615 Lecture 4 Last Lecture Analysis of Algorithms Empirical Analysis Mathematical Analysis Big-Oh notation Today Basics of programming in C Syntax of C programs
More informationDOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE
Chapter 1 : What is an application of linear linked list data structures? - Quora A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements
More information#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;
1. Short answer questions: (a) Compare the typical contents of a module s header file to the contents of a module s implementation file. Which of these files defines the interface between a module and
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationChapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Dynamic Data Structures Instructor: Öğr. Gör. Okan Vardarlı Copyright 2004 Pearson Addison-Wesley. All rights reserved. 12-1 Dynamic Data Structure Usually, so far, the arrays and strings we
More informationCS 315 Data Structures mid-term 2
CS 315 Data Structures mid-term 2 1) Shown below is an AVL tree T. Nov 14, 2012 Solutions to OPEN BOOK section. (a) Suggest a key whose insertion does not require any rotation. 18 (b) Suggest a key, if
More informationUniversity of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014
University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014 Midterm Examination Instructor: Ladan Tahvildari, PhD, PEng, SMIEEE Date: Tuesday,
More informationData Structures and Algorithms for Engineers
04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationWhat we have learned so far
What we have learned so far Straight forward statements Conditional statements (branching) Repeated statements (loop) Arrays One variable many data Problem: Read 10 numbers from the keyboard and store
More informationCSE 214 Computer Science II Searching
CSE 214 Computer Science II Searching Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction Searching in a
More informationCS 223: Data Structures and Programming Techniques. Exam 2. April 19th, 2012
CS 223: Data Structures and Programming Techniques. Exam 2 April 19th, 2012 Instructor: Jim Aspnes Work alone. Do not use any notes or books. You have approximately 75 minutes to complete this exam. Please
More informationRecursion. Example R1
Recursion Certain computer problems are solved by repeating the execution of one or more statements a certain number of times. So far, we have implemented the repetition of one or more statements by using
More informationAlgorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE
Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.1 SYMBOL TABLES Algorithms F O U R T H E D I T I O N API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE https://algs4.cs.princeton.edu
More informationUNIT-2 Stack & Queue
UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More informationint marks[10]; // fixed size and fixed address No change in Memory address.
Dynamic Memory Allocation : Used When we want to allocate memory during run time. int marks[10]; // fixed size and fixed address No change in Memory address. // fixed size. ( no change in size possible
More informationDownloaded from : Algorithm: Implementation QUESTION 1 :
QUESTION 1 : WRITE AN ALGORITHM THAT ACCEPTS A BINARY TREE AS INPUT AND PR INTS ITS HEIGHT TO STANDARD OUTPUT. Algorithm: 1. If tree is empty then return 0 2. Else (a) Get the max depth of left subtree
More informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex
More informationCSE wi: Practice Midterm
CSE 373 18wi: Practice Midterm Name: UW email address: Instructions Do not start the exam until told to do so. You have 80 minutes to complete the exam. This exam is closed book and closed notes. You may
More informationLecture Notes on Tries
Lecture Notes on Tries 15-122: Principles of Imperative Computation Thomas Cortina Notes by Frank Pfenning Lecture 24 April 19, 2011 1 Introduction In the data structures implementing associative arrays
More informationHashing Algorithms. Hash functions Separate Chaining Linear Probing Double Hashing
Hashing Algorithms Hash functions Separate Chaining Linear Probing Double Hashing Symbol-Table ADT Records with keys (priorities) basic operations insert search create test if empty destroy copy generic
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationMid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:
Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering Mid-term Exam Name: This exam is closed book and notes. Read the questions carefully and focus your answers on what has
More informationSolved by: Syed Zain Ali Bukhari (BSCS)
Solved by: Syed Zain Ali Bukhari (BSCS) 1. If there are N internal nodes in a binary tree then what will be the no. of external nodes in this binary tree? N -1 N N +1 (Page 303) N +2 2.If there are N elements
More informationProgramming. Lists, Stacks, Queues
Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation
More informationCOS 226 Midterm Exam, Spring 2009
NAME: login ID: precept: COS 226 Midterm Exam, Spring 2009 This test is 10 questions, weighted as indicated. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators
More informationComputer Science Foundation Exam
Computer Science Foundation Exam January 13, 2018 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts Category
More informationCS:3330 (22c:31) Algorithms
What s an Algorithm? CS:3330 (22c:31) Algorithms Introduction Computer Science is about problem solving using computers. Software is a solution to some problems. Algorithm is a design inside a software.
More informationPriority Queues (Heaps)
Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the
More informationDATA STRUCTURES AND ALGORITHMS
DATA STRUCTURES AND ALGORITHMS For COMPUTER SCIENCE DATA STRUCTURES &. ALGORITHMS SYLLABUS Programming and Data Structures: Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary
More informationArrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]
(November 10, 2009 2.1 ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I
More informationElementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington
Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Basic Types Types like integers, real numbers, characters.
More informationStacks. Manolis Koubarakis. Data Structures and Programming Techniques
Stacks Manolis Koubarakis 1 Stacks and Queues Linear data structures are collections of components arranged in a straight line. If we restrict the growth of a linear data structure so that new components
More informationAnalysis of Algorithms. Unit 4 - Analysis of well known Algorithms
Analysis of Algorithms Unit 4 - Analysis of well known Algorithms 1 Analysis of well known Algorithms Brute Force Algorithms Greedy Algorithms Divide and Conquer Algorithms Decrease and Conquer Algorithms
More informationSymbol Table. IP address
4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 2002 2010 4/2/11 10:40 AM Symbol Table Symbol table. Key-value pair abstraction.
More informationCGS 3460 Summer 07 Midterm Exam
Short Answer 3 Points Each 1. What would the unix command gcc somefile.c -o someotherfile.exe do? 2. Name two basic data types in C. 3. A pointer data type holds what piece of information? 4. This key
More informationDEPARTMENT OF MATHS, MJ COLLEGE
T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,
More informationThe C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and
The C Programming Language Part 4 (with material from Dr. Bin Ren, William & Mary Computer Science, and www.cpp.com) 1 Overview Basic Concepts of Pointers Pointers and Arrays Pointers and Strings Dynamic
More informationWrite a program that creates in the main function two linked lists of characters and fills them with the following values:
Write a program that creates in the main function two linked lists of characters and fills them with the following values: The first list will have 3 nodes with the following characters: A,B, and C. The
More informationLinked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Dynamic memory allocation: review typedef struct { int hitemp;
More informationBit Manipulation in C
Bit Manipulation in C Bit Manipulation in C C provides six bitwise operators for bit manipulation. These operators act on integral operands (char, short, int and long) represented as a string of binary
More informationCHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS
0 1 2 025-612-0001 981-101-0002 3 4 451-229-0004 CHAPTER 9 HASH TABLES, MAPS, AND SKIP LISTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
More information