![Page 1: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/1.jpg)
1
Discrete StructuresTrees (Ch. 11)
Dr. Muhammad HumayounAssistant Professor
COMSATS Institute of Computer Science, [email protected]
https://sites.google.com/a/ciitlahore.edu.pk/dstruct/Modified slides of Dr. M. Atif
![Page 2: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/2.jpg)
2
First we need
some definitions from Graph theory
![Page 3: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/3.jpg)
3
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.
![Page 4: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/4.jpg)
4
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.• …
![Page 5: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/5.jpg)
5
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.
![Page 6: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/6.jpg)
6
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.)
![Page 7: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/7.jpg)
7
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
![Page 8: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/8.jpg)
8
• 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.
![Page 9: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/9.jpg)
9
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
ForestOne graph with 3 trees
![Page 10: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/10.jpg)
10
![Page 11: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/11.jpg)
11
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
![Page 12: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/12.jpg)
12
SubtreeIf 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.
![Page 13: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/13.jpg)
13
Spanning Tree
• A spanning tree of a connected graph is a spanning subgraph that is a tree
![Page 14: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/14.jpg)
14
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?
![Page 15: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/15.jpg)
15
![Page 16: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/16.jpg)
16
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
![Page 17: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/17.jpg)
17
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.
![Page 18: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/18.jpg)
18
Ordered Rooted Tree• An ordered rooted tree is a rooted tree where
the children of each internal vertex are ordered.
![Page 19: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/19.jpg)
19
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.
![Page 20: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/20.jpg)
20
![Page 21: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/21.jpg)
21
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
![Page 22: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/22.jpg)
22
![Page 23: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/23.jpg)
23
![Page 24: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/24.jpg)
24
![Page 25: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/25.jpg)
25
![Page 26: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/26.jpg)
26
Procedureprocedure preorder(T:ordered rooted tree)r:= root of TList/print rfor each child e of r from left to right T(e) := subtree with e as its root preorder(T(e))
![Page 27: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/27.jpg)
27
Inorder Traversal
![Page 28: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/28.jpg)
28
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.
![Page 29: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/29.jpg)
29
![Page 30: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/30.jpg)
30
![Page 31: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/31.jpg)
31
![Page 32: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/32.jpg)
32
Procedureprocedure inorder (T : ordered rooted tree)r := root of Tif r is a leaf then list/print relse 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))
![Page 33: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/33.jpg)
33
Postorder Traversal
![Page 34: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/34.jpg)
34
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.
![Page 35: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/35.jpg)
35
![Page 36: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/36.jpg)
36
![Page 37: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/37.jpg)
37
![Page 38: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/38.jpg)
38
Proceedure
procedure postorder(T : ordered rooted tree)r := root of Tfor each child c of r from left to right T (c) := subtree with c as its root postorder(T (c))list/print r
![Page 39: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/39.jpg)
39
Infix, Prefix, and Postfix Notation
• We can represent complicated expressions, such as compound propositions, combinations of sets, and arithmetic expressions using ordered rooted trees.
![Page 40: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/40.jpg)
40
Ordered rooted tree of an Expression
• ((x + y) ↑ 2) + ((x − 4)/3)
![Page 41: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/41.jpg)
41
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.
![Page 42: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/42.jpg)
42
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
![Page 43: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/43.jpg)
43
Prefix form
• What is prefix form of ((x + y) ↑ 2) + ((x − 4)/3)?• + ↑ + x y 2 / − x 4 3.
• Preorder traversal
![Page 44: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/44.jpg)
44
Evaluating a prefix form
What is the value of the prefix expression + − 2 3 5∗ /↑ 2 3 4?
![Page 45: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/45.jpg)
45
Postfix form
• Also known as Reverse Polish Notation.• We obtain the postfix form of an expression by
traversing its tree in postorder.
![Page 46: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/46.jpg)
46
Example
What is the postfix form of the expression: ((x + y) ↑ 2) + ((x - 4)/3) ?
Postorder Traversal
x y + 2 ↑ x 4 - 3 / +
![Page 47: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/47.jpg)
47
Evaluating a Postfix Expression
Postfix expression: 7 2 3 * - 4 t 9 3/+
![Page 48: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/48.jpg)
48
Representing other types of expressions with Rooted trees
Compound proposition: (¬ (p q)) ↔ (∧ ¬ p ∨¬ q).
![Page 49: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/49.jpg)
49
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".
![Page 50: Discrete Structures Trees (Ch. 11) Dr. Muhammad Humayoun Assistant Professor COMSATS Institute of Computer Science, Lahore. mhumayoun@ciitlahore.edu.pk](https://reader035.vdocuments.us/reader035/viewer/2022062409/56649f295503460f94c41c95/html5/thumbnails/50.jpg)
50
END