Download presentation

Presentation is loading. Please wait.

Published byFelix Franklin Modified over 2 years ago

1
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type

2
©Brooks/Cole, 2003 Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations and applications. After reading this chapter, the reader should be able to: O BJECTIVES Understand the concept of a stack as well as its operations and applications. Understand the concept of a queue as well as its operations and applications. Understand the concept of a tree as well as its operations and applications. Understand the concept of a graph as well as its operations and applications.

3
©Brooks/Cole, 2003 BACKGROUNDBACKGROUND 12.1

4
The concept of abstraction means: 1. You know what a data type can do. 2. How it is done is hidden. Note:

5
©Brooks/Cole, 2003 Abstract Data Type 1.Declaration of data 2.Declaration of operations 3.Encapsulation of data and operations 3.Encapsulation ( 膠囊化 ) of data and operations Note:

6
©Brooks/Cole, 2003 Figure 12-1 Model for ADT

7
©Brooks/Cole, 2003 LINEARLISTSLINEARLISTS 12.2

8
Figure 12-2 Linear list A linear list is a list in which each element has a unique successor. A linear list is a list in which each element has a unique successor. Linear lists can be divided into two categories: general and restricted Linear lists can be divided into two categories: general and restricted General list: random list and ordered list General list: random list and ordered list Restricted list: queue and stack Restricted list: queue and stack

9
©Brooks/Cole, 2003 Figure 12-3 Categories of linear lists

10
©Brooks/Cole, 2003 Operations on linear lists Insertion: Fig. 12.4 Insertion: Fig. 12.4 Most of the time, data are inserted somewhere in the middle of the list. Most of the time, data are inserted somewhere in the middle of the list. Deletion: Fig. 12.5 Deletion: Fig. 12.5 Deletion from a general list requires that the list be searched to located the data being deleted. Deletion from a general list requires that the list be searched to located the data being deleted. Retrieval: Fig. 12.6 Retrieval: Fig. 12.6 A copy of the data should be retrieved without changing the contents of the list. A copy of the data should be retrieved without changing the contents of the list. Traversal: Fig. 12.7 Traversal: Fig. 12.7 List traversal is an operation in which all elements in the list are processed sequentially, one by one. List traversal is an operation in which all elements in the list are processed sequentially, one by one.

11
©Brooks/Cole, 2003 Figure 12-4 Insertion in a linear list

12
©Brooks/Cole, 2003 Figure 12-5 Deletion from a linear list

13
©Brooks/Cole, 2003 Figure 12-6 Retrieval from a linear list

14
©Brooks/Cole, 2003 Figure 12-7 Traversal of a linear list

15
©Brooks/Cole, 2003 Implementation Implementation of a general linear list: Implementation of a general linear list: Array Array Linked list Linked list

16
©Brooks/Cole, 2003 STACKSSTACKS 12.3

17
Figure 12-8 Three representations of a stack

18
©Brooks/Cole, 2003 Operations on stacks Push: Fig. 12.9 Push: Fig. 12.9 Push adds an item at the top of the stack. Push adds an item at the top of the stack. If there is not enough room, the stack is in an overflow state and the item cannot be added. If there is not enough room, the stack is in an overflow state and the item cannot be added. Pop: Fig. 12.10 Pop: Fig. 12.10 When you pop a stack, you remove the item at the top of the stack and return it to the user. When you pop a stack, you remove the item at the top of the stack and return it to the user. If pop is called when the stack is empty, it is in an underflow state. If pop is called when the stack is empty, it is in an underflow state.

19
©Brooks/Cole, 2003 Figure 12-9 Push operation in a stack

20
©Brooks/Cole, 2003 Figure 12-10 Pop operation in a stack

21
©Brooks/Cole, 2003 Implementation and application Implementation of a stack: Implementation of a stack: Array Array Linked list Linked list Stack applications Stack applications Reversing data Reversing data Parsing Parsing Postponement ( 延期 ) Postponement ( 延期 ) Backtracking Backtracking

22
©Brooks/Cole, 2003 Example 1 Show the result of the following operations on a stack S. push (S, 10) push (S, 12) push (S, 8) if not empty (S), then pop (S) push (S, 2) Solution

23
©Brooks/Cole, 2003 QUEUESQUEUES 12.4

24
Figure 12-12 Queue representation

25
©Brooks/Cole, 2003 Operations on queues Enqueue: Fig. 12.13 Enqueue: Fig. 12.13 After the data have been inserted into the queue, the new element becomes the rear. After the data have been inserted into the queue, the new element becomes the rear. If there is not enough room for another element in the queue, the queue is in an overflow state. If there is not enough room for another element in the queue, the queue is in an overflow state. Dequeue: Fig. 12.14 Dequeue: Fig. 12.14 The data at the front of the queue are removed from the queue and returned to the user. The data at the front of the queue are removed from the queue and returned to the user. If there are no data in the queue when a dequeue is attempted, the queue is in an underflow state. If there are no data in the queue when a dequeue is attempted, the queue is in an underflow state.

26
©Brooks/Cole, 2003 Figure 12-13 Enqueue operation

27
©Brooks/Cole, 2003 Figure 12-14 Dequeue operation

28
Example 2 Show the result of the following operations on a queue Q. enqueue (Q, 23) if not empty (Q), dequeue (Q) enqueue (Q, 20) enqueue (Q, 19) if not empty (Q), dequeue (Q) Solution

29
©Brooks/Cole, 2003 Implementation and application Implementation of a queue: Implementation of a queue: Array Array Linked list Linked list Queue applications Queue applications Operating system Operating system Preserve the order of data … Preserve the order of data …

30
©Brooks/Cole, 2003 TREESTREES 12.5

31
Basic tree concepts A tree consists of a finite set of elements (nodes) and a finite set of directed lines (branches) that connect the nodes. A tree consists of a finite set of elements (nodes) and a finite set of directed lines (branches) that connect the nodes. Degree of a node: indegree and outdegree Degree of a node: indegree and outdegree Root, internal node, and leaf Root, internal node, and leaf Parent, child, and siblings Parent, child, and siblings Ancestor ( 祖先 ) and descendant ( 後裔 ) Ancestor ( 祖先 ) and descendant ( 後裔 ) Path Path Level, height, and depth Level, height, and depth

32
©Brooks/Cole, 2003 Figure 12-16 Representation of a tree

33
©Brooks/Cole, 2003 Figure 12-17 Tree terminology

34
©Brooks/Cole, 2003 Figure 12-18 Subtrees

35
©Brooks/Cole, 2003 BINARYTREESBINARYTREES 12.6

36
Figure 12-19 Binary tree A binary tree is a tree in which no node can have more than two subtrees. A binary tree is a tree in which no node can have more than two subtrees. A null tree is a tree with no nodes. A null tree is a tree with no nodes.

37
©Brooks/Cole, 2003 Figure 12-20 Examples of binary trees

38
©Brooks/Cole, 2003 Properties of binary trees Height of binary tree: Height of binary tree: N : the number of nodes in a binary tree N : the number of nodes in a binary tree H max = N H max = N H min = [log 2 N ] + 1 H min = [log 2 N ] + 1 H : the height of a binary tree H : the height of a binary tree N min = H N min = H N max = 2 H – 1 N max = 2 H – 1

39
©Brooks/Cole, 2003 Properties of binary trees Balance factor : B Balance factor : B The balance factor of a binary tree is the difference in height between its left and right subtrees. The balance factor of a binary tree is the difference in height between its left and right subtrees. B = H L -H R B = H L -H R A binary tree is balanced if the height of its subtrees differs by no more than 1 (AVL tree) A binary tree is balanced if the height of its subtrees differs by no more than 1 (AVL tree)

40
©Brooks/Cole, 2003 Operations on binary trees Binary tree traversals Binary tree traversals A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. Depth-first traversals Depth-first traversals Preorder, inorder, and postorder traversals Preorder, inorder, and postorder traversals Breadth-first traversals Breadth-first traversals

41
©Brooks/Cole, 2003 Figure 12-21 Depth-first traversal of a binary tree

42
©Brooks/Cole, 2003 Figure 12-22 Preorder traversal of a binary tree A B C D E F

43
©Brooks/Cole, 2003 Figure 12-23 Inorder traversal of a binary tree C B D A E F

44
©Brooks/Cole, 2003 Figure 12-24 Postorder traversal of a binary tree C D B F E A

45
©Brooks/Cole, 2003 Figure 12-25 Breadth-first traversal of a binary tree

46
©Brooks/Cole, 2003 Implementation and application Implementation of a binary tree: linked list Implementation of a binary tree: linked list Application Application Expression tree Expression tree Each leaf is an operand Each leaf is an operand The root and internal nodes are operators The root and internal nodes are operators Subtrees are subexpressions, with the root being an operator Subtrees are subexpressions, with the root being an operator

47
©Brooks/Cole, 2003 Figure 12-26 Expression tree Infix: a*(b+c)+d Prefix: +*a+bcd Postfix: abc+*d+

48
©Brooks/Cole, 2003 GRAPHSGRAPHS 12.7

49
Figure 12-27 Directed and undirected graphs

50
©Brooks/Cole, 2003 Graphs A graph is a collection of nodes (vertices) and a collection of line segments (lines) connecting pairs of vertices. A graph is a collection of nodes (vertices) and a collection of line segments (lines) connecting pairs of vertices. Directed graph (digraph): line arc Directed graph (digraph): line arc Undirected graph: line edge Undirected graph: line edge Path, cycle, and loop Path, cycle, and loop A path is a sequence of vertices in which each vertex is adjacent to the next one. A path is a sequence of vertices in which each vertex is adjacent to the next one. A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. A cycle is a path consisting of at least three vertices that starts and ends with the same vertex. A loop is a special case of a cycle in which a single arc begins and ends at the same vertex. A loop is a special case of a cycle in which a single arc begins and ends at the same vertex.

51
©Brooks/Cole, 2003 Graphs Strongly connected and weakly connected Strongly connected and weakly connected A directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph. A directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph. A directed graph is weakly connected if at least two vertices are not connected. A directed graph is weakly connected if at least two vertices are not connected. A disjoint graph is not connected. A disjoint graph is not connected. Degree, outdegree, and indegree of a vertex Degree, outdegree, and indegree of a vertex

52
©Brooks/Cole, 2003 Operations on graphs Add vertex Add vertex Delete vertex Delete vertex Add edge Add edge Delete edge Delete edge Find vertex Find vertex Traverse graph Traverse graph Depth-first traversal Depth-first traversal Breadth-first traversal Breadth-first traversal

53
Figure 12-28 Add vertex Delete vertex

54
©Brooks/Cole, 2003 Figure 12-30 Add edge Delete edge

55
©Brooks/Cole, 2003 Figure 12-32 Find vertex

56
©Brooks/Cole, 2003 Figure 12-33 Depth-first traversal of a graph A X G H E M J Y P 3 5 8 6 7 4 9

57
©Brooks/Cole, 2003 Figure 12-34 Breadth-first traversal of a graph A X G H P E M Y J

58
©Brooks/Cole, 2003 Implementation and application Implementation of a graph: Implementation of a graph: Array (Fig. 12.35) Array (Fig. 12.35) Linked list (Fig. 12.35) Linked list (Fig. 12.35) Graph applications Graph applications Networks: weighted graph Networks: weighted graph Minimum spanning tree Minimum spanning tree A spanning tree is a tree that contains all of the vertices in the graph. A spanning tree is a tree that contains all of the vertices in the graph. A minimum spanning tree is a spanning tree such that the sum of its weights is the minimum. A minimum spanning tree is a spanning tree such that the sum of its weights is the minimum.

59
©Brooks/Cole, 2003 Figure 12-35: Part I Graph implementations

60
©Brooks/Cole, 2003 Figure 12-35: Part 2 Graph implementations

61
©Brooks/Cole, 2003 Key terms Abstract data type Abstract data type Abstraction Abstraction Ancestor Ancestor Arc Arc Array Array Backtracking Backtracking Balance factor Balance factor Binary tree Binary tree Binary tree traversal Binary tree traversal Branch Branch Breadth-first traversal Breadth-first traversal Child Child Cycle Cycle Degree Depth Depth-first traversal Dequeue Descendant Digraph Directed graph Disjoint graph Edge Enqueue Expression tree FIFO General list

62
©Brooks/Cole, 2003 Graph Graph Height Height Indegree Indegree Infix Infix Inorder traversal Inorder traversal Internal node Internal node LIFO LIFO Leaf Leaf Level Level Line Line Linear list Linear list Loop Loop Minimum spanning tree Minimum spanning tree Node null tree Node null tree Root Siblings Spanning tree Stack Strongly connected graph Subtree Token Traversal Tree Underflow Undirected graph Vertex Weakly connected graph Weighted graph Ordered list Ordered list Outdegree Outdegree Parent Parent Parsing Parsing Path Path Pop Pop Postfix Postfix Postorder traversal Postorder traversal Prefix Prefix Preorder traversal Preorder traversal Push Push Queue Queue Queue simulation Queue simulation Random list Random list Restricted list Restricted list Retrieval Retrieval

Similar presentations

OK

CS235102 Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline Introduction Representation Of Trees Binary Trees Binary Tree Traversals

CS235102 Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline Introduction Representation Of Trees Binary Trees Binary Tree Traversals

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on information system audit Ppt on adjectives for class 1 Ppt on power generation using footsteps prayer Ppt on engineering college life Ppt on elections in india downloads Ppt on leadership qualities of mahatma gandhi Ppt on geometrical shapes Ppt on us polo brand Run ppt on android Ppt on swine flu free download