1 / 56

Chapter 8

Chapter 8. Data Abstractions. Chapter 8: Data Abstractions. 8.1 Data Structure Fundamentals 8.2 Implementing Data Structures 8.3 A Short Case Study 8.4 Customized Data Types 8.5 Classes and Objects 8.6 Pointers in Machine Language. Basic Data Structures. Homogeneous array

chandler
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 Data Abstractions

  2. Chapter 8: Data Abstractions • 8.1 Data Structure Fundamentals • 8.2 Implementing Data Structures • 8.3 A Short Case Study • 8.4 Customized Data Types • 8.5 Classes and Objects • 8.6 Pointers in Machine Language

  3. Basic Data Structures • Homogeneous array • Heterogeneous array • List • Stack • Queue • Tree

  4. Figure 8.1 Lists, stacks, and queues

  5. Terminology for lists • List = collection of data whose entries are arranged sequentially • Head = beginning of the list • Tail = end of the list

  6. Terminology for stacks • Stack = a list in which entries are removed and inserted only at the head • LIFO = last-in-first-out • Top = head of list • Bottom or base = tail of list • Pop = remove entry from the top • Push = insert entry at the top

  7. Terminology for queues • Queue = a list in which entries are removed at the head and are inserted at the tail • FIFO = first-in-first-out

  8. Terminology for a tree • Tree = a collection of data whose entries have a hierarchical organization • Node = an entry in a tree • Binary tree = tree in which every node has at most two children • Depth = number of nodes in longest path from root to leaf

  9. Terminology for a tree (continued) • Root node = the node at the top • Terminal or leaf node = a node at the bottom • Parent of a node = the node immediately above the specified node • Child of a node = a node immediately below the specified node • Ancestor = parent, parent of parent, etc. • Descendent = child, child of child, etc. • Siblings = nodes sharing a common parent

  10. Figure 8.1 An example of an organization chart

  11. Figure 8.2 Tree terminology

  12. Basic data structures • Homogeneous array • is an array of entries of the same type. • List The unique feature of arrays is that their size and shape are constant.􀂄 • For the group with dynamic population (members join/leave), lists structure employs the concept of a pointer to accommodate such variations on membership.

  13. Nature of data structures • Static: size of data structure does not change • Dynamic: size of data structure can change • Pointer:Main memory location of an data item A is identified by its numeric address, which value can be saved in another memory location P.􀂄 • Such P is called a pointer to the data item A.􀂄 • The program counter is indeed the instruction pointer.􀂄 • URLs(Uniform resource Locator) used to link hypertext contents are also like the pointers.

  14. Figure 8.3 Novels arranged by title but linked according to authorship

  15. 1.Give examples of each of the following structures in daily life: List Stack Queue Tree 2.Suppose a tree has four nodes A,B,C,and D. If A and C are siblings and D’s parent is A, which nodes are leaf Nodes? Which is root node?

  16. Storing homogeneous arrays • Requires enough contiguous memory to hold all data • Int readings [24]; reading[4] <- 67; • Two dimensional arrays • Row-major order: each row is stored together • Address polynomial: A[r,c] = (r-1)(# columns) + (c-1) • Column-major order: each column is stored together

  17. Figure 8.4 The array of temperature readings stored in memory starting at address x

  18. Figure 8.5 A two-dimensional array with four rows and five columns stored inrow major order

  19. Figure 8.7 Storing the heterogeneous array Employee

  20. 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

  21. Figure 8.6 Names stored in memory as a contiguous list

  22. Figure 8.7 The structure of a linked list

  23. Figure 8.8 Deleting an entry from a linked list

  24. Figure 8.9 Inserting an entry into a linked list

  25. Storing stacks and queues • Can use same mechanisms as for lists • Circular queue = homogeneous array where the first array entry is treated as immediately following the last array entry • Prevents a queue from crawling out of its allotted storage space

  26. Figure 8.10 A stack in memory

  27. Figure 8.11 A queue implementation with head and tail pointers. Note how the queuecrawls through memory as entries are inserted and removed.

  28. Problems with Pointers Pointers in Queue may grow out of the region of the specified memory region In Java to advance pointer redirect Next to the next list entry In C Assign Next the value Next +1 What is the problem in C?

  29. 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] • …

  30. Figure 8.12 The structure of a node in a binary tree

  31. Figure 8.13 The conceptual and actual organization of a binary tree using a linked storagesystem

  32. Figure 8.14 A tree stored without pointers

  33. Figure 8.15 A sparse, unbalanced tree shown in its conceptual form and as it would bestored without pointers

  34. 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.

  35. Figure 8.16 A procedure for printing a linked list

  36. What condition indicates a linked list is empty? What about Stack and Queue?

  37. A Case Study • Store the letters in a binary tree • The middle of the entry the root node • The middle of the remaining first half of the list • the root’s left child • 3. The middle of the remaining second half of the list • the root’s right child • A,B,C,D,E,F,G,H,I,J,K,L,M • Then 3 operations to be performed • Search for the presence of an entry • Print the list in alphabetical order • Insert a new entry

  38. Figure 8.17 The letters A through M arranged in an ordered tree

  39. Figure 8.18 The binary search as it would appear if the list were implemented as a linkedbinary tree

  40. Figure 8.19 The successively smaller trees considered by the procedure in Figure 8.18when searching for the letter J

  41. Figure 8.20 Printing a search tree in alphabetical order

  42. Figure 8.21 A procedure for printing the data in a binary tree

  43. Figure 8.22 Inserting the entry M into the list B, E, G, H, J, K, N, P stored as a tree

  44. Figure 8.23 A procedure for inserting a new entry in a list stored as a binary tree

  45. Non-recursive way for serach procedure BinarySearch (Tree, TargetValue) CurrentPointer ¬root pointer of Tree; *The node pointed to by CurrentPointer will be called the current node* Found ¬false; while (Found is false and CurrentPointer is not NIL) do (Perform the activity associated with the appropriate case below:

  46. Non-recursive way for serach case 1, TargetValue = current node: (Found ¬true) case 2, TargetValue < current node: (CurrentPointer ¬current node's left child pointer) case 3, TargetValue > current node: (CurrentPointer ¬current node's right child pointer) ) if (Found = false) then (declare the search a failure) else (declare the search a success)

  47. Customized data types • User-defined data type = template for a heterogeneous structure define type EmployeeType to be { char Name[8]; Int Age ; real SkillRating; } EmployeeType DistManager; DistManager.name=John; DistManager.age=30;

  48. Customized data types • Abstract data type = user-defined data type with methods for access and manipulation define type StackType to be {int StackEntries[20]; int StackPointer = 0; Procedure push(value) {StackEnties[StackPointer]=value; Stackpointer++;} Procedure pop….. } StackType Stackone; Stackone.push(25);

  49. Customized data types • What should be considered when building an abstract data type for a list. • 1 insert, delete, find the entry • 2. Ways of implementing a list— • Contiguous or linked list • 3. The choice of either way is invisible to users

  50. Customized data types • Class and Objects Class = abstract data type with extra features • Characteristics can be inherited • Contents can be encapsulated • Constructor methods to initialize new objects

More Related