Data Abstractions

advertisement
Data Abstractions
Shyh-Kang Jeng
Department of Electrical Engineering/
Graduate Institute of Communication Engineering
National Taiwan University
1
Data Structures


Conceptual organization of data
Basic data structures


Homogeneous array
List
Stack
 Queue


Tree
2
List


A collection of entries that appear in a
sequential order
Examples





Class enrollment lists
“things-to-do” lists
Dictionaries
Sentences
Appear in both static and dynamic forms
3
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
4
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)
5
Organization Chart
6
File Structure of Windows
7
Trees
8
Trees

Nodes





Root
Terminal (leaf)
Parent, children, siblings
Subtrees
Depth
9
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
The term user can be either human or a
software module
10
Static vs. Dynamic Structures

Static structures



Shape and size of the structure do not change
over time
Easier to manage
Dynamic structures


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
11
Pointers


A memory cell (or perhaps a block of
memory cells) that contains the address of
another memory cell
Examples



Many programming languages include
pointers as a primitive data type


Program counter as an instruction pointer
URL
Allow the declaration, allocation, and
manipulation of pointers
Used to create dynamic data structures
12
Use of Pointers
13
Stack and Heap Space
Heap
Storage
Stack
Storage
14
Homogeneous Arrays
15
Two-Dimensional Array
16
Storage of a 2-D 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
17
Mini Review

Show how the array below would be
arranged in main memory when stored
in row major order
5
3
7
4
2
8
1
9
6
18
Answer

5 3 7 4 2 8 1 9 6
19
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?
20
Answers
x  r  ( j  1)  (i  1)
ci  j
21
Storing lists


Contiguous list = list stored in a
homogeneous array
Linked list = list in which each node
points to the next one


Head pointer = pointer to first entry in list
NIL pointer = non-pointer value used to
indicate end of list
22
Contiguous Lists
23
Contiguous List


Convenient storage structure when
implementing static lists
Inconvenient in dynamic cases

Delete a name


Move entries to keep the list in the same order
Add a name

Move the entire list to obtain an available block of
contiguous cells large enough for the expanded list
24
Linked List
25
Deleting an Entry
26
Inserting an Entry
27
A Stack in Memory
28
Operations on Queues
29
Circular Queues (1)
30
Circular Queues (2)
31
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
32
Answer
H
A
H
T
B
C
H
B
T
T
E
E
F
C
H
C
T
T
C
H
H
T
C
D
E
T
H
H
D
E
D
D
T
F
33
Storing a binary tree

Linked structure



Each node = data cell + two child pointers
Accessed through a pointer to root node
Mapped to a contiguous array




A[1] = root node
A[2],A[3] = children of A[1]
A[4],A[5],A[6],A[7] = children of A[2] and A[3]
…
34
Binary Tree Node
35
Linked Storage System
36
Storage without Pointers
37
Inefficient Storage
38
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
39
Answer
Y
Z
X
Y
X
NIL
Z
W
NILNIL
NILNIL
W
40
Manipulating data structures

Ideally, a data structure should be
manipulated solely by pre-defined
procedures.


Example: A stack typically needs at least
push and pop procedures.
The data structure along with these
procedures constitutes a complete abstract
tool.
41
Printing a Linked List
42
Binary Tree Package

Search for the presence of an entry



Use the binary search algorithm
Print the list in order
Insert a new entry
43
Ordered Tree
44
Search the Binary Tree
45
Search Binary Tree
46
Printing a Binary Tree
47
Mini Review

Draw a binary tree to store the list R, S, T,
U, V, W, X, Y, and Z for future searching
48
Answer
V
Y
T
S
R
X
U
Z
W
49
Printing a Tree in Order
50
Traversing a Binary Tree

Inorder traversal


Preorder traversal


Left – Root – Right
Root – Left – Right
Postorder traversal

Left – Right – Root
51
Inserting an Entry (1)
52
Inserting an Entry (2)
53
Inserting an Entry
54
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
55
Customized data types



User-defined data type = template for a
heterogeneous structure
Abstract data type = user-defined data type
with methods for access and manipulation
Class = abstract data type with extra features



Characteristics can be inherited
Contents can be encapsulated
Constructor methods to initialize new objects
56
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;
57
Declaration of Nodes of a Binary
Tree
typedef struct {
char data;
Node* left;
Node* right;
} Node;
Node* root;
58
C++ Class
59
Using Classes



StackOfIntegers StackOne(50);
StackOne.push(106);
int OldValue = StackOne.pop();
60
Standard Template Library


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
61
Pointers in Machine Language


Machine language defined in Appendix C
Load data (immediate addressing)


Load address (direct addressing)


1RXY
Load through pointer (indirect addressing)



2RXY
DRXY
DR0S
Save through pointer

XY
S
ER0S
R
62
Expanding the Machine Language
to Take Advantage of Pointers
63
Loading a Register from a Memory
Cell Located by a Pointer in a
Register
64
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:



15A5
25CA
D508
65
Answers



CA
CA
CA
66
Exercise

Review problems
2, 5, 8, 14, 16, 19, 24, 26, 30, 44
67
Download