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.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Introduction to Trees Chapter 6 Objectives
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.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Data Structures ( 数据结构 ) Chapter 10:Graphs. Vocabulary Graph 图 Vertex 顶点 Edge 边 Arc 弧 Directed Graph 有向图 Undirected Graph 无向图 Adjacent Vertices 邻接点 Path.
Advanced Data Structures
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
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,
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Transforming Infix to Postfix
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
12 Abstract Data Types Foundations of Computer Science ã Cengage Learning.
Binary Trees Chapter 6.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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 
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Starting at Binary Trees
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Discrete Mathematics Chapter 5 Trees.
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.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
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:
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 05 Introduction to Graph And Search Algorithms.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Trees A non-linear implementation for collection classes.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Chapter 12 Abstract Data Type.
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.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 5 : Trees.
Chapter 15 Lists Objectives
Tree.
Lecture 18. Basics and types of Trees
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Trees, Binary Search Trees
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Important Problem Types and Fundamental Data Structures
Binary Trees, Binary Search Trees
Heaps Chapter 6 Section 6.9.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

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

BACKGROUNDBACKGROUND 12.1

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.

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

Queue problem Queue simulation queue enqueuing dequeuing

Abstract Data Type 1.Declaration of data 2.Declaration of operations 3.Encapsulation of data and operations Note:

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.

Figure 12-1 Model for ADT

LINEARLISTSLINEARLISTS 12.2

Figure 12-2 Linear list Linear List is a list in which each element has a unique successor.

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

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

Figure 12-5 Deletion from a linear list

Figure 12-6 Retrieval from a linear list

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

Implementation Two common methods:  Array  Linked list

STACKSSTACKS 12.3

Figure 12-8 Three representations of a stack 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.

Figure 12-9 Push operation in a stack

Figure Pop operation in a stack

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

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

QUEUESQUEUES 12.4

Figure Queue representation 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.

Figure Enqueue operation

Figure Dequeue operation

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

Implementation  Two common methods: Array Linked list (more common) Applications found in virtually every OS and network and in countless other areas.

TREESTREES 12.5

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.

Figure Representation of a tree

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 path from the root to the node. Descendant – any node in the path from the node to a leaf.

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.

Figure Tree terminology Height : 3

Figure Subtrees

BINARYTREESBINARYTREES 12.6

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)  H max = N  H min = [log 2 N] +1 # of nodes of a binary tree – (Given height H)  N min = H  N max = 2 H - 1

Figure Binary tree

Figure Examples of binary trees

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 = H L - H R A binary tree is balanced if the height of its subtrees differs by no more than 1 (B = -1, 0, or +1) and its subtrees are also balanced.

A BC D F E

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

Figure Depth-first traversal of a binary tree NLR LNR LRN

Figure Preorder traversal of a binary tree A, B, C, D, E, F

Figure Inorder traversal of a binary tree C, B, D, A, E, F

Figure Postorder traversal of a binary tree C, D, B, F, E, A

Figure Breadth-first traversal of a binary tree

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: 1. Infix expression 2. Postfix expression – produced by Postorder traversal 3. Prefix expression – produced by Preorder traversal

Figure Expression tree Infix expression: Prefix expression: + * a + b c d Postfix expression: a b c + * d +

GRAPHSGRAPHS 12.7

Graphs A Graph consists of  A set of vertices (sing. vertex) and  A set of lines (connecting pairs of vertices) Directed graph (digraph) – each line has a direction to its successor - arc Undirected graph – each line has no direction – edge Adjacent vertices (neighbors) – two vertices directly connected by a line Path – a sequence of vertices in which each vertex is adjacent to the next one. Cycle – a path consisting of at least three vertices that starts and ends with the same vertex.

Graphs Two vertices are said to be connected if there is a path between them. A graph is said to be connected (suppressing direction) if there is a path from any vertex to any other vertex. A directed graph is strongly connected if there is a path from each vertex to every other vertex. A directed graph is weekly connected if at least two vertices are not connected.

Figure Directed and undirected graphs

Figure Add vertex

Figure Delete vertex

Figure Add edge

Figure Delete edge

Figure Find vertex

Traverse Graph You must somehow ensure that you process the data in each vertex only once. Visited flag Two standard graph traversals  Depth-first traversal – you process a vertex’s descendants before you move to an adjacent vertex.  Breadth-first traversal – you process all adjacent vertices of a vertex before going to the next level.

Figure Depth-first traversal of a graph 在不同的資料結構下,即使起始點相同,產生的順序亦有所不同。

Figure Breadth-first traversal of a graph 在不同的資料結構下,即使起始點相同,產生的順序亦有所不同。

Implementation  Two common methods: Array (adjacency matrix) Linked list (adjacency list) Applications  Network Weighted graph - a graph with weighted lines.  Minimum Spanning Tree 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.

Figure 12-35: Part I Graph implementations Weight (e.g. distance between networks in an internetwork)

Figure 12-35: Part 2 Graph implementations