Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University
Data Structures
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
2
Static structures
Shape and size of the structure do not change over time Easier to manage 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
3
Dynamic structures
Pointers
A memory cell (or perhaps a block of memory cells) that contains the address of another memory cell Examples
Stack Storage
Use of Pointers
Arrays
Two-Dimensional Array
Row major order vs. column major order Finding an entry in the i th row and j th column of a c-column 2-D array stored in row major order
x c (i 1) ( j 1)
Address polynomial
9
Mini Review
Show how the array below would be arranged in main memory when stored in row major order
5 4 1 3 2 9 7 8 6
10
Answer
5 3 7 4 2 8 1 9 6
11
Mini Review
Give a formula for finding the entry in the i th row and j 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?
12
Answers
x r ( j 1) (i 1)
ci j
13
List
Contiguous Lists
15
Contiguous List
Convenient storage structure when implementing static lists Inconvenient in dynamic cases
Delete a name
Add a name
16
Linked List
17
Deleting an Entry
18
Inserting an Entry
19
20
Stacks
A list in which all insertions and deletions are performed at the same end A last-in, first-out (LIFO) structures Push and pop
Top B A
C
21
Backtracking
22
Backtracking
23
24
25
Procedure ReversePrint
26
A Stack in Memory
27
Push an entry Pop an entry Testing for an empty stack Testing for a full stack Can be also implemented as a linked list
28
Mini Review
Based on the technique for implementing a stack in a contiguous block of cells, what condition indicates that the stack is empty? Design a procedure for popping an entry off the stack that is implemented with a stack pointer. Your procedure should print an error message if the stack is empty.
29
Answers
The stack pointer points to the cell immediately below the base of the stack procedure pop() if( the stack pointer points below the stack base ) then ( exit with error message ) Extract the entry pointed by the stack pointer; Adjust the stack pointer to point to the next lower stack entry
30
Queue
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 Head (front) and tail (rear)
31
Operations on Queues
32
Crawling Queue
33
34
35
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.
Insert entries A, B, C Remove two entries Insert entries D, E Remove an entry Insert entry F Remove an entry
36
Answer
H A B C
H C T
H B C
H C
T
E
H
C T D E H E
H
D
H
D
T
F
37
Organization Chart
38
Trees
39
40
Trees
Nodes
Subtrees Depth
41
42
43
44
Inefficient Storage
45
Mini Review
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.
y x z
w
46
Answer
Y Z NIL NIL
NIL
NIL NIL
47
48
Ordered Tree
49
50
51
52
Mini Review
Draw a binary tree to store the list R, S, T, U, V, W, X, Y, and Z for future searching
53
Answer
V T S R Y
U
W
54
55
Inorder traversal
Preorder traversal
Postorder traversal
56
57
58
Inserting an Entry
59
User-Defined Types
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
60
Classes
Data types
A predetermined storage system A collection of predefined operations Define both storage system and operations
Classes
63
C++ Class
64
Using Classes
65
Encapsulation
The private data and procedures can not be referenced directly from outside All access to the instance must be performed via the public method The integrity of these data types is protected from poorly conceived modifications
66
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
67
68
69
2RXY 1RXY
XY S
70
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:
Answers
CA CA CA
72
Exercise
73