knure, software department, ph. 7021-446, e-mail: [email protected]
DESCRIPTION
Discrete mathematics. The trees. N . V . Bilous. Faculty of computer sciences Software department, KNURE. KNURE, Software department, Ph. 7021-446, e-mail: [email protected]. The basic definitions. a. b. c. d. g. f. - PowerPoint PPT PresentationTRANSCRIPT
KNURE, Software department,Ph. 7021-446, e-mail: [email protected]
NN..VV. . BilousBilous
Faculty of computer sciencesSoftware department, KNURE
The trees.The trees.
Discrete mathematicsDiscrete mathematics..
N.V.Belous 7.Introduction to Trees 2
The basic definitionsThe basic definitions
A tree is a connected undirected graph with no simple circuits.
Graphs containing no simple circuits that are not necessarily connected are called forests .
d
ba
c
fg G1G2 G3 G4
The graph G1 is a Tree , Subgraphs are form the forest.42 GG
N.V.Belous 7.Introduction to Trees 3
The basic definitionsThe basic definitions
A particular vertex of a tree is designated as the root.
A tree together with its root produces a directed graph called a rooted tree.
h
e
i
g
k
b
d
c
f
a
A Rooted Tree: a – a root
N.V.Belous 7.Introduction to Trees 4
The basic definitionsThe basic definitions
Let’s T is a rooted tree. If v is a vertex in T other than the root, the parent of v is the unique, vertex u such that there is a directed edge from u to v.
When u is the parent of v, v is called a child of u.
Example
The parent of h and i is g.
The children of g are h, and i i
g
k
h
N.V.Belous 7.Introduction to Trees 5
The basic definitionsThe basic definitions
Vertices with the same parent are called siblings.
The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root.
Example
The sibling of h is i.
The ancestors of e are c, b, and a.
h
e
i
g
k
b
d
c
f
a
N.V.Belous 7.Introduction to Trees 6
The basic definitionsThe basic definitions
The descendants of a vertex v are those vertices that have v as an ancestor.
A vertex of a tree is called a leaf if it has no children. Vertices that have children (exclude a root) are called
internal vertices. Example
The descendants of b are c, d and e.
The leaves are d, e, f, i and k. The internal vertices are a, b, c, g and h.
h
e
i
g
k
b
d
c
f
a
N.V.Belous 7.Introduction to Trees 7
The basic definitionsThe basic definitions
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 incident to these descendants.
A Rooted Tree and its Subtree.
h
e
i
g
k
b
d
c
f
a
i
g
k
h
N.V.Belous 7.Introduction to Trees 8
The basic definitionsThe basic definitions
A spainning tree is a tree which contains all vertices of the initial graph.
The chord of spainning tree Н in graph G is the edge of graph G, which are not belonging to spainning tree Н (this edge, is between vertices 3 and 4).
Spainning tree H of graph G.
N.V.Belous 7.Introduction to Trees 9
Cayley’s ProblemCayley’s Problem
Let М is set of cities.с(a, b) is cost of road construction between
cities a and b (a, b M). What should be net of roads between the cities a
and b (a, b M) that on it was possible to pass from any city a into any city b and that cost of this net с(a, b) was minimal?
N.V.Belous 7.Introduction to Trees 10
Cayley’s ProblemCayley’s Problem
It is possible to construct nn-2 trees on n vertices.
n=4 , then 42=16.
Quantity of trees with 4 vertices.
N.V.Belous 7.Introduction to Trees 11
Tree of the minimal weight (cost)Tree of the minimal weight (cost)
Graph that have a number assigned to each edge is called a weighted graphs.
A number assigned to each edge of graph is called
a weight.
The sum of edges weights for a graph is called a weight of graph from G .
Graph G.
N.V.Belous 7.Introduction to Trees 12
Algorithm for finding the minimal weight treeAlgorithm for finding the minimal weight tree
1. Arrange edges use the order of their weight number.
2. Include in the spainning tree edges use the increasing order of weight number.
3. Add a next edge. If an added edge generate the circuit in the graph then delete it from the tree.
4. The tree is constructed when in it is included n-1 edge.
N.V.Belous 7.Introduction to Trees 13
Algorithm for finding the minimal weight treeAlgorithm for finding the minimal weight tree
Example.
It is given graph G. To construct for it a tree of the minimal weight.
N.V.Belous 7.Introduction to Trees 14
Algorithm for finding the minimal weight treeAlgorithm for finding the minimal weight tree
Continuation of example.СЕ = 4 АВ = 6
АС = 7 SН=6+8+7+4=25.АD = 8
N.V.Belous 7.Introduction to Trees 15
Algorithm of tree codingAlgorithm of tree coding
1)Count the number of vertices n in the tree. The length of the tree code is equal to n-2.
2) Write all leaves of the tree. From of all leaves find the leaf with a minimal number. This leaf is deleted from the tree with the incidence edge. The number of an adjacence vertex write in the tree code.
3) This process repeats until will receive the code by length n-2.
N.V.Belous 7.Introduction to Trees 16
Example of coding of treesExample of coding of trees
2 4
1
3
6
78
9 11
10
12
5
13
2 4
1
3
6
78
9 11
10
12
5
13
2 4
1
3
6
78
9 11
10
12
5
13
2 4
1
3
6
78
9 11
10
12
5
13
N.V.Belous 7.Introduction to Trees 17
Algorithm of the tree decodingAlgorithm of the tree decoding
1) Consider the tree code. If the code length is equal top, then the number of vertices in the tree is equal to p+2;
2) Find vertices (leaves number that do not enter into a code);
3) Find the leaf with minimal number and connect it with the first unuseable vertex in the code. If the connected vertex does not use in the next positions of the code, write this vertex in the sequence of leaves;
4) Repeat point 3),until use all leaves;
5) The last vertex in the code connect with the leaf with maximal number.
N.V.Belous 7.Introduction to Trees 18
Algorithm of decoding of treesAlgorithm of decoding of trees
Example.The code: 1, 1, 1, 5. It is necessary to construct a tree.The decision.
1. Total of vertices equally 4+2=6.2. Find leaves: 2, 3, 4, 6
2 3 4
1 5
6
N.V.Belous 7.Introduction to Trees 19
The level and height of treeThe level and height of tree
The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex.
The level of the root is defined to be zero.
The height of a rooted tree is the maximum of the levels of vertices.
N.V.Belous 7.Introduction to Trees 20
The level and height of treeThe level and height of tree
Example.
The root a is at level 0.
b, j, o and k are at level 1.
d, g, i, m, n, and s are at level 3.
c, e, f, l and p are at level 2.
h is at level 4.
This tree has height 4.
a
k
l p
m
o
kd g
c
b
e
j
h
f
in
N.V.Belous 7.Introduction to Trees 21
m-ary tree, binary treem-ary tree, binary tree
A rooted tree is called an m-ary tree if every internal vertex has no more than m children.
The tree is called a full m-ary tree if every internal vertex has exactly m children.
An m-ary tree with m = 2 is called a binary tree.
A Binary Tree
a
f g
d
b
e
c
h i
j lk
N.V.Belous 7.Introduction to Trees 22
A binary tree.A binary tree.
In an ordered binary tree, if an internal vertex has two children, the first child is called the left child and the second child is called the right child.
The tree rooted at the left child of a vertex is called the left subtree of this vertex.
The tree rooted at the right child of a vertex is called the right subtree of the vertex .
N.V.Belous 8.Properties of Trees 23
Properties of TreesProperties of Trees
Properties of Trees
1. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.
2. A tree with n vertices has n –1 edges.
3. A full m-ary tree with i internal vertices contains n = mi + 1 vertices
N.V.Belous 8.Properties of Trees 24
Properties of TreesProperties of Trees
4. A full m-ary tree
(I) with n vertices has i = (n - 1)/m internal vertices and
l = [(m - 1)n + 1]/m leaves.
(II) with i internal vertices has n = mi + 1 vertices and
l = (m - 1)i + 1 leaves.
(III) with l leaves has n = (ml - 1)/(m - 1) vertices and i = (l-1)/(m-1) internal vertices.
N.V.Belous 8.Properties of Trees 25
Infix, prefix, and postfix notationInfix, prefix, and postfix notation
An ordered rooted tree can be used to represent such expressions, where the internal vertices represent operations, and the leaves represent the variables or numbers.
There are infix, prefix, and postfix notation ordered rooted tree.
N.V.Belous 7.Introduction to Trees 26
Infix, prefix, and postfix notationInfix, prefix, and postfix notation
Infix notation: any operation write between operands.
Prefix notation: at first write operations, at second – operands.
Postfix notation: at first write operands, at second – operations.
N.V.Belous 8.Properties of Trees 27
Infix, prefix, and postfix notationInfix, prefix, and postfix notation
The prefix form:
+ + x y 2 / – x 4 3
The infix form:
x+y2+x–4/3x+y/2
The postfix form:
x y + 2 x 4 – 3 / + y
+
x
-
x 4
/
32
*
+
The Binary Tree for ((x+y)2)+((x– 4)/3)
N.V.Belous 8.Properties of Trees 28
Tree TraversalTree Traversal
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. Step 1: Visit r
T1 T2 Tn
Step 2: Visit T1
in preorder
Step 3: Visit T2
in preorder
Step n+1: Visit Tn
in preorder
r
N.V.Belous 8.Properties of Trees 29
The preorder traversalThe preorder traversal
1
4
7
2 9
3
8 56
1
4
7
2 9
3
8 56
N.V.Belous 8.Properties of Trees 30
Algorithm of Preorder TraversalAlgorithm of Preorder Traversal
Algorithm Preorder Traversal.procedure preorder (T ordered rooted tree)r = root of Tlist rfor each child c of r from left to rightbegin T(c) = subtree with c as its root preorder(T(c))end
N.V.Belous 8.Properties of Trees 31
Prefix notationPrefix notation
Obtain the prefix form of an expression when traverse its rooted tree in preorder.
Example.
By traversing it in preorder
receive the prefix notation for
this expression:
+ + x y 2 / – x 4 3
y
+
x
-
x 4
/
32
*
+
N.V.Belous 8.Properties of Trees 32
Tree TraversalTree Traversal
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. Step 2: Visit r
T1 T2 Tn
Step 1: Visit T1
in inorder
Step 3: Visit T2
in inorder
Step n+1: Visit Tn
in inorder
r
N.V.Belous 8.Properties of Trees 33
The The inorderinorder traversal traversal
6
4
2
7
9
1
8
3
56
4
2
7
9
1
8
3
5
N.V.Belous 8.Properties of Trees 34
Algorithm of Inorder TraversalAlgorithm of Inorder Traversal
Algorithm Inorder Traversalprocedure inorder(T: ordered rooted tree)r: = root of Tif r is a leaf then list relsebegin l: = first child of r from left to right T(l) : = subtree with l as its root Inorder (T (l)) list for each child c of r except for l from left to right T (c) : = subtree with c as its root Inorder (T (c))end
N.V.Belous 8.Properties of Trees 35
Infix notationInfix notation
Obtain the infix form of an expression when traverse its rooted tree in inorder.
Example.
By traversing it in inorder
receive the infix notation for
this expression:
x+y2+x–4/3x+y/2
y
+
x
-
x 4
/
32
*
+
N.V.Belous 8.Properties of Trees 36
Tree TraversalTree Traversal
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. Step n+1: Visit r
T1 T2 Tn
Step 1: Visit T1
in postorder
Step 2: Visit T2
in postorder
Step n: Visit Tn
in postorder
r
N.V.Belous 8.Properties of Trees 37
The pThe postoostorder traversalrder traversal
6
2 9
7
4
8 5
3
1
6
2 9
7
4
8 5
3
1
N.V.Belous 8.Properties of Trees 38
Algorithm of Postorder TraversalAlgorithm of Postorder Traversal
Algorithm Postorder Traversal.procedure postorder (T: ordered rooted tree)r : = root of Tfor each child c of r from left to rightbegin T (c) = subtree with c as its root postorder(T(c))endlist r
N.V.Belous 8.Properties of Trees 39
Postfix notationPostfix notation
Obtain the postfix form of an expression when traverse its rooted tree in postorder .
Example.
By traversing it in postorder
receive the postfix notation for
this expression:
x y + 2 x 4 – 3 / +
y
+
x
-
x 4
/
32
*
+
N.V.Belous 8.Properties of Trees 40
Infix, prefix, and postfix notationInfix, prefix, and postfix notation
Obtain the prefix form of an expression when traverse its rooted tree in preorder. Expressions written in prefix form are said to be in Polish notation (which is named after the logician Jan Lukasiewicz) .
Obtain the infix form of an expression when traverse its rooted tree in inorder.
Obtain the postfix form of an expression by traversing its binary tree in postorder. Expressions written in postfix form are said to be in reverse Polish notation.
N.V.Belous 8.Properties of Trees 41
A similar binary treesA similar binary trees
Two binary trees are called a similar if they have identical structure, e. m. they are either empty or contain identical number subtrees and their left and right subtrees are similar.
N.V.Belous 8.Properties of Trees 42
Equivalent binary treesEquivalent binary trees
Binary trees are equivalent, if they are similar and corresponding units contain the same information.