670 likes | 796 Views
Data Abstractions. Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University. Data Structures. Conceptual organization of data Basic data structures Homogeneous array List Stack Queue Tree. List.
E N D
Data Abstractions Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University
Data Structures • Conceptual organization of data • Basic data structures • Homogeneous array • List • Stack • Queue • Tree
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
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
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)
Trees • Nodes • Root • Terminal (leaf) • Parent, children, siblings • Subtrees • Depth
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
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
Pointers • A memory cell (or perhaps a block of memory cells) that contains the address of another memory cell • Examples • Program counter as an instruction pointer • URL • Many programming languages include pointers as a primitive data type • Allow the declaration, allocation, and manipulation of pointers • Used to create dynamic data structures
Stack and Heap Space Heap Storage Stack Storage
Storage of a 2-D Array • 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 Address polynomial
Mini Review • Show how the array below would be arranged in main memory when stored in row major order
Answer • 5 3 7 4 2 8 1 9 6
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 • 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?
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
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
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
H T H T B C A B C H T T H H T H T C C D E C D E D T H H T E F D E F Answer
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] • …
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 z x w
Y X Z W Answer Y Z NIL NIL X NIL W NIL NIL
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.
Binary Tree Package • Search for the presence of an entry • Use the binary search algorithm • Print the list in order • Insert a new entry
Mini Review • Draw a binary tree to store the list R, S, T, U, V, W, X, Y, and Z for future searching
Answer V Y T X Z U S W R