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 Related Concepts – Static versus dynamic structures – Pointers 8.3 Implementing Data Structures
8-3 Data Structures A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently Basic Data Structures – Homogeneous array – Heterogeneous array – List Linked list Stack Queue – Tree
8-4 Basic Data Structures : Arrays Homogeneous array: The block of data items whose entries are of the same type – int array1[6]; // includes 6 integers – float * array2 =new float [6]; // includes 6 floating-point numbers Heterogeneous array: The block of data items that might be of different types. (The items within block are usually called components) – class Employee { public : int ID; string name;}; Employee array[8];
Basic Data Structures : Lists List : A collection whose entries are arranged sequentially. – The beginning of the list is head. – The other end of the list is tail. 8-5 datanextdatanextdatanext NULL head class Linked_list { int data; Linked_list*next; }; Linked_list * head; tail
Basic Data Structures : Stacks Stack : A list in which entries are inserted or removed only from head. – The head of the stack is called top of the stack – Bottom or base: The tail of list (stack) – Inserting a new entry is called pushing. – Deleting an entry is called popping – Last in first out (LIFO) : The last entry placed on a stack will always be the first entry to be removed 8-6 top
Basic Data Structures : Queue Queue : A list in which entries are inserted only at the tail and removed only from head. – First in first out (FIFO) : The first entry placed on a queue will always be the first entry to be removed 8-7
8-8 Figure 8.1 Lists, stacks, and queues
8-9 Figure 8.2 An example of an organization chart
8-10 Basic Data Structures : Tree Tree: A collection of data whose entries have a hierarchical organization – Node: An entry in a tree – Root node: The node at the top – Terminal or leaf node: A node at the bottom Root Leaf nodes
8-11 Basic Data Structures : Tree (cont.) – Parent: The node immediately above a specified node ( 3 is the parent of 1 and 6 ) – Child: A node immediately below a specified node ( 14 is a child of 10) – Ancestor: Parent, parent of parent, etc. (10 is ancestor of 14 and 13) – Descendent: Child, child of child, etc. (13 is a decendent of 14,10 and 7) – Siblings: Nodes sharing a common parent (4 and 7 are siblings)
8-12 Basic Data Structures : Tree (cont.) Binary tree: A tree in which every node has at most two children Depth (The depth of tree): The number of nodes in longest path from root to leaf Subtree:The node (except root node) with the nodes below it A subtree
Basic Data Structures : Tree (cont.) The size of a binary tree is the number of nodes in it –This tree has size 9 The depth of a node is its distance from the root –a is at depth zero –e is at depth 2 The depth of a binary tree is the depth of its deepest node –This tree has depth 3 a b c de f g hı
8-14 Figure 8.3 Tree terminology
8-15 Related Concepts Static Data Structures: Size and shape of data structure does not change (compile time) Dynamic Data Structures: Size and shape of data structure can change (run time) Pointers: Used to locate data
8-16 Storing Arrays Homogeneous arrays – Row-major order versus column major order – Address polynomial Heterogeneous arrays – Components can be stored one after the other in a contiguous block – Components can be stored in separate locations identified by pointers
8-17 Figure 8.5 The array of temperature readings stored in memory starting at address x (Homogenous Array)
8-18 Figure 8.6 A two-dimensional array with four rows and five columns stored in row major order
19 Storage mapping function : ROW MAJOR ORDER the mapping b/w pointer values and array indices EXAMPLE: int d [3]; d[i] *(&d[0]+i) *(d+i) EXAMPLE: int a [3] [4]; a[i][j] *(&a[0][0]+4*i+j) *(a+4*i+j) // address polynomial If indexing starts from 1 then x+ c*(i-1)+(j-1)
8-20 Figure 8.7 Storing the heterogeneous array Employee
8-21 Storing Lists Contiguous list: List stored in a homogeneous array (Example: int a [10]) Linked list: List in which each entries are linked by pointers – Head pointer: Pointer to first entry in list – NIL pointer: A “non-pointer” value used to indicate end of list
8-22 Figure 8.8 Names stored in memory as a contiguous list
8-23 Figure 8.9 The structure of a linked list
8-24 Figure 8.10 Deleting an entry from a linked list
8-25 Figure 8.11 Inserting an entry into a linked list
8-26 Figure 8.19 A procedure for printing a linked list
8-27 Exercise : Question 8 (pg.428) Linked list The table represents a position in memory. Each entry in the list consists of two cells: The first contains a letter of alphabet; the second contains a pointer to the next list entry. Alter the pointers so that the letter N is no longer in the list then replace the letter N with the letter G and alter the pointers so that the new letter appears int he list in its proper place in alphabetical order
8-28 Storing Stacks and Queues Stacks usually stored as contiguous lists (arrays) but also may be implemented using linked lists Queues usually stored as Circular Queues – Stored in a contiguous block in which the first entry is considered to follow the last entry – Prevents a queue from crawling out of its allotted storage space
8-29 Figure 8.12 A stack in memory
Exercise :STACKS x= pop(&n); push(11,&n); x= pop(&n); push(3,&n); push(2,&n); x= pop(&n);} main() { stack n; int x; reset(&n); push(4,&n); push(5,&n); push(3,&n); push(2,&n); push(1,&n);
8-31 Figure 8.13 A queue implementation with head and tail pointers // insert from tail //remove from head enqueue(A) enqueue(B) enqueue(C) X= dequeue() enqueue(D) Y= dequeue() enqueue(E)
8-32 Figure 8.14 A circular queue containing the letters P through V
8-33 Storing Binary Trees Linked structure – Each node = data cells + two child pointers – Accessed via a pointer to root node struct node { int data; struct node * left; struct node * right;}; Contiguous array structure (without pointers) – 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]
8-34 Figure 8.15 The structure of a node in a binary tree
8-35 Figure 8.16 The conceptual and actual organization of a binary tree using a linked storage system
8-36 Figure 8.17 A tree stored without pointers
8-37 Figure 8.18 A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers
8-38 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. A queue typically needs at least enqueue and dequeue procedures. – The data structure along with these procedures constitutes a complete abstract tool.