Discrete Structures Trees Ch 11 Dr Muhammad Humayoun

  • Slides: 50
Download presentation
Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer

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 1

First we need some definitions from Graph theory 2

First we need some definitions from Graph theory 2

Path • A path is a sequence of edges that begins at a vertex

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 e 1 , . . . , en of G for which there exists a sequence x 0=u, x 1, . . . , xn− 1, xn=v of vertices. 3

Example • Find a path between a and e. • a, d, c, f

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. • … 4

Circuit • The path is a circuit if it begins and ends at the

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

Connected Graph • An undirected graph is called connected if there is a path

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

Trees • A (free) tree is an undirected graph T such that – T

Trees • A (free) tree is an undirected graph T such that – T has no simple circuits – T is connected Tree • 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. 7

 • Which of the graphs are trees? • G 1 and G 2

• Which of the graphs are trees? • G 1 and G 2 are trees, because both are connected graphs with no simple circuits. • G 3 is not a tree because e, b, a, d, e is a simple circuit in this graph. • G 4 is not a tree because it is not connected. 8

Forest • A forest is an undirected graph such that – It has no

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 9

10

10

Rooted Tree • A rooted tree is a tree in which one vertex has

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 11

Subtree If a is a vertex in a tree, the subtree with a as

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

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

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

Spanning tree of the simple graph • it is not a tree because it

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? 14

15

15

Some facts • A simple graph is connected if and only if it has

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 16

Tree Traversal • Ordered rooted trees are often used to store information. • We

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

Ordered Rooted Tree • An ordered rooted tree is a rooted tree where the

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

Traversal Algorithms • Procedures for systematically visiting every vertex of an ordered rooted tree

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

20

20

Preorder Traversal • Let T be an ordered rooted tree with root r. If

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 T 1, T 2, . . . , Tn are the subtrees at r from left to right in T. The preorder traversal begins by visiting r. • It continues by traversing T 1 in preorder, then T 2 in preorder, and so on, until Tn is traversed in preorder 21

22

22

23

23

24

24

25

25

Procedure preorder(T: ordered rooted tree) r: = root of T List/print r for each

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

Inorder Traversal 27

Inorder Traversal 27

Inorder traversal • Let T be an ordered rooted tree with root r. If

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 T 1, T 2, . . . , Tn are the subtrees at r from left to right. • The inorder traversal begins by traversing T 1 in inorder, then visiting r. It continues by traversing T 2 in inorder, then T 3 in inorder, . . . , and finally Tn in inorder. 28

29

29

30

30

31

31

Procedure procedure inorder (T : ordered rooted tree) r : = root of T

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

Postorder Traversal 33

Postorder Traversal 33

Postorder Traversal • Let T be an ordered rooted tree with root r. If

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 T 1, T 2, . . . , Tn are the subtrees at r from left to right. The postorder traversal begins by traversing T 1 in postorder, then T 2 in postorder, . . . , then Tn in postorder, and ends by visiting r. 34

35

35

36

36

37

37

Proceedure procedure postorder(T : ordered rooted tree) r : = root of T for

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 38

Infix, Prefix, and Postfix Notation • We can represent complicated expressions, such as compound

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

Ordered rooted tree of an Expression • ((x + y) ↑ 2) + ((x

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

Infix notation • It is the common arithmetic and logical formula notation, in which

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

Prefix form • Also known as Polish notation. • A form of notation for

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*67 Prefix notation 42

Prefix form • What is prefix form of ((x + y) ↑ 2) +

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

Evaluating a prefix form What is the value of the prefix expression + −

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

Postfix form • Also known as Reverse Polish Notation. • We obtain the postfix

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

Example What is the postfix form of the expression: ((x + y) ↑ 2)

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

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

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

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

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

Binary Trees • Binary rooted Trees • A binary tree is a tree data

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

END 50

END 50