Download presentation

Presentation is loading. Please wait.

Published byJoshua Harrington Modified over 4 years ago

1
KNURE, Software department, Ph. 7021-446, e-mail: belous@kture.kharkov.ua N.V. Bilous Faculty of computer sciences Software department, KNURE The trees. Discrete mathematics.

2
N.V.Belous7.Introduction to Trees 2 The 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 b a c f g G1G1 G2G2 G3G3 G4G4 The graph G 1 is a Tree, Subgraphs are form the forest.

3
N.V.Belous7.Introduction to Trees 3 The 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

4
N.V.Belous7.Introduction to Trees 4 The 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

5
N.V.Belous7.Introduction to Trees 5 The 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

6
N.V.Belous7.Introduction to Trees 6 The 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

7
N.V.Belous7.Introduction to Trees 7 The 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

8
N.V.Belous7.Introduction to Trees 8 The 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.

9
N.V.Belous7.Introduction to Trees 9 Cayley’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?

10
N.V.Belous7.Introduction to Trees 10 Cayley’s Problem It is possible to construct n n-2 trees on n vertices. n=4, then 4 2 =16. Quantity of trees with 4 vertices.

11
N.V.Belous7.Introduction to Trees 11 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.

12
N.V.Belous7.Introduction to Trees 12 Algorithm 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.

13
N.V.Belous7.Introduction to Trees 13 Algorithm for finding the minimal weight tree Example. It is given graph G. To construct for it a tree of the minimal weight.

14
N.V.Belous7.Introduction to Trees 14 Algorithm for finding the minimal weight tree Continuation of example. СЕ = 4 АВ = 6 АС = 7 S Н =6+8+7+4=25. АD = 8

15
N.V.Belous7.Introduction to Trees 15 Algorithm 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.

16
N.V.Belous7.Introduction to Trees 16 Example of coding of trees 24 1 3 6 7 8 911 10 12 5 13 24 1 3 6 7 8 911 10 12 5 13 24 1 3 6 7 8 911 10 12 5 13 24 1 3 6 7 8 911 10 12 5 13

17
N.V.Belous7.Introduction to Trees 17 Algorithm 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.

18
N.V.Belous7.Introduction to Trees 18 Algorithm 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

19
N.V.Belous7.Introduction to Trees 19 The 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.

20
N.V.Belous7.Introduction to Trees 20 The 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.

21
N.V.Belous7.Introduction to Trees 21 m-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

22
N.V.Belous7.Introduction to Trees 22 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.

23
N.V.Belous8.Properties of Trees 23 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

24
N.V.Belous8.Properties of Trees 24 Properties 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.

25
N.V.Belous8.Properties of Trees 25 Infix, 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.

26
N.V.Belous7.Introduction to Trees 26 Infix, 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.

27
N.V.Belous8.Properties of Trees 27 Infix, prefix, and postfix notation The prefix form: + + x y 2 / – x 4 3 The infix form: x+y 2+x–4/3x+y/2 The postfix form: x y + 2 x 4 – 3 / + The Binary Tree for ((x+y) 2)+((x– 4)/3)

28
N.V.Belous8.Properties of Trees 28 Tree Traversal T 1,T 2,…T n 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 T n is traversed in preorder. Step 1: Visit r T1T1 T2T2 TnTn Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder Step n+1: Visit T n in preorder r

29
N.V.Belous8.Properties of Trees 29 The preorder traversal 1 4 7 29 3 856 1 4 7 29 3 856

30
N.V.Belous8.Properties of Trees 30 Algorithm of Preorder Traversal Algorithm Preorder Traversal. procedure preorder (T ordered rooted tree) r = root of T list r for each child c of r from left to right begin T(c) = subtree with c as its root preorder(T(c)) end

31
N.V.Belous8.Properties of Trees 31 Prefix 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

32
N.V.Belous8.Properties of Trees 32 Tree Traversal T 1,T 2,…T n 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 T n in inorder. Step 2: Visit r T1T1 T2T2 TnTn Step 1: Visit T 1 in inorder Step 3: Visit T 2 in inorder Step n+1: Visit T n in inorder r

33
N.V.Belous8.Properties of Trees 33 The inorder traversal 6 4 2 7 9 1 8 3 56 4 2 7 9 1 8 3 5

34
N.V.Belous8.Properties of Trees 34 Algorithm of Inorder Traversal Algorithm Inorder Traversal procedure inorder(T: ordered rooted tree) r: = root of T if r is a leaf then list r else begin 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

35
N.V.Belous8.Properties of Trees 35 Infix 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+y 2+x–4/3x+y/2

36
N.V.Belous8.Properties of Trees 36 Tree Traversal T 1,T 2,…T n 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 T n in postorder, and ends by visiting r. Step n+1: Visit r T1T1 T2T2 TnTn Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder Step n: Visit T n in postorder r

37
N.V.Belous8.Properties of Trees 37 The postorder traversal 6 29 7 4 8 5 3 1 6 29 7 4 8 5 3 1

38
N.V.Belous8.Properties of Trees 38 Algorithm of Postorder Traversal Algorithm Postorder Traversal. procedure postorder (T: ordered rooted tree) r : = root of T for each child c of r from left to right begin T (c) = subtree with c as its root postorder(T(c)) end list r

39
N.V.Belous8.Properties of Trees 39 Postfix 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 / +

40
N.V.Belous8.Properties of Trees 40 Infix, 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.

41
N.V.Belous8.Properties of Trees 41 A 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.

42
N.V.Belous8.Properties of Trees 42 Equivalent binary trees Binary trees are equivalent, if they are similar and corresponding units contain the same information.

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google