1 Data Abstractions Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University.

Slides:



Advertisements
Similar presentations
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
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.
Grade 12 Computer Studies HG
Chapter 8 Data Abstractions. 2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Connecting with Computer Science 2 Objectives Learn what a data structure is and how it is used Learn about single and multidimensional arrays and how.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Pointers and Dynamic Data Structures Problem Solving, Abstraction,
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Connecting with Computer Science, 2e Chapter 8 Data Structures.
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.
Data Structures Systems Programming. 22 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Data structures Abstract data types Java classes for Data structures and ADTs.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
Overview of Course Java Review 1. This Course Covers, using Java Abstract data types Design, what you want them to do (OOD) Techniques, used in implementation.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Chapter 8 Data Abstractions. © 2005 Pearson Addison-Wesley. All rights reserved 8-2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
12/8/2015CS Introduction to Information Engineering1 Data Structure 12/8/2015 Che-Rung Lee.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structure and Algorithms
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 12 – Data Structures
Class Review.
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
12 C Data Structures.
Planning & System installation
Chapter 8: Data Abstractions
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Chapter 8: Data Abstractions
Further Data Structures
Pointers & Dynamic Data Structures
Chapter 20: Binary Trees.
Chapter 8: Data Abstractions
Presentation transcript:

1 Data Abstractions Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University

2 Data Structures Conceptual organization of data Conceptual organization of data Basic data structures Basic data structures Homogeneous array Homogeneous array List List Stack Stack Queue Queue Tree Tree

3 List A collection of entries that appear in a sequential order A collection of entries that appear in a sequential order Examples Examples Class enrollment lists Class enrollment lists “ things-to-do ” lists “ things-to-do ” lists Dictionaries Dictionaries Sentences Sentences Appear in both static and dynamic forms Appear in both static and dynamic forms

4Stacks A list in which all insertions and deletions are performed at the same end A list in which all insertions and deletions are performed at the same end A last-in, first-out (LIFO) structures A last-in, first-out (LIFO) structures Push and pop Push and pop C A B Top

5 Queue A list in which all insertions are performed at one end while all deletions are made at the other A list in which all insertions are performed at one end while all deletions are made at the other A first-in, first-out (FIFO) structure A first-in, first-out (FIFO) structure Head (front) and tail (rear) Head (front) and tail (rear)

6 Organization Chart

7 File Structure of Windows

8 Trees

9 Trees Nodes Nodes Root Root Terminal (leaf) Terminal (leaf) Parent, children, siblings Parent, children, siblings Subtrees Subtrees Depth Depth

10 Data Abstraction Explores ways in which users can be shielded from the details of actual data storage (memory cells and address) and be allowed to access information as though it were stored in a more convenient form – Concept of abstraction Explores ways in which users can be shielded from the details of actual data storage (memory cells and address) and be allowed to access information as though it were stored in a more convenient form – Concept of abstraction The term user can be either human or a software module The term user can be either human or a software module

11 Static vs. Dynamic Structures Static structures Static structures Shape and size of the structure do not change over time Shape and size of the structure do not change over time Easier to manage Easier to manage Dynamic structures Dynamic structures Either shape or size of the structure changes over time Either shape or size of the structure changes over time Must deal with adding and deleting data entries as well as finding the memory space required by a growing data structure Must deal with adding and deleting data entries as well as finding the memory space required by a growing data structure

12Pointers A memory cell (or perhaps a block of memory cells) that contains the address of another memory cell A memory cell (or perhaps a block of memory cells) that contains the address of another memory cell Examples Examples Program counter as an instruction pointer Program counter as an instruction pointer URL URL Many programming languages include pointers as a primitive data type Many programming languages include pointers as a primitive data type Allow the declaration, allocation, and manipulation of pointers Allow the declaration, allocation, and manipulation of pointers Used to create dynamic data structures Used to create dynamic data structures

13 Use of Pointers

14 Stack and Heap Space Heap Storage Stack Storage

15 Homogeneous Arrays

16 Two-Dimensional Array

17 Storage of a 2-D Array Row major order vs. column major order Row major order vs. column major order Finding an entry in the th row and th column of a -column 2-D array stored in row major order Finding an entry in the th row and th column of a -column 2-D array stored in row major order Address polynomial

18 Mini Review Show how the array below would be arranged in main memory when stored in row major order Show how the array below would be arranged in main memory when stored in row major order

19 Answer

20 Mini Review Give a formula for finding the entry in the th row and th column of a 2-D array stored in column major order Give a formula for finding the entry in the th row and th column of a 2-D array stored in column major order In C, C++, and Java, indices of arrays start at 0 rather than 1. In this case what address polynomial is used by the translator to convert references of the form Array[i][j] into memory address? In C, C++, and Java, indices of arrays start at 0 rather than 1. In this case what address polynomial is used by the translator to convert references of the form Array[i][j] into memory address?

21 Answers

22 Storing lists Contiguous list = list stored in a homogeneous array Contiguous list = list stored in a homogeneous array Linked list = list in which each node points to the next one Linked list = list in which each node points to the next one Head pointer = pointer to first entry in list Head pointer = pointer to first entry in list NIL pointer = non-pointer value used to indicate end of list NIL pointer = non-pointer value used to indicate end of list

23 Contiguous Lists

24 Contiguous List Convenient storage structure when implementing static lists Convenient storage structure when implementing static lists Inconvenient in dynamic cases Inconvenient in dynamic cases Delete a name Delete a name Move entries to keep the list in the same order Move entries to keep the list in the same order Add a name Add a name Move the entire list to obtain an available block of contiguous cells large enough for the expanded list Move the entire list to obtain an available block of contiguous cells large enough for the expanded list

25 Linked List

26 Deleting an Entry

27 Inserting an Entry

28 A Stack in Memory

29 Operations on Queues

30 Circular Queues (1)

31 Circular Queues (2)

32 Mini Review Using paper and pencil, keep a record of the circular queue during the following scenario. Assume that the block reserved for the queue can contain only four entries. Using paper and pencil, keep a record of the circular queue during the following scenario. Assume that the block reserved for the queue can contain only four entries. Insert entries A, B, C Insert entries A, B, C Remove two entries Remove two entries Insert entries D, E Insert entries D, E Remove an entry Remove an entry Insert entry F Insert entry F Remove an entry Remove an entry

33Answer ABC H T BC H T C H T DC HT EDC HT ED T H EFD T H EF H T

34 Storing a binary tree Linked structure Linked structure Each node = data cell + two child pointers Each node = data cell + two child pointers Accessed through a pointer to root node Accessed through a pointer to root node Mapped to a contiguous array Mapped to a contiguous array A[1] = root node A[1] = root node A[2],A[3] = children of A[1] A[2],A[3] = children of A[1] A[4],A[5],A[6],A[7] = children of A[2] and A[3] A[4],A[5],A[6],A[7] = children of A[2] and A[3] …

35 Binary Tree Node

36 Linked Storage System

37 Storage without Pointers

38 Inefficient Storage

39 Mini Review Draw a diagram representing how the tree below appears in memory when stored using the left and right child pointers. Draw a diagram representing how the tree below appears in memory when stored using the left and right child pointers. Draw another diagram showing how the tree would appear in contiguous storage. Draw another diagram showing how the tree would appear in contiguous storage. y x z w

40 Answer YZNIL X W YXZW

41 Manipulating data structures Ideally, a data structure should be manipulated solely by pre-defined procedures. Ideally, a data structure should be manipulated solely by pre-defined procedures. Example: A stack typically needs at least push and pop procedures. Example: A stack typically needs at least push and pop procedures. The data structure along with these procedures constitutes a complete abstract tool. The data structure along with these procedures constitutes a complete abstract tool.

42 Printing a Linked List

43 Binary Tree Package Search for the presence of an entry Search for the presence of an entry Use the binary search algorithm Use the binary search algorithm Print the list in order Print the list in order Insert a new entry Insert a new entry

44 Ordered Tree

45 Search the Binary Tree

46 Search Binary Tree

47 Printing a Binary Tree

48 Mini Review Draw a binary tree to store the list R, S, T, U, V, W, X, Y, and Z for future searching Draw a binary tree to store the list R, S, T, U, V, W, X, Y, and Z for future searching

49 Answer V T Y S U R XZ W

50 Printing a Tree in Order

51 Traversing a Binary Tree Inorder traversal Inorder traversal Left – Root – Right Left – Root – Right Preorder traversal Preorder traversal Root – Left – Right Root – Left – Right Postorder traversal Postorder traversal Left – Right – Root Left – Right – Root

52 Inserting an Entry (1)

53 Inserting an Entry (2)

54 Inserting an Entry

55 User-Defined Types Expressing an algorithm is often more convenient if types other than those provided as primitives in the programming language Expressing an algorithm is often more convenient if types other than those provided as primitives in the programming language Many modern programming languages allow programmers to define additional data types, using the primitive types and structures as building blocks Many modern programming languages allow programmers to define additional data types, using the primitive types and structures as building blocks

56 Customized data types User-defined data type = template for a heterogeneous structure User-defined data type = template for a heterogeneous structure Abstract data type = user-defined data type with methods for access and manipulation Abstract data type = user-defined data type with methods for access and manipulation Class = abstract data type with extra features Class = abstract data type with extra features Characteristics can be inherited Characteristics can be inherited Contents can be encapsulated Contents can be encapsulated Constructor methods to initialize new objects Constructor methods to initialize new objects

57 Customized Data Types struct { char Name[8]; int Age; float SkillRating; } Employee; typedef struct { char Name[8]; int Age; float SkillRating; } EmployeeType; EmployeeType DistManager, SalesRep1, SalesRep2;

58 Declaration of Nodes of a Binary Tree typedef struct { char data; char data; Node* left; Node* left; Node* right; Node* right; } Node; Node* root;

59 C++ Class

60 Using Classes StackOfIntegers StackOne(50); StackOfIntegers StackOne(50); StackOne.push(106); StackOne.push(106); int OldValue = StackOne.pop(); int OldValue = StackOne.pop();

61 Standard Template Library A collection of predefined classes in C++ that describe popular data structures A collection of predefined classes in C++ that describe popular data structures The programmer is thus relieved from the task of describing these structures in detail The programmer is thus relieved from the task of describing these structures in detail

62 Pointers in Machine Language Machine language defined in Appendix C Machine language defined in Appendix C Load data (immediate addressing) Load data (immediate addressing) 2RXY 2RXY Load address (direct addressing) Load address (direct addressing) 1RXY 1RXY Load through pointer (indirect addressing) Load through pointer (indirect addressing) DRXY DRXY DR0S DR0S Save through pointer Save through pointer ER0S ER0S XY R S

63 Expanding the Machine Language to Take Advantage of Pointers

64 Loading a Register from a Memory Cell Located by a Pointer in a Register

65 Mini Review Suppose the machine language has been extended to include pointer operations. Moreover, suppose register 8 contains the pattern DB, the memory cell at address DB contains the pattern CA, and the cell at address A5 contains the pattern CA. What bit pattern will be in register 5 immediately after executing the following instructions: Suppose the machine language has been extended to include pointer operations. Moreover, suppose register 8 contains the pattern DB, the memory cell at address DB contains the pattern CA, and the cell at address A5 contains the pattern CA. What bit pattern will be in register 5 immediately after executing the following instructions: 15A5 15A5 25CA 25CA D508 D508

66 Answers CA CA

67 Exercise Review problems Review problems 2, 5, 8, 14, 16, 19, 24, 26, 30, 44 2, 5, 8, 14, 16, 19, 24, 26, 30, 44