discrete structures trees
TRANSCRIPT
-
8/6/2019 Discrete Structures Trees
1/26
CSC 102Discrete Structures
Trees
Atif Manzoor, [email protected]
-
8/6/2019 Discrete Structures Trees
2/26
What is a tree?
A tree is a connected simple undirected graph
with no simple circuits
Properties:
There is a unique simple path between any 2 of its
vertices
No loops
No multiple edges
-
8/6/2019 Discrete Structures Trees
3/26
Example 1
This graph is a tree because it is a connected
graph with no simple circuits
-
8/6/2019 Discrete Structures Trees
4/26
Example 2
This graph is not a tree because there is a
cycle a, b, e, d, a
-
8/6/2019 Discrete Structures Trees
5/26
Forest
An undirected graph without simple circuits is
called aforest
You can think of it as a set of trees having disjoint
sets of nodes.
-
8/6/2019 Discrete Structures Trees
6/26
Example
This graph is not a tree because it is not
connected
This graph is Forest in which each connected
component is a tree
Component 1: a, f
Component 2: c, e, b, d
-
8/6/2019 Discrete Structures Trees
7/26
Rooted (Directed) Trees
A rooted tree is a tree in which one node has
been designated the root and every and edge
is directed away from the root.
You should know the following terms about
rooted trees:
Root, Parent, child, siblings, ancestors,
descendents, leaf, internal node, subtree
-
8/6/2019 Discrete Structures Trees
8/26
Terminology
Root: a vertex with indegreezero [node a is parent]
Parent: Vertex u is a parent
such that there is a directededge from vertex u to vertexv [b is a parent of f and g]
Child: If vertex u is a parent
of vertex v then vertex v isthe child of vertex u [g and fare children of b]
-
8/6/2019 Discrete Structures Trees
9/26
Terminology
Siblings: Vertices with the
same parents are siblings [ f
and g ]
Ancestors: Vertices in thepath from the root to vertex
v, excluding v itself and
including the root are the
ancestors of vertex v [a and bare the ancestors of g]
-
8/6/2019 Discrete Structures Trees
10/26
Terminology
Descendents: All thevertices that have vertex v asancestor are thedescendents of v[Descendents of b: f, g, y]
Leaf: Vertices with nochildren are the leaves [y, f,g, e, d]
Internal Vertices are thevertices that have children
-
8/6/2019 Discrete Structures Trees
11/26
Terminology
Subtree: Subtree is asubgraph consisting of v andits descendents and theirincident edges [subtreerooted at b]
Level (of a vertex v) is thelength of unique path fromroot to v [level of root = 0,level of b = 1, level of g =2]
Height is the maximum ofvertices levels. [Height = 3]
-
8/6/2019 Discrete Structures Trees
12/26
m-ary trees
A rooted tree is called m-ary if every if internal
vertex has no more than m children
It is full mary if every internal vertex has
exactly m children.
A 2-ary tree is called a binary tree.
-
8/6/2019 Discrete Structures Trees
13/26
Examples
-
8/6/2019 Discrete Structures Trees
14/26
Ordered Rooted Tree
A rooted tree where the children of each
internal node are ordered
In ordered binary trees, we can define:
left child, right child
left subtree, right subtree
For m-ary trees with trees m>2, we can use
terms like leftmost, rightmostetc.
-
8/6/2019 Discrete Structures Trees
15/26
Examples
-
8/6/2019 Discrete Structures Trees
16/26
Properties Of Trees
1. A tree with n vertices has vertices n- 1 edges
2. A full m-ary tree with I internal vertices containsn = m * I + 1 vertices
3. A full m-ary tree with: n vertices has I = (n 1 ) / m internal vertices and L
=[ ( m-1)*n + 1]/ m leaves
I internal vertices has internal n = m * I + 1 vertices
and L = (m-1)*I + 1 leaves L leaves has n = (m*L 1) / ( m- 1) vertices and I = (L
1) / (m-1) internal vertices.
-
8/6/2019 Discrete Structures Trees
17/26
-
8/6/2019 Discrete Structures Trees
18/26
Balanced Trees
A rooted a m-ary tree of height tree h is
balanced if all leaves are at h or h-1
-
8/6/2019 Discrete Structures Trees
19/26
Tree Traversal
Tree traversal is visiting every vertex of
ordered rooted tree.
Algorithms
Preorder: Root, Left, Right
Inorder: Left, Root, Right
Postorder: Left, Right, Root.
-
8/6/2019 Discrete Structures Trees
20/26
Example
-
8/6/2019 Discrete Structures Trees
21/26
Infix, prefix, and postfix Notation
A tree can be used to represent mathematicalexpressions
Example: (( x + y) * 2) + (( x 4) / 3)
Prefix: + * + x y 2 / - x 4 3
Postfix: x y + 2 * x 4 3 / +
-
8/6/2019 Discrete Structures Trees
22/26
Infix: Inorder traversal of tree, it must be fullyparenthesized it to remove ambiguity
Example: (( x + y) * 2) + (( x 4) / 3)
Prefix (polish): preorder traversal of tree (noparenthesis needed)
Example: from the above tree: + * + x y 2 / - x 4 3
Postfix: postorder traversal (no parenthesisneeded)
Example: from the above tree: x y + 2 * x 4 - 3 / +
-
8/6/2019 Discrete Structures Trees
23/26
Evaluating Expressions
-
8/6/2019 Discrete Structures Trees
24/26
Postfix: Left to right postorder
-
8/6/2019 Discrete Structures Trees
25/26
Spanning Tree
Let G be a simple graph. A spanning tree of G
is a subgraph of G that is a tree containing
every vertex of G
-
8/6/2019 Discrete Structures Trees
26/26
Exmaple
Graph b is a subgraph of Graph A containing
all of its vertices, therefore Graph B is a
spanning tree of Graph a