License
c 2001-2016 T. Uyar, A. Yayımlı, E. Harmancı
Discrete Mathematics
You are free to:
I Share – copy and redistribute the material in any medium or format
I Adapt – remix, transform, and build upon the material
Trees
Under the following terms:
H. Turgut Uyar
Ayşegül Gençata Yayımlı
Emre Harmancı
I Attribution – You must give appropriate credit, provide a link to the license,
and indicate if changes were made.
I NonCommercial – You may not use the material for commercial purposes.
I ShareAlike – If you remix, transform, or build upon the material, you must
2001-2016
distribute your contributions under the same license as the original.
For more information:
https://creativecommons.org/licenses/by-nc-sa/4.0/
Read the full license:
https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
1 / 55
Topics
2 / 55
Tree
Definition
tree: connected graph with no cycle
Trees
Introduction
Rooted Trees
Binary Trees
Decision Trees
examples
Tree Problems
Minimum Spanning Tree
3 / 55
4 / 55
Tree Theorems
Tree Theorems
Theorem
T is a tree (T is connected and contains no cycle).
⇔
There is one and only one path
between any two distinct nodes in T .
⇔
T is connected, but if any edge is removed
it will no longer be connected.
⇔
T contains no cycle, but if an edge is added
between any pair of nodes one and only one cycle will be formed.
Theorem
|E | = |V | − 1
I
proof method: induction on the number of edges
6 / 55
5 / 55
Tree Theorems
Tree Theorems
Proof: Induction step.
I
|E | = k + 1
Proof: Base step.
I
|E | = 0 ⇒ |V | = 1
I
|E | = 1 ⇒ |V | = 2
I
|E | = 2 ⇒ |V | = 3
I
assume that |E | = |V | − 1 for |E | ≤ k
I
remove edge (y , z):
T1 = (V1 , E1 ), T2 = (V2 , E2 )
|V | = |V1 | + |V2 |
= |E1 | + 1 + |E2 | + 1
= (|E1 | + |E2 | + 1) + 1
= |E | + 1
7 / 55
8 / 55
Tree Theorems
Tree Theorems
Theorem
In a tree, there are at least two nodes with degree 1.
Theorem
Proof.
T is a tree (T is connected and contains no cycle).
⇔
T is connected and |E | = |V | − 1.
⇔
T contains no cycle and |E | = |V | − 1.
P
I
2|E | =
I
assume: only 1 node with degree 1:
⇒ 2|E | ≥ 2(|V | − 1) + 1
⇒ 2|E | ≥ 2|V | − 1
⇒ |E | ≥ |V | − 12 > |V | − 1
v ∈V
dv
10 / 55
9 / 55
Rooted Tree
Node Level
I
hierarchy between nodes
I
level of node: distance from root
I
creates implicit direction on edges: in and out degrees
I
parent: adjacent node closer to root (only 1 such node)
I
in-degree 0: root (only 1 such node)
I
child: adjacent nodes further from root
I
out-degree 0: leaf
I
sibling: nodes with same parent
I
not a leaf: internal node
I
depth of tree: maximum level in tree
11 / 55
12 / 55
Rooted Tree Example
Rooted Tree Example
Book
I C1
I
I
leaves: x y z u v
I
internal nodes: r p n t s q w
I
parent of y : w
children of w : y and z
I
I
root: r
I
I
I
S1.1
S1.2
C2
C3
I
I
y and z are siblings
S3.1
S3.2
I
I
I
13 / 55
Ordered Rooted Tree
S3.2.1
S3.2.2
S3.3
14 / 55
Lexicographic Order
I
siblings ordered from left to right
I
address A comes before address B if one of:
I
universal address system
I
I
root: 0
I
children of root: 1, 2, 3, . . .
A = x1 x2 . . . xi xj . . .
B = x1 x2 . . . xi xk . . .
xj comes before xk
I
v : internal node with address a
children of v : a.1, a.2, a.3, . . .
I
A = x1 x2 . . . xi
B = x1 x2 . . . xi xk . . .
15 / 55
16 / 55
Lexicographic Order Example
Binary Trees
I
0 - 1 - 1.1 - 1.2
- 1.2.1 - 1.2.2 - 1.2.3
- 1.2.3.1 - 1.2.3.2
- 1.3 - 1.4 - 2
- 2.1 - 2.2 - 2.2.1
- 3 - 3.1 - 3.2
I
T = (V , E ) is a binary tree:
∀v ∈ V [dv o ∈ {0, 1, 2}]
I
T = (V , E ) is a complete binary tree:
∀v ∈ V [dv o ∈ {0, 2}]
18 / 55
17 / 55
Expression Tree
I
binary operations can be represented as binary trees
I
root: operator, children: operands
I
mathematical expression can be represented as trees
I
internal nodes: operators, leaves: variables and values
Expression Tree Examples
7−a
19 / 55
a+b
20 / 55
Expression Tree Examples
7−a
5
Expression Tree Examples
7−a
· (a + b)3
5
(a + b)3
22 / 55
21 / 55
Expression Tree Examples
Expression Tree Traversals
1. inorder traversal:
traverse left subtree, visit root, traverse right subtree
t+
2. preorder traversal:
visit root, traverse left subtree, traverse right subtree
u∗v
w + x − yz
3. postorder traversal (reverse Polish notation):
traverse left subtree, traverse right subtree, visit root
23 / 55
24 / 55
Inorder Traversal Example
Preorder Traversal Example
t + u ∗ v /w + x − y ↑ z
+t / ∗ uv + w − x ↑ y z
25 / 55
Postorder Traversal Example
26 / 55
Expression Tree Evaluation
t uv ∗ w x y z ↑ − + /+
27 / 55
I
inorder traversal requires parantheses for precedence
I
preorder and postorder traversals do not require parantheses
28 / 55
Postorder Evaluation Example
Regular Trees
t uv ∗ w x y z ↑ − + /+
423 ∗ 1923 ↑ − + /+
4
4
4
4
4
4
7
∗
9 2 3
9 8 −
1 +
/
2 3
6 1
6 1
6 1
6 2
3 +
I
T = (V , E ) is an m-ary tree:
∀v ∈ V [dv o ≤ m]
I
T = (V , E ) is a complete m-ary tree:
∀v ∈ V [dv o ∈ {0, m}]
↑
29 / 55
Regular Tree Theorem
30 / 55
Regular Tree Examples
Theorem
T = (V , E ): complete m-ary tree
I
how many matches are played in a tennis tournament
with 27 players?
I
every player is a leaf: l = 27
n =m·i +1
I
every match is an internal node: m = 2
l = n − i = m · i + 1 − i = (m − 1) · i + 1
I
number of matches: i =
I
n: number of nodes
I
l: number of leaves
I
i: number of internal nodes
I
I
i=
l−1
m−1
=
27−1
2−1
= 26
l −1
m−1
31 / 55
32 / 55
Regular Tree Examples
I
Decision Trees
how many extension cords with 4 outlets are required
to connect 25 computers to a wall socket?
I
every computer is a leaf: l = 25
I
every extension cord is an internal node: m = 4
I
number of cords: i =
l−1
m−1
=
25−1
4−1
I
one of 8 coins is counterfeit (heavier)
I
find counterfeit coin using a beam balance
I
depth of tree: number of weighings
=8
34 / 55
33 / 55
Decision Trees
Decision Trees
35 / 55
36 / 55
Spanning Tree
I
I
Kruskal’s Algorithm
T = (V 0 , E 0 ) is a spanning tree of G (V , E ):
T is a subgraph of G
T is a tree
V0 = V
1. G 0 = (V 0 , E 0 ), V 0 = ∅, E 0 = ∅
minimum spanning tree:
total weight of edges in E 0 is minimal
4. if |E 0 | = |V | − 1: result is G 0
2. select e = (v1 , v2 ) ∈ E − E 0 such that:
E 0 ∪ {e} contains no cycle, and wt(e) is minimal
3. E 0 = E ∪ {e}, V 0 = V 0 ∪ {v1 , v2 }
5. go to step 2
37 / 55
Kruskal’s Algorithm Example
38 / 55
Kruskal’s Algorithm Example
I
minimum weight: 1
(e, g )
I
minimum weight: 2
(d, e), (d, f ), (f , g )
I
E 0 = {(e, g )}
I
E 0 = {(e, g ), (d, f )}
I
|E 0 | = 1
I
|E 0 | = 2
39 / 55
40 / 55
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
I
minimum weight: 2
(d, e), (f , g )
I
E 0 = {(e, g ), (d, f ), (d, e)}
I
|E 0 | = 3
I
minimum weight: 2
(f , g ) forms a cycle
I
minimum weight: 3
(c, e), (c, g ), (d, g )
(d, g ) forms a cycle
I
E 0 = {(e, g ), (d, f ), (d, e), (c, e)}
I
|E 0 | = 4
41 / 55
Kruskal’s Algorithm Example
42 / 55
Kruskal’s Algorithm Example
I
E0 = {
(e, g ), (d, f ), (d, e),
(c, e), (b, e)
}
I
E0 = {
(e, g ), (d, f ), (d, e),
(c, e), (b, e), (a, b)
}
I
|E 0 | = 5
I
|E 0 | = 6
43 / 55
44 / 55
Kruskal’s Algorithm Example
Prim’s Algorithm
1. T 0 = (V 0 , E 0 ), E 0 = ∅, v0 ∈ V , V 0 = {v0 }
2. select v ∈ V − V 0 such that for a node x ∈ V 0
e = (x, v ), E 0 ∪ {e} contains no cycle, and wt(e) is minimal
I
total weight: 17
3. E 0 = E ∪ {e}, V 0 = V 0 ∪ {x}
4. if |V 0 | = |V |: result is T 0
5. go to step 2
45 / 55
Prim’s Algorithm Example
46 / 55
Prim’s Algorithm Example
I
E0 = ∅
I
E 0 = {(a, b)}
I
V 0 = {a}
I
V 0 = {a, b}
I
|V 0 | = 1
I
|V 0 | = 2
47 / 55
48 / 55
Prim’s Algorithm Example
Prim’s Algorithm Example
I
E 0 = {(a, b), (b, e)}
I
E 0 = {(a, b), (b, e), (e, g )}
I
V 0 = {a, b, e}
I
V 0 = {a, b, e, g }
I
|V 0 | = 3
I
|V 0 | = 4
49 / 55
Prim’s Algorithm Example
50 / 55
Prim’s Algorithm Example
I
E 0 = {(a, b), (b, e), (e, g ), (d, e)}
I
V 0 = {a, b, e, g , d}
I
|V 0 | = 5
51 / 55
I
E0 = {
(a, b), (b, e), (e, g ),
(d, e), (f , g )
}
I
V 0 = {a, b, e, g , d, f }
I
|V 0 | = 6
52 / 55
Prim’s Algorithm Example
Prim’s Algorithm Example
I
E0 = {
(a, b), (b, e), (e, g ),
(d, e), (f , g ), (c, g )
}
I
V 0 = {a, b, e, g , d, f , c}
I
|V 0 | = 7
I
53 / 55
References
Required Reading: Grimaldi
I
Chapter 12: Trees
I
I
I
12.1. Definitions and Examples
12.2. Rooted Trees
Chapter 13: Optimization and Matching
I
13.2. Minimal Spanning Trees:
The Algorithms of Kruskal and Prim
55 / 55
total weight: 17
54 / 55
© Copyright 2026 Paperzz