10 Trees
10 Trees
Trees
Objectives
⚫ 10.1- Introduction to Trees
⚫ 10.2- Applications of Trees
⚫ 10.3- Tree Traversal
10.1- Introduction to Trees
Introduction to Trees…
Introduction to Trees
circuit disconnected
Introduction to Trees…
Proof: page 684
Suppose that 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 (the reader
should show that such a vertex is unique). When u is the
parent of v, v is called a child of u. 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 (that is, its parent, its parent’s parent, and so on, until
the root is reached). The descendants of a vertex v are
those vertices that have v as an ancestor. A vertex of a
rooted tree is called a leaf if it has no children. Vertices
that have children are called internal vertices. The root is
an internal vertex unless it is the only vertex in the graph,
in which case it is a leaf.
Introduction to Trees…
Subtree
Root node (vertex)
Internal node
Leaf
Parent
Child
Siblings
Descendants
Ancestors
Introduction to Trees…
A Organizational Tree
Some Tree Models
m=3
i=4
n= 3.4+1=13
m=5
i=3
n= 3.5+1=16
Introduction to Trees…
m=3
n=13
i=12/3= 4
l= [(3-1)13+1]/3=9
m=5
n=16
i= 15/5=3
l= (4.16+1)/5= 13
n −1
n = mi + 1 i =
m
n =l +i
l + i = mi + 1
(m − 1)( n − 1)
l = (m − 1)i + 1 = +1
m
Introduction to Trees…
⚫ Level of a vertex: The length of the path from the
root to this vertex.
⚫ Height of Tree: The maximum of levels of vertices =
The length of the longest path from the root to any
vertex.
Introduction to Trees…
A m-ary tree of height h is called balanced if all
leaves are at levels h or h-1.
Construct a binary search tree for numbers: 23, 16, 43, 5, 9, 1, 6, 2, 33, 27.
Algorithm for inserting an element to BST
Complexity: O(logn)
Proof: page 698
Decision Trees
a, b, e, f, c, d, g, h, i
?
Inorder Traversal
e, b, f, a, c, g, d, h, i
?
Postorder Traversal
e, f, b, c, g, h, i, d, a
?
Traverse Algorithms
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation
⚫ Infix form:
operand_1 operator operand_2 x+y
⚫ Prefix form:
operator(operand_1,operand_2) +xy
⚫ Postfix form:
(operand_1,operand_2)operator xy+
⚫ How to find prefix and postfix form from infix
form?
(1) Draw expression tree.
(2) Using Preorder traverse → Prefix form
Using Postorder traverse → Postfix form
Infix, Prefix, and Postfix Notation
Infix form
Prefix form
Postfix form
x+y 2+x–4/3
Infix, Prefix, and Postfix Notation
10.4- Spanning Trees
DEFINITION 1: 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
10.4- Spanning Trees
THEOREM 1 : A simple graph is connected if and only if it has a
spanning tree.
10.4.2 Depth-First Search
11.4.3 Breadth-First Search