Chapter 8: Data Abstractions
Computer Science: An Overview Tenth Edition
by J. Glenn Brookshear
Presentation files modified by Farn Wang
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-2 Basic Data Structures
• Homogeneous array • Heterogeneous array • List – Stack – Queue • Tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-3 Lists, stacks, and queues
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-4 Terminology for Lists
• List: A collection of data whose entries are arranged sequentially • Head: The beginning of the list • Tail: The end of the list
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-5 Terminology for Stacks
Stack A list in which entries are removed and inserted only at the head • LIFO: Last-in-first-out • Top: The head of list (stack) • Bottom or base: The tail of list (stack) • Pop: To remove the entry at the top • Push: To insert an entry at the top
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-6 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-7 Tree - An example of an organization chart
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-8 Terminology for a 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-9 Terminology for a Tree (continued)
• Parent: The node immediately above a specified node • Child: A node immediately below a specified node • Ancestor: Parent, parent of parent, etc. • Descendent: Child, child of child, etc. • Siblings: Nodes sharing a common parent
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-10 Terminology for a Tree (continued)
• Binary tree: A tree in which every node has at most two children • Depth: The number of nodes in longest path from root to leaf
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-11 Tree terminology
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-12 Additional Concepts
• Static Data Structures: Size and shape of data structure does not change • Dynamic Data Structures: Size and shape of data structure can change • Pointers: – Used to locate data – Used for constructing dynamic structures
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-13 Dynamic linked lists - example
Novel records arranged by title but linked according to authorship
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-14 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-15 The array of temperature readings stored in memory starting at address x
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-16 A two-dimensional array with four rows and five columns stored in row major order
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-17 Storing the heterogeneous array Employee
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-18 Storing Lists
• Contiguous list: List stored in a homogeneous array • 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-19 Names stored in memory as a contiguous list
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-20 The structure of a linked list
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-21 Deleting an entry from a linked list
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-22 Inserting an entry into a linked list
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-23 Storing Stacks and Queues
• Stacks usually stored as contiguous 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
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-24 A stack in memory
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-25 A queue implementation with head and tail pointers
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-26 A circular queue containing the letters P through V
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-27 Storing Binary Trees
• Linked structure – Each node = data cells + two child pointers – Accessed via a pointer to root node • Contiguous array structure – 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]
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-28 The structure of a node in a binary tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-29 The conceptual and actual organization of a binary tree using a linked storage system
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-30 A tree stored without pointers
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-31 A sparse, unbalanced tree shown in its conceptual form and as it would be stored without pointers
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-32 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-33 A procedure for printing a linked list
procedure print_list(list) { c = list; while (c is not NIL) do { print the name pointed to by c; let c by the next element to c in the list; } }
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-34 Case Study
Problem: Construct an abstract tool consisting of a list of names in alphabetical order along with the operations search, print, and insert.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-35 The letters A through M arranged in an ordered tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-36 The binary search as it would appear if the list were implemented as a linked binary tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-37 The successively smaller trees considered by the procedure in Figure 8.18 when searching for the letter J
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-38 Printing a search tree in alphabetical order
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-39 A procedure for printing the data in a binary tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-40 Inserting the entry M into the list B, E, G, H, J, K, N, P stored as a tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-41 A procedure for inserting a new entry in a list stored as a binary tree
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-42 User-defined Data Type
• A template for a heterogeneous structure • Example: define type EmployeeType to be {char Name[25]; int Age; real SkillRating; }
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-43 Abstract Data Type
• A user-defined data type with procedures for access and manipulation • Example: define type StackType to be {int StackEntries[20]; int top = 0; procedure push(value){ StackEntries[top] ← value; top = top + 1; } procedure pop . . . }
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-44 Class
• An abstract data type with extra features – Characteristics can be inherited – Contents can be encapsulated – Constructor methods to initialize new objects
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-45 A stack of integers implemented in Java and C#
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-46 Pointers in Machine Language
• Immediate addressing: Instruction contains the data to be accessed • Direct addressing: Instruction contains the address of the data to be accessed • Indirect addressing: Instruction contains the location of the address of the data to be accessed
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-47 Our first attempt at expanding the machine language in Appendix C to take advantage of pointers
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-48 Loading a register from a memory cell that is located by means of a pointer stored in a register
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-49 Indirect vs. direct addressing
• Direct addressing
PC PC 9C00 AB02
BR AB02 BR AB02
35AD AB02 35AD
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-50 Indirect vs. direct addressing
• Indirect addressing 35AD
PC PC …….. 9C00 35AD
BR *AB02 BR AB02
35AD AB02 35AD
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-51 Beyond the basics
Tree applications • heirarchical network topology of network • cluster computing • circuit signal propagations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-52 Beyond the basics
Trees • depth log(# of nodes in the tree) • efficient membership checking • Questions ? Can we maintain the log depths with dynamic node insertion and deletion ?
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-53 Beyond the basics
Trees Maintaining the log depths with dynamic node insertion and deletion ? • trees implemented with pointers: • solutions – 2-3 trees – red-black trees – splay trees
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-54 Beyond the basics
Graphs • undirected
• directed
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-55 Beyond the basics
Graph problems • Find a path (with a cost ?) between two nodes. • Find a cycle in a graph. • Find the biconnected components • Check if a graph is planar. • ……
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-56 Beyond the basics
DAG (directed acyclic graphs) • Directed graphs without cycles • Can be used for Boolean formula presentation.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8-57 Truth Table, DNF, and CNF
x1 x2 x3 F • Disjunctive normal form 0 0 0 0 (DNF) 0 0 1 0 F = (x x x ) (x x x ) 0 1 0 0 1 2 3 1 2 3 (x x x ) 0 1 1 1 1 2 3 1 0 0 0 • Conjunctive normal form 1 0 1 1 (CNF) 1 1 0 0 F = (x1 x2 x3) (x1 x2x3) 1 1 1 1 (x1 x2 x3) (x1x2x3)
(x1 x2 x3)
58 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Decision Tree (BDT) - predecessor of BDD with redundancies
• Same size as truth tables • Lots of redundancy: Out of 8 subtrees rooted at b2 only 3 are distinct!!! • Merge isomorphic subtrees BDD
59 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Truth Table and Decision Tree
x1 0 0 0 0 1 1 1 1
x2 0 0 1 1 0 0 1 1
x3 0 1 0 1 0 1 0 1 x1 0 1 F 0 0 0 1 0 1 0 1
x2 x2 0 1 0 1
x3 x3 x3 x3 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1
60 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Structure sharing in BDT function values (a1b1a2b2) (a1 b1 a2 b2) determined at (a1 b1 a2 b2) b1 (a1 b1 a2 b2)
61 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Binary Decision Diagram (BDD) unique BDD for a function • BDD: A minimal canonical form
minimumrepresentation size for Boolean formulas. representation• Motivation: (for a given var ordering)– Too much space redundancy in traditional representations – BDD is more compact than truth tables, conjunctive normal form, disjunctive normal form, binary decision trees, etc. – BDD has a canonical form
– BDD operations are efficient 62 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BDD (Binary Decision Diagram)
minimal canonical form of Boolean functions • minimal efficiency (space & computation) – for a specific variable ordering x 0 • Canonical form: 1 unique BDD for a function y 0 z 1 0 1 (x y) z 0 0 1 0 1
63 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BDD function evluation
Using path traversal to evaluate function
x 1 (x y) z x x 0 x 0 0 1 y 0 1 0 y y 0 y 0 z z z 0 1 1 1 z 0 1 0 1
0 1 0 1
64 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BDD Reduction (I) - from bottom up
at leaf level x1 x1 0 1 0 1 x x 2 x2 2 x2 1 0 1 1 0 1 0 0 x3 x3 x3 1 0 x3 0 1 0 0 1 1
0 1 0 1 0 1
65 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BDD Reduction (II) - from bottom up
at level x3 x1 x1 0 1 0 1 x x 2 x2 2 1 0 x2 1 0 1 1 0 0 x3 x3 x3 0 1 0 0 1 1
0 1 0 1
66 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley BDD Reduction (III) - from bottom up
at level x2 x1 x1 0 0 1 1 x x 2 1 0 x2 2 1 1 0 0 x3 x3 0 1 0 1
0 1 0 1
67 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relations for Logic Blocks: Example
x1 x2 x3 y1 y2 0 0 0 0 0 x 1 y1 0 0 1 0 1 0 1 0 0 1 x2 0 1 1 0 1 y2 x3 1 0 0 0 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1
68 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example (continued)
x1 x2 x3 y1 y2 F 0 0 0 0 0 1 x1
0 0 1 0 1 1 x2 0 1 0 0 1 1 x 0 1 1 0 1 1 3 1 0 0 0 0 1 y1 1 0 1 0 1 1 y 1 1 0 1 1 1 2
1 1 1 1 1 1 0 1 other 0
69 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Relations for FSMs: Example
Ins CS CSC NS NSC 0 A 00 B 10 C 0,1 A 00 A 00 1 0 B 10 B 10 0 1 B 10 A 00 0 B 0 C 01 B 10 A 1 C 01 A 00 1 0 0,1
70 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Example (continued)
Relation i = ia1a2b1b2 a1 + a1a2b1b2 b1 + ia1a2b1b2 a + ia1a2b1b2 2
+ ia1a2b1b2 b2 0 1 + ia1a2b1b2
71 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Efficient Operations on BDDs
• Apply – NOT, AND, OR, EXOR, etc. • Quantification (existential, universal) • Replace • Compose • Specialized operators – Generalized cofactor (constrain, restrict) – Compatible projection, etc.
72 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Components of BDDs and Their Use
• Nodes (represent functions; complexity) • Terminal nodes (constant functions) • Edges (relationship between functions) • Paths (true and false var. assignments) • Cuts (variable partitions and subsets)
73 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Properties of BDDs
• Canonicity • Compactness (with some exceptions) • Representing a variety of discrete objects – Boolean functions – Compositional sets – Encodings and labelings • Facilitating symbolic methods – Two-level minimization – State traversal of FSMs – …
74 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley