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

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
DATA STRUCTURE RECAP Yiqun Zhang University of Houston.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Abstract Data Types and Subprograms
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Advanced Data Structures
CS 171: Introduction to Computer Science II
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Transforming Infix to Postfix
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Computer Science: A Structured Programming Approach Using C Graphs A graph is a collection of nodes, called vertices, and a collection of segments,
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Graphs Upon completion you will be able to:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Chapter 9 Abstract Data Types and Algorithms Nell Dale John Lewis.
Stacks Chapter 3 Objectives Upon completion you will be able to
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Chapter 12 Abstract Data Type.
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Trees Chapter 15.
Chapter 15 Lists Objectives
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Data Structure Interview
Introduction to Data Structure
Chapter 17 Object-Oriented Data Structures
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Heaps Chapter 6 Section 6.9.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

©Brooks/Cole, 2003 Chapter 12 Abstract Data Type

©Brooks/Cole, 2003 BACKGROUNDBACKGROUND 12.1

Abstract Data Type (ADT) In early days programming, to read a file, we wrote a code to read the physical file. And we had to do this over and over again. Using ADT, we write the code to read a file and place it in the library for all programs to use. ATD consists of a set of definitions that allow programmers to use the functions while hiding the implementation.

©Brooks/Cole, 2003 The Concept of Abstraction The Generalization of operations with unspecified implementations is known as abstraction. Thus the concept of abstraction means: –You know what data type can do –How is done is hidden

©Brooks/Cole, 2003 Definition An Abstract data type is: 1.Declaration of data 2.Declaration of operations 3.Encapsulation of data and operations. Note Application programs shouldn’t reference to the data structure. Because if we did so, ADT will not be fully portable to other applications.

©Brooks/Cole, 2003 Figure 12-1 Model for ADT Data are entered, accessed … etc through operational interfaces Only operation name is visible to the user Additional operations may be created to satisfy specific requirements

©Brooks/Cole, 2003 LINEARLISTSLINEARLISTS 12.2

Figure 12-2 Linear list In linear list, each element has a unique successor

©Brooks/Cole, 2003 Figure 12-3 Categories of linear lists In general list, data can be inserted and deleted anywhere and no restriction on operations. In restricted list, data can only be inserted or deleted at the end of the structure.

©Brooks/Cole, 2003 Figure 12-4 (1) Insertion in a linear list Data are inserted in the middle of the list most of the time. To determine where data to be placed, search algorithm (sequential search) is used. If there is no enough room for data, the list is in overflow state and the item can not be added.

©Brooks/Cole, 2003 Figure 12-5 (2) Deletion from a linear list To delete data from the list, the data have to be located using search algorithm (sequential search). Once located, the data are removed. If the list is empty, the list is underflow state and delete operation fails.

©Brooks/Cole, 2003 Figure 12-6 (3)Retrieval from a linear list To retrieve data from the list, the data have to be located using search algorithm. A copy of data should be retrieved without changing the content. You can not retrieve items form empty list.

©Brooks/Cole, 2003 Figure 12-7 (4)Traversal of a linear list List traversal is the operation where all elements in the list are processed sequentially, one by one. Walker point to the element must be processed at a time. Processing could be retrieval, sorting … etc. List traversal requires looping algorithm rather than search, loop terminate when all elements been processed.

©Brooks/Cole, 2003 Implementation of A General Liner List The General list could be either an array or a linked list Liner List Applications Linear list can be used in situation where the elements are accessed randomly. For example; to store information about students in the college Dr Barnawi

©Brooks/Cole, 2003 STACKSSTACKS 12.3

Figure 12-8 Three representations of a stack Stack is a restricted linear list in which all additions and deletions are made at the end, called the top. Stack is known as last in, first out (LIFO) data structure. Examples stack of coins, books….

©Brooks/Cole, 2003 Figure 12-9 (1) Push operation Push adds an item to the top of the stack. The new item becomes the top. If there is no enough room for new item, the stack is in an overflow state and item can’t be added. Dr Barnawi

©Brooks/Cole, 2003 Figure (2) Pop operation in a stack Pop removes an item from the top of the stack and return it to the user. If an empty stack has been popped, the stack is in an underflow state.

©Brooks/Cole, 2003 (3) Empty operation This operation check to see if a stack is empty or not. The response is either true or false

©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)

©Brooks/Cole, 2003 Implementation of a stack Although Stack can be implemented either as an array or a linked list, linked list is more common because pop and push operations are much easier on liked list.

©Brooks/Cole, 2003 Stack Applications 1.Reversing Data; data reordred so the first and the last are exchanged. 2.Parsing; breaking data into independent order. 3.Postponement; first inputs are deferred for later processing. 4.Backtracking; Going back to previous data is used for computer gaming, decision analysis and expert systems. Dr Barnawi

©Brooks/Cole, 2003 QUEUESQUEUES 12.4

Figure Queue representation Queue is a linear list in which data are inserted at the rear and deleted from the end. The Queue is FIFO; first data to be processed first

©Brooks/Cole, 2003 Figure (1) Enqueue operation Enqueue insert data into the queue. The new item becomes the rear. If there is no enough room for new item, the queue is in an overflow state and the item can’t be added. Dr Barnawi

©Brooks/Cole, 2003 Figure Dequeue operation Dequeue removes an item from the front of the queue and return it to the user. If an empty queue has been dequeued, the queue is in an underflow state.

©Brooks/Cole, 2003 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)

©Brooks/Cole, 2003 Implementation of a queue A Queue can be implemented either as an array or a linked list. Queue Applications Queues are found in every operating systems and network and countless other areas.

©Brooks/Cole, 2003 TREESTREES 12.5

Figure Representation of a tree nodes Indegree branch outdegree branch The sum of the indegree and outdegree branches is the degree of the node. The indegree of the root is zero by definition. All nodes in the tree must have indegree of one and different number of outdegree.

©Brooks/Cole, 2003 Figure Tree terminology

©Brooks/Cole, 2003 Figure Subtrees Dr Barnawi Subtree is any connected structure below the root

©Brooks/Cole, 2003 BINARYTREESBINARYTREES 12.6

Figure Binary tree Binary Tree is a tree in which no node can have more than two subtrees. In other word a node can have 0,1 or 2 subtrees. These subtrees are designated as left and right subtrees.

©Brooks/Cole, 2003 Figure Examples of binary trees

©Brooks/Cole, 2003 Properties of Binary Trees (1) Height of Binary Tree Given the number of nodes, N; The maximum height; H max = N The minimum height; H min = [Log 2 N ] + 1 Given the height of binary tree, H; The minimum number of nodes; N min = H The maximum number of nodes; N max = 2 H - 1

©Brooks/Cole, 2003 Properties of Binary Trees (2) Balance The balance factor of binary tree is the difference in height between the left and right subtrees; B = H L - H R A tree is balanced if B = 0 → Rare A tree is balanced if B = -1, 0, +1 → General Dr Barnawi

©Brooks/Cole, 2003 Figure a. Depth-first traversal Operations on Binary trees Binary tree traversals Root node is processed first followed by the lift subtree then the right subtree Left subtree is processed first followed by the root node then the right subtree Left subtree is processed first followed by the right subtree node then the root node

©Brooks/Cole, 2003 Figure Preorder traversal of a binary tree Node is processed when you are on the left

©Brooks/Cole, 2003 Figure Inorder traversal of a binary tree Node is processed when you are under

©Brooks/Cole, 2003 Figure Postorder traversal of a binary tree Node is processed when you are on the right

©Brooks/Cole, 2003 Figure b. Breadth-first traversal of a binary tree Tree is processed in stair like fashion, starting from root at level 0 then nodes in level 1 and so on

©Brooks/Cole, 2003 Implementation of a binary tree A binary tree is normaly implemented as a linked list. Dr Barnawi

©Brooks/Cole, 2003 Binary tree Applications Expression tree The expression tree is binary tree with 1.Each leaf is an oprand. 2.The root and internal nodes are operators. 3.Subtrees are subexpressions and the root is operator. Standard traversals represent three expression formats: 1.Inorder traversal produces the infix expression. 2.Postorder traversal produces the postfix expression. 3.Preorder traversal produces the prefix expression.

©Brooks/Cole, 2003 Figure Expression tree - infix

©Brooks/Cole, 2003 GRAPHSGRAPHS 12.7

Graphs-Terminology A Graph is collection of nodes called vertices (single is vertex) and collection of line segments called lines connecting pairs of vertices. Graph could be directed (diagraph) or undirected. Arcs, Edges Adjacent vertices (neighbor) Path, Cycle, Loop. Strongly, weakly connected and disjoint graphs Degree, outdegree, indegree See book Dr Barnawi

©Brooks/Cole, 2003 Figure Directed and undirected graphs

©Brooks/Cole, 2003 Figure Operations On Graphs (1) Add vertex First vertex is added as a disjoint vertex and then connected to a neighbor

©Brooks/Cole, 2003 Figure (2) Delete vertex When vertex is deleted all edges also are removed

©Brooks/Cole, 2003 Figure (3) Add edge To add an edge two vertex must be specified Dr Barnawi

©Brooks/Cole, 2003 Figure (4) Delete edge When edge is deleted connection between two neighbors are removed

©Brooks/Cole, 2003 Figure (5) Find vertex Find vertex search the graph and return data for specific vertex

©Brooks/Cole, 2003 In graph traversal, you must ensure that you process the data in each vertex only once. The problem is that there are more than one path to a vertex in a graph. The solution is; before traversal, you set a visited flag at each vertex “off”, as you traverse the vertex you set the visited flag “on” (6) Traverse Graph

©Brooks/Cole, 2003 Figure (a) Depth-first traversal of a graph Select any vertex adjacent to processed vertex Dr Barnawi

©Brooks/Cole, 2003 Figure (b) Breadth-first traversal of a graph Process all vertex adjacent to processed vertex

©Brooks/Cole, 2003 Figure 12-35: Part I Graph implementations To represent a graph two sets of data are needed; the first to represent the vertices and the second to represent the arcs

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