Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 6 Dynamic data structures Motivation Common dynamic ADTs Stacks, queues, lists:
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
Data Structures Dynamic Sets Heaps Binary Trees & Sorting Hashing.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
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.
Data Structures from Cormen, Leiserson, Rivest & Stein.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
1 Welcome to Data Structures Spring 2009 The slides in this course includes slides by T. Tamir, A. Tal, S. Ar, Y. Moses. Thanks null
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Stack and Queue.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
Lecture 6 Data Structures. Stack top x.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
STACK Data Structure
Definition 2 A Dynamic Dictionary is a data structure of item with keys that support the following basic operations: (1) (1)Insert a new item (2) (2)Remove.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Elementary data structures
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Chapter 15 Lists Objectives
Data Structures and Algorithms
CPSC 311 Section 502 Analysis of Algorithm
CS 583 Analysis of Algorithms
Elementary Data Structures
Algorithms Part III. Data Structures
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Binary Search Trees (13.1/12.1)
Dynamic Sets (III, Introduction)
Data Structures and Algorithms
CS6045: Advanced Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Presentation transcript:

Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.

chap10Hsiu-Hui Lee Stacks and queues Stacks and queues are dynamic set in which element removed from the set by the DELETE operation is prespecified. In a stack the element deleted from the set is the one most recently inserted; the stack implements a last-in, first-out, or LIFO, policy. Similarly, in a queue, the element deleted is implements a first-in, first-out, or FIFO, policy.

chap10Hsiu-Hui Lee3 An array implementation of a stack S PUSH(S, 17) PUSH(S, 3) POP(S)

chap10Hsiu-Hui Lee4 STACK_EMPTY(S ) 1 if top[S] = 0 2then return TRUE 3else return FALSE O(1) empty, underflows, overflows

chap10Hsiu-Hui Lee5 PUSH(S,x) 1 top[S]  top[S]+1 2 S[top[S]]  x O(1) POP(S ) 1 if STACK-EMPTY(S ) 2then error “ underflow ” 3else top[S]  top[s] -1 4return S[top[S] + 1] O(1)

chap10Hsiu-Hui Lee6 An array implementation of a queue Q ENQUEUE(Q, 17) ENQUEUE (Q, 3) ENQUEUE (Q, 5) DEQUEUE(Q)

chap10Hsiu-Hui Lee7 ENQUEUE(Q, x ) 1Q[tail[Q]]  x 2 if tail[Q]= length[Q] 3 then tail[Q]  1 4 else tail[Q]  tail[Q]+1 O(1)

chap10Hsiu-Hui Lee8 DEQUEUE(Q) 1 x  Q[head[Q]] 2 if head[Q] = length[Q] 3 then head[Q]  1 4 else head[Q]  head[Q] +1 5 return x O(1)

chap10Hsiu-Hui Lee Linked lists Doubly linked list L: key, next, prev If prev[x]=NIL, the element has no predecessor and is the first element, or head, of the list. If next[x]=NIL, the element has no successor and is the last element, or tail, of the list. head[L] points to the first element of the list. If head[L]=NIL, the list is empty.

chap10Hsiu-Hui Lee10 LIST-SEARCH(L, k) 1 x  head[L] 2 while x ≠ NIL and key[x] ≠ k 3 do x  next[x] 4return x Θ (n) in worst case LIST-SEARCH(L, 4) returns a pointer to the third element LIST-SEARCH(L, 7) returns NIL

chap10Hsiu-Hui Lee11 LIST-INSERT(L, x) 1 next[x]  head[L] 2 if head[L] ≠ NIL 3then prev[head[L]]  x 4 head[L]  x 5 prev[x]  NIL O(1) LIST-INSERT(L, 25)

chap10Hsiu-Hui Lee12 LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2then next[prev[x]]  next[x] 3else head[L]  next[x] 4 if next[x] ≠ NIL 5 then prev[next[x]  prev[x] O(1) or O(n) Delete a specified element (Call LIST_SEARCH first O(n)) LIST-DELETE(L, 4)

chap10Hsiu-Hui Lee13 Circular, doubly linked list with a sentinel A Sentinel is a dummy object to simplify boundary conditions. The sentinel nil[L] is placed between the head and the tail. next[nil[L]] points to the head of the list and prev[nil[L]] points to the tail. Both the next field of the tail and the prev field of the head point to nil[L]. We can eliminate the attribute head[L], since next[nil[L]] points to the head.

chap10Hsiu-Hui Lee14 LIST_DELETE ’ (L, x) 1 next[prev[x]]  next[x] 2 prev[next[x]]  prev[x] LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2then next[prev[x]]  next[x] 3else head[L]  next[x] 4 if next[x] ≠ NIL 5 then prev[next[x]  prev[x] LIST-DELETE(L, 1)

chap10Hsiu-Hui Lee15 LIST_SEARCH ’ (L, k) 1 x  next[nil[L]] 2 while x ≠ nil[L] and key[x] ≠ k 3 do x  next[x] 4 return x LIST-SEARCH(L, k) 1 x  head[L] 2 while x ≠ NIL and key[x] ≠ k 3 do x  next[x] 4return x

chap10Hsiu-Hui Lee16 LIST_INSERT ’ (L, x) 1 next[x]  next[nil[L]] 2 prev[next[nil[L]]]  x 3 next[nil[L]]  x 4 prev[x]  nil[L] LIST-INSERT(L, x) 1 next[x]  head[L] 2 if head[L] ≠ NIL 3then prev[head[L]]  x 4 head[L]  x 5 prev[x]  NIL

chap10Hsiu-Hui Lee Implementing pointers and objects How to implement pointers and objects in languages, such as Fortran, that do not provide them?

chap10Hsiu-Hui Lee18 Multiple-array representation of objects

chap10Hsiu-Hui Lee19 A single array representation of objects

chap10Hsiu-Hui Lee20 Allocating and freeing objects-- garbage collector

chap10Hsiu-Hui Lee21 Allocate_object( ), LIST_INSERT(L,4), Key(4)=25

chap10Hsiu-Hui Lee22 LIST_DELETE(L,5), FREE_OBJECT(5)

chap10Hsiu-Hui Lee23 ALLOCATE_OBJECT( ) 1if free = NIL 2then error “ out of space ” 3else x  free 4free  next[x] 5return x FREE_OBJECT(x) 1 next[x]  free 2 free  x

chap10Hsiu-Hui Lee24 Two link lists

chap10Hsiu-Hui Lee Representing rooted trees

chap10Hsiu-Hui Lee26 Binary trees p[x], left[x], right[x]

chap10Hsiu-Hui Lee27 Rooted tree with unbounded branching

chap10Hsiu-Hui Lee28 Rooted tree with unbounded branching p[x], left-child[x], right-sibling[x]

chap10Hsiu-Hui Lee29 Other tree representation