More on Data Structures in C CS-2301 D-term 20091 More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.

Slides:



Advertisements
Similar presentations
Lecture 22, Revision 1 Lecture notes Java code – CodeFromLectures folder* Example class sheets – 4 of these plus solutions Extra examples (quicksort) Lab.
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Hash Tables and Constant Access Time CS-2303, C-Term Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Hash Tables Professor Jennifer Rexford COS 217.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Symbol Tables Symbol tables are used by compilers to keep track of information about variables functions class names type names temporary variables etc.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CSC 143 Trees [Chapter 10].
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Review for Final Exam – cs411/511 Definitions (5 questions, 2 points each) Algorithm Analysis (3 questions, 3 points each) General Questions (3 questions,
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
353 3/30/98 CSE 143 Trees [Sections , 13.6,13.8]
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees Implementing Balancing Operations Hash Tables
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 15.
Week 6 - Wednesday CS221.
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Tree data structure.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Tree data structure.
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Chapter 20: Binary Trees.
Presentation transcript:

More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

More on Data Structures in C CS-2301 D-term Linked List Review Linear data structure Easy to grow and shrink Easy to add and delete items Time to search for an item – O(n)

More on Data Structures in C CS-2301 D-term Linked List (continued) payload next payload next payload next payload next struct listItem *head;

More on Data Structures in C CS-2301 D-term Doubly-Linked List (review) prevnext payload prevnext payload prevnext payload prevnext payload struct listItem *head, *tail;

More on Data Structures in C CS-2301 D-term AddAfter(item *p, item *new) Simple linked list {new -> next = p -> next; p -> next = new; } Doubly-linked list {new -> next = p -> next; if (p -> next) p->next->prev = new; new -> prev = p; p -> next = new; }

More on Data Structures in C CS-2301 D-term AddAfter(item *p, item *new) Simple linked list {new -> next = p -> next; p -> next = new; } Doubly-linked list {new -> next = p -> next; if (p -> next) p->next->prev = new; new -> prev = p; p -> next = new; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term AddAfter(item *p, item *new) Simple linked list {new -> next = p -> next; p -> next = new; } Doubly-linked list {new -> next = p -> next; if (p -> next) p->next->prev = new; new -> prev = p; p -> next = new; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term AddAfter(item *p, item *new) Simple linked list {new -> next = p -> next; p -> next = new; } Doubly-linked list {new -> next = p -> next; if (p -> next) p->next->prev = new; new -> prev = p; p -> next = new; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term AddAfter(item *p, item *new) Simple linked list {new -> next = p -> next; p -> next = new; } Doubly-linked list {new -> next = p -> next; if (p -> next) p->next->prev = new; new -> prev = p; p -> next = new; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term deleteNext(item *p) Simple linked list {if (p->next != NULL) p->next = p->next-> next; } Doubly-linked list Complicated Easier to deleteItem

More on Data Structures in C CS-2301 D-term deleteItem(item *p) Simple linked list Not possible without having a pointer to previous item! Doubly-linked list {if(p->next != NULL) p->next->prev = p->prev; if(p->prev != NULL) p->prev->next = p->next; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term deleteItem(item *p) Simple linked list Not possible without having a pointer to previous item! Doubly-linked list {if(p->next != NULL) p->next->prev = p->prev; if(p->prev != NULL) p->prev->next = p->next; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term deleteItem(item *p) Simple linked list Not possible without having a pointer to previous item! Doubly-linked list {if(p->next != NULL) p->next->prev = p->prev; if(p->prev != NULL) p->prev->next = p->next; } prevnext payload prevnext payload prevnext payload

More on Data Structures in C CS-2301 D-term Special Cases of Linked Lists Queue:– –Items always added to tail –Items always removed from head Stack:– –Items always added to head –Items always removed from head Singly-linked list works okay Need pointers to head and tail Singly-linked list works okay Only need pointer to head

More on Data Structures in C CS-2301 D-term Bubble Sort a Linked List item *BubbleSort(item *p) { if (p->next != NULL) { item *q = p->next, *qq = p; for (;q != NULL; qq = q, q = q->next) if (p->payload > q->payload){ /*swap p and q */ } p->next = BubbleSort(p->next); }; return p; }

More on Data Structures in C CS-2301 D-term Bubble Sort a Linked List item *BubbleSort(item *p) { if (p->next != NULL) { item *q = p->next, *qq = p; for (;q != NULL; qq = q, q = q->next) if (p->payload > q->payload){ item *temp = p->next; p->next = q->next; q->next = temp; qq->next = p; p = q; } p->next = BubbleSort(p->next); }; return p; }

More on Data Structures in C CS-2301 D-term Bubble Sort a Linked List item *BubbleSort(item *p) { if (p->next != NULL) { item *q = p->next, *qq = p; for (;q != NULL; qq = q, q = q->next) if (p->payload > q->payload){ item *temp = p->next; p->next = q->next; q->next = temp; qq->next = p; p = q; } p->next = BubbleSort(p->next); }; return p; } Head of (sub)list being sortedPointer to step thru (sub)list Pointer to item previous to q in (sub)list

More on Data Structures in C CS-2301 D-term Potential Exam Questions Analyze BubbleSort to determine if it is correct, and fix it if incorrect. Hint: you need to define “correct” Hint2: you need to define a loop invariant to convince yourself Draw a diagram showing the nodes, pointers, and actions of the algorithm

More on Data Structures in C CS-2301 D-term Observations:– What is the order (Big-O notation) of the Bubble Sort algorithm? Answer: O(n 2 ) Note that Quicksort is faster – O(n log n) on average Pages 87 & 110 in Kernighan and Ritchie Potential exam question:– why?

More on Data Structures in C CS-2301 D-term Questions?

More on Data Structures in C CS-2301 D-term Binary Tree (review) A linked list but with two links per item struct treeItem { type payload; treeItem *left; treeItem *right; }; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

More on Data Structures in C CS-2301 D-term Binary Trees (continued) Two-dimensional data structure Easy to grow and shrink Easy to add and delete items at leaves More work needed to insert or delete branch nodes Search time is O(log n) If tree is reasonably balanced Degenerates to O(n) in worst case if unbalanced

More on Data Structures in C CS-2301 D-term Order of Traversing Binary Trees In-order Traverse left sub-tree (in-order) Visit node itself Traverse right sub-tree (in-order) Pre-order Visit node first Traverse left sub-tree Traverse right sub-tree Post-order Traverse left sub-tree Traverse right sub-tree Visit node last

More on Data Structures in C CS-2301 D-term Order of Traversing Binary Trees In-order Traverse left sub-tree (in-order) Visit node itself Traverse right sub-tree (in-order) Pre-order Visit node first Traverse left sub-tree Traverse right sub-tree Post-order Traverse left sub-tree Traverse right sub-tree Visit node last Programming Assignment #6

More on Data Structures in C CS-2301 D-term Example of Binary Tree x = (a.real*b.imag - b.real*a.imag) / sqrt(a.real*b.real – a.imag*b.imag) = x/ sqrt - **.. arealbimag.. brealaimag - …

More on Data Structures in C CS-2301 D-term Question What kind of traversal order is required for this expression? In-order? Pre-order? Post-order?

More on Data Structures in C CS-2301 D-term Binary Trees in Compilers Used to represent the structure of the compiled program Optimizations Common sub-expression detection Code simplification Loop unrolling Parallelization Reductions in strength – e.g., substituting additions for multiplications, etc. Many others

More on Data Structures in C CS-2301 D-term Questions about Trees? or about Programming Assignment 6?

More on Data Structures in C CS-2301 D-term New Challenge What if we require a data structure that has to be accessed by value in constant time? I.e., O(log n) is not good enough! Need to be able to add or delete items Total number of items unknown But an approximate maximum might be known

More on Data Structures in C CS-2301 D-term Examples Anti-virus scanner Symbol table of compiler Virtual memory tables in operating system Bank account for an individual

More on Data Structures in C CS-2301 D-term Observation Arrays provide constant time access … … but you have to know which element you want! We only know the contents of the item we want! Also Not easy to grow or shrink Not open-ended Can we do better?

More on Data Structures in C CS-2301 D-term Answer – Hash Table Definition:– Hash Table A data structure comprising an array (for constant time access) A set of linked lists (one list for each array element) A hashing function to convert search key to array index

More on Data Structures in C CS-2301 D-term Definition Search key:– a value stored as (part of) the payload of the item you are looking for Need to find the item containing that value (i.e., key)

More on Data Structures in C CS-2301 D-term Answer – Hash Table Definition:– Hash Table A data structure comprising an array (for constant time access) A set of linked lists (one list for each array element) A hashing function to convert search key to array index Definition:– Hashing function (or simply hash function) A function that takes the search key in question and “randomizes” it to produce an index So that non-randomness of keys avoids concentration of too many elements around a few indices in array See §6.6 in Kernighan & Ritchie

More on Data Structures in C CS-2301 D-term data next Hash Table Structure item... data next data next data next data next data next data next data next data next data next data next data next data next

More on Data Structures in C CS-2301 D-term Guidelines for Hash Tables Lists from each item should be short I.e., with short search time (approximately constant) Size of array should be based on expected # of entries Err on large side if possible Hashing function Should “spread out” the values relatively uniformly Multiplication and division by prime numbers usually works well

More on Data Structures in C CS-2301 D-term Example Hashing Function P. 144 of K & R #define HASHSIZE 101 unsigned int hash(char *s) { unsigned int hashval; for (hashval = 0; *s != ‘\0’; s++) hashval = *s + 31 * hashval; return hashval % HASHSIZE }

More on Data Structures in C CS-2301 D-term Example Hashing Function P. 144 of K & R #define HASHSIZE 101 unsigned int hash(char *s) { unsigned int hashval; for (hashval = 0; *s != ‘\0’; s++) hashval = *s + 31 * hashval; return hashval % HASHSIZE } Note choice of prime numbers to “mix it up”

More on Data Structures in C CS-2301 D-term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ }

More on Data Structures in C CS-2301 D-term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ } Hash table is indexed by hash value of s

More on Data Structures in C CS-2301 D-term Using a Hash Table struct item *lookup(char *s) { struct item *np; for (np = hashtab[hash(s)]; np != NULL; np = np -> next) if (strcmp(s, np->data) == 0) return np; /*found*/ return NULL;/* not found */ } Traverse the linked list to find item s

More on Data Structures in C CS-2301 D-term Using a Hash Table (continued) struct item *addItem(char *s, …) { struct item *np; unsigned int hv; if ((np = lookup(s)) == NULL) { np = malloc(item); /* fill in s and data */ np -> next = hashtab[hv = hash(s)]; hashtab[hv] = np; }; return np; }

More on Data Structures in C CS-2301 D-term Using a Hash Table (continued) struct item *addItem(char *s, …) { struct item *np; unsigned int hv; if ((np = lookup(s)) == NULL) { np = malloc(item); /* fill in s and data */ np -> next = hashtab[hv = hash(s)]; hashtab[hv] = np; }; return np; } Inserts new item at head of the list indexed by hash value

More on Data Structures in C CS-2301 D-term Hash Table Summary Widely used for constant time access Easy to build and maintain There exist an art and science to the choice of hashing functions Consult textbooks, web, etc.

More on Data Structures in C CS-2301 D-term Questions?