Discrete Structures Trees (Ch. 11)

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
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,
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Module #19: Graph Theory: part II Rosen 5 th ed., chs. 8-9.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
CS 103 Discrete Structures Lecture 23 Trees (1). Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
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.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
Discrete Mathematics Trees.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
12. Graphs and Trees 2 Summary
Section 8.1 Trees.
CS223 Advanced Data Structures and Algorithms
Graph Algorithms Using Depth First Search
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
And the Final Subject is…
Trees Chapter 11.
Presentation transcript:

Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk https://sites.google.com/a/ciitlahore.edu.pk/dstruct/ Modified slides of Dr. M. Atif

First we need some definitions from Graph theory

Path A path is a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along edges of the graph. Formal Definition: Let n∈Z+ and G an undirected graph. A path of length n from vertices u to v in G is a sequence of n edges e1 , ..., en of G for which there exists a sequence x0=u, x1, . . . , xn−1, xn=v of vertices.

Example Find a path between a and e. a, d, c, f , e is a simple path of length 4, because {a, d}, {d, c}, {c, f }, and {f, e} are all edges. a, e is a simple path of length 1, because {a, e} is the edge connecting both vertices. a, b, f , e is a simple path of length 3, because {a, b}, {b, f}, and {f, e} are all edges. …

Circuit The path is a circuit if it begins and ends at the same vertex, that is, if u = v, and has length greater than zero. Path a, b, c, d, a is a circuit. A path or circuit is simple if it does not contain the same edge more than once.

Connected Graph An undirected graph is called connected if there is a path between every pair of distinct vertices of the graph. The graph G1 is connected. The graph G2 is not connected. (For instance, there is no path in G2 between vertices a and d.)

Trees A (free) tree is an undirected graph T such that T has no simple circuits T is connected A tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph. Theorem: An undirected graph is a tree iff there is a unique simple path between any two of its vertices. Tree

Which of the graphs are trees? G1 and G2 are trees, because both are connected graphs with no simple circuits. G3 is not a tree because e, b, a, d, e is a simple circuit in this graph. G4 is not a tree because it is not connected.

Forest A forest is an undirected graph such that It has no simple circuits It is not necessarily connected The connected components of a forest are trees Forest One graph with 3 trees

Rooted Tree A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root. Root, parent, child, siblings relations between vertices Tree and Rooted Tree

Subtree If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges connected to these descendants.

Spanning Tree A spanning tree of a connected graph is a spanning subgraph that is a tree

Spanning tree of the simple graph it is not a tree because it contains three simple circuits. Which ones? {a, e}, {e,f} and {c,g} Spanning tree: Can we produce more spanning trees?

Some facts A simple graph is connected if and only if it has a spanning tree. A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks

Tree Traversal Ordered rooted trees are often used to store information. We need procedures for visiting each vertex of an ordered rooted tree to access data. We will study some important algorithms for visiting all the vertices of an ordered rooted tree. Ordered rooted trees can also be used to represent various types of expressions.

Ordered Rooted Tree An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.

Traversal Algorithms Procedures for systematically visiting every vertex of an ordered rooted tree are called traversal algorithms. Most commonly used algorithms: Preorder traversal Inorder traversal Postorder traversal All recursively defined.

Preorder Traversal Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T . Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right in T. The preorder traversal begins by visiting r. It continues by traversing T1 in preorder, then T2 in preorder, and so on, until Tn is traversed in preorder

Procedure procedure preorder(T:ordered rooted tree) r:= root of T List/print r for each child e of r from left to right T(e) := subtree with e as its root preorder(T(e))

Inorder Traversal

Inorder traversal Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T . Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The inorder traversal begins by traversing T1 in inorder, then visiting r. It continues by traversing T2 in inorder, then T3 in inorder, . . . , and finally Tn in inorder.

Procedure procedure inorder (T : ordered rooted tree) r := root of T if r is a leaf then list/print r else l := first child of r from left to right T (l) := subtree with l as its root inorder(T (l)) list/print r for each child c of r except for l from left to right T (c) := subtree with c as its root inorder(T (c))

Postorder Traversal

Postorder Traversal Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T . Otherwise, suppose that T1, T2, . . . , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, . . . , then Tn in postorder, and ends by visiting r.

Proceedure procedure postorder(T : ordered rooted tree) r := root of T for each child c of r from left to right T (c) := subtree with c as its root postorder(T (c)) list/print r

Infix, Prefix, and Postfix Notation We can represent complicated expressions, such as compound propositions, combinations of sets, and arithmetic expressions using ordered rooted trees.

Ordered rooted tree of an Expression ((x + y) ↑ 2) + ((x − 4)/3)

Infix notation It is the common arithmetic and logical formula notation, in which operators are written between the operands they act on (e.g. 2 + 2). Inorder traversal.

Prefix form Also known as Polish notation. A form of notation for logic, arithmetic, and algebra. It places operators to the left of their operands. (5 − 6) * 7 Infix notation * - 5 6 7 Prefix notation 5 − (6 * 7) Infix notation − 5 * 6 7 Prefix notation

Prefix form What is prefix form of ((x + y) ↑ 2) + ((x − 4)/3)? Preorder traversal

Evaluating a prefix form What is the value of the prefix expression + − ∗ 2 3 5/↑ 2 3 4?

Postfix form Also known as Reverse Polish Notation. We obtain the postfix form of an expression by traversing its tree in postorder.

Example What is the postfix form of the expression: ((x + y) ↑ 2) + ((x - 4)/3) ? Postorder Traversal x y + 2 ↑ x 4 - 3 / +

Evaluating a Postfix Expression Postfix expression: 7 2 3 * - 4 t 9 3/+

Representing other types of expressions with Rooted trees Compound proposition: (¬(p ∧ q)) ↔ (¬p ∨¬q).

Binary Trees Binary rooted Trees A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right".

END