Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Data Abstractions Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University."— Presentation transcript:

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

2 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 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

4 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 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 6 Organization Chart

7 7 File Structure of Windows

8 8 Trees

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

10 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 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

12 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 13 Use of Pointers

14 14 Stack and Heap Space Heap Storage Stack Storage

15 15 Homogeneous Arrays

16 16 Two-Dimensional Array

17 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 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 537 428 196

19 19 Answer 5 3 7 4 2 8 1 9 6 5 3 7 4 2 8 1 9 6

20 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 21 Answers

22 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 23 Contiguous Lists

24 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 25 Linked List

26 26 Deleting an Entry

27 27 Inserting an Entry

28 28 A Stack in Memory

29 29 Operations on Queues

30 30 Circular Queues (1)

31 31 Circular Queues (2)

32 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

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

34 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 35 Binary Tree Node

36 36 Linked Storage System

37 37 Storage without Pointers

38 38 Inefficient Storage

39 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 40 Answer YZNIL X W YXZW

41 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 42 Printing a Linked List

43 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 44 Ordered Tree

45 45 Search the Binary Tree

46 46 Search Binary Tree

47 47 Printing a Binary Tree

48 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 49 Answer V T Y S U R XZ W

50 50 Printing a Tree in Order

51 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 52 Inserting an Entry (1)

53 53 Inserting an Entry (2)

54 54 Inserting an Entry

55 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 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 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 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 59 C++ Class

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

61 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 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 63 Expanding the Machine Language to Take Advantage of Pointers

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

65 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 66 Answers CA CA

67 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


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

Similar presentations


Ads by Google