1 / 67

Chapter 12

Chapter 12. Abstract Data Type. O BJECTIVES. Understand the concept of a linear list as well as its operations and applications. 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.

burrd
Download Presentation

Chapter 12

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 12 AbstractData Type

  2. OBJECTIVES Understand the concept of a linear list as well as its operations and applications. 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. Understand the concept of an abstract data type (ADT). After reading this chapter, the reader should be able to:

  3. 12.1 BACKGROUND

  4. Abstract Data Type (ADT) • An Abstract Data Type (ADT)is a data structure and a collection of functions which operate on the data structure. • Stack – The operations new(),push(v, S),top(S), and pop(S) may be defined with axiomatic semantics as following. • new() returns a stack • pop(push(v, S)) = S • top(push(v, S)) = v • where S is a stack and v is a value.

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

  6. Queue problem • Queue simulation enqueuing dequeuing queue

  7. Note: • Abstract Data Type • Declaration of data • Declaration of operations • Encapsulation of data and operations

  8. Stack problem • Stack – The operations new(),push(S, v),top(S), and popoff(S) may be defined with axiomatic semantics as following. • new() returns a stack • popoff(push(S, v)) = S • top(push(S, v)) = v where S is a stack and v is a value. • The pop operation is a combination of top, to return the top value, and popoff, to remove the top value.

  9. Model for ADT Figure 12-1

  10. 12.2 LINEAR LISTS

  11. Linear list Figure 12-2 • Linear Listis a list in which each element has a unique successor.

  12. Figure 12-3 Categories of linear lists • Data can be inserted and deletedanywhere. • Data can only be inserted and deleted at the ends of the structure.

  13. Insertion in a general linear list with ordered data Figure 12-4

  14. Figure 12-5 Deletion from a linear list

  15. Figure 12-6 Retrieval from a linear list

  16. Traversal of a linear list Figure 12-7 • Traversalis an operation in which all elements in the list are processed sequentially, one by one.

  17. Implementation • Two common methods: • Array • Linked list

  18. 12.3 STACKS

  19. Three representations of a stack Figure 12-8 • Stacks • A restricted linear list in which all additions and deletions are made at one end, called the top. • A LIFO (Last In, First Out) data structure.

  20. Push operation in a stack Figure 12-9

  21. Pop operation in a stack Figure 12-10

  22. 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. Implementation • Two common methods: • Array • Linked list (more common) • Applications • Reversing data • 1,2,3,4 → 4,3,2,1 • Parsing • Unmatched parentheses in an algebraic expression • Postponing data • Postfix: ab+c* → (a+b)*c, abc+* → a*(b+c) • Backtracking steps • Computer gaming

  24. 12.4 QUEUES

  25. Queue representation Figure 12-12 • Queues • A restricted linear list in which data can only be inserted at one end (rear) and deleted from the other end (front). • A FIFO (First In, First Out) data structure.

  26. Enqueue operation Figure 12-13

  27. Dequeue operation Figure 12-14

  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. Implementation • Two common methods: • Array • Linked list (more common) • Applicationsfound in virtually every OS and network and in countless other areas.

  30. 12.5 TREES

  31. Trees • A Tree consists of • a finite set of elements called Nodes • a finite set of directed lines, called Branches, that connect the nodes • Degree of a node – number of branches associated with the node. • Indegree branch • Outdegree branch • Root – • The first node of a tree • Indegree : 0 • Other nodes – • Indegree : 1 • All nodes – • outdegree : 0, 1 or more.

  32. Figure 12-16 Representation of a tree

  33. Trees • Leaf – • Outdegree : 0 • Internal nodes – a node that is not a root or a leaf. • Parent – • A node with successors • outdegree : > 0 • Child – • A node with a predecessor • indegree : 1 • Siblings – two or more nodes with the same parent. • Ancestor – any node in the pathfrom the root to the node. • Descendant – any node in the pathfrom the node to a leaf.

  34. Trees • Path – a sequence of nodes in which each node is adjacent to the next one. • Level of a node – its distance from the root. • Root : 0 • Height (Depth) of the tree – the level of the leaf in the longest path from the root plus 1. • Subtree – any connected structure below the root.

  35. Figure 12-17 Tree terminology Height : 3

  36. Figure 12-18 Subtrees

  37. 12.6 BINARY TREES

  38. Binary Trees • Binary tree – A tree in which no node can have more than two subtrees. • Null tree - A tree with no node. • Height of a Binary Tree – (with N nodes) • Hmax = N • Hmin = [log2N] +1 • # of nodes of a binary tree – (Given height H) • Nmin = H • Nmax = 2H - 1

  39. Binary tree Figure 12-19

  40. Examples of binary trees Figure 12-20

  41. Balanced Trees • The distance of a node from the root determines how efficiently it can be located. • Balance Factor of a Binary Tree – is the difference in height between its left and right subtrees.B = HL - HR • A binary tree is balancedif the height of its subtrees differs by no more than 1 (B = -1, 0, or +1) and its subtrees are also balanced.

  42. A B C D E F

  43. Binary Tree Traversal • Binary Tree Traversal each node of the tree be processed once and only once in a predetermined sequence. • Depth-First Traversal – • Preorder Traversal (NLR) – • Inorder Traversal (LNR) – • Postorder Traversal (LRN) – • Need a stack • Breadth-First Traversal – • process all of the children of a node before proceeding with the next level. • Need a queue

  44. Depth-first traversal of a binary tree Figure 12-21 • NLR LNR LRN

  45. Preorder traversal of a binary tree Figure 12-22 • A, B, C, D, E, F

  46. Inorder traversal of a binary tree Figure 12-23 • C, B, D, A, E, F

  47. Postorder traversal of a binary tree Figure 12-24 • C, D, B, F, E, A

  48. Figure 12-25 Breadth-first traversal of a binary tree

  49. Implementation • A binary tree is normally implemented as a linked list. • Applications • Expression tree– a binary tree with the following properties: • Each leaf is an operand • The root and internal nodes are operators. • Subtrees are subexpressions, with the root being an operator. • Three different expression formats: • Infix expression • Postfix expression – produced by Postorder traversal • Prefix expression – produced by Preorder traversal

  50. Expression tree Figure 12-26 • Infix expression: • Prefix expression: + * a + b c d • Postfix expression: a b c + * d +

More Related