Professional Documents
Culture Documents
Trees
Objectives
10.1 Introduction to Trees
10.2 Applications of Trees
10.3 Tree Traversal
10.4 Spanning Trees
10.5 Minimum Spanning Trees
10.1 Introduction to Trees
10.1 Introduction to Trees
10.1 Introduction to Trees
THEOREM 1:
An undirected graph is a tree if and only if there is a unique simple
path between any two of its vertices.
DEFINITION 2:
A rooted tree is a tree in which one vertex has been designated
as the root and every edge is directed away from the root.
Some terminologies
Parent, Child, Siblings, Ancestors, Descendants, Leaf (Vertices that
have no children), internal vertices (Vertices that have children),
Subtree.
Ex:
the parent of c: b
the children of g: h, i, j
the siblings of h: i, j
all ancestors of e: c, b, a
all descendants of b: c, d, e
all internal vertices: a, b, g, c, h, j
all leaves: d, e, f, i, k, l, m
the subtree rooted at g:
10.1 Introduction to Trees
DEFINITION 3:
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.
Ex:
m=3
i=4
n = 3.4+1 = 13 vertices.
n - 1 = 12 edges
m=5
i=3
n = 3.5+1 = 16 vertices.
n - 1 = 15 edges
Properties of Trees
THEOREM 4: A full m-ary tree with
(i) n vertices has i n 1 / m internal vertices and l m 1 n 1 / m leaves,
(ii) i internal vertices has n m.i 1 vertices and l m 1 i 1 leaves,
(iii) l leaves has n ml 1 / m 1 vertices and i l 1 / m 1 internal vertices.
Ex: 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
10.1 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.
Ex: Level
0
4 (height)
10.1 Introduction to Trees
THEOREM 5:
There are at most
h mh leaves in an m-ary tree of height h.
There are at most m leaves in an m-ary tree of height h.
Proof: page 754
COROLLARY 1:
hash.l leaves, then h log ml .
If an m-ary tree of heightofhheight
If the m-ary tree is full and balanced, then h log ml .
Ex:
6
2 7
9
1 4
5 8
3
Constructing a Binary Search Tree
Ex1: Construct a binary search tree for numbers: 23, 16, 43, 5, 9,
1, 6, 2, 33, 27.
23
16 43
5 33
1 9 27
2 6
Constructing a Binary Search Tree
Ex2: Form a binary search tree for the words mathematics, physics, geography,
zoology, meteorology, geology, psychology, and chemistry (using alphabetical
order).
Algorithm for inserting an element to BST
Complexity: O(logn)
Proof: page 759
Decision Trees
Ex: Suppose there are seven coins, all with the same weight, and a counterfeit
coin that weighs less than the others. How many weighings are necessary
using a balance scale to determine which of the eight coins is the counterfeit
one? Give an algorithm for finding this counterfeit coin.
a, b, e, f, c, d, g, h, i
b, e, j, k, n, o, p, f, m ?
Inorder Traversal
e, b, f, a, c, g, d, h, i
j, e, n, k, o, p, b, f, m
?
Postorder Traversal
e, f, b, c, g, h, i, d, a
j, n, o, p, k, e, m, f, b ?
Traverse
Algorithms
Example
In which order are the vertices of the ordered rooted
tree visited using:
a) A preorder traversal?
a, b, e, j , k , n, o, p, f , c, d , g , l , m, h, i
b) An inorder traversal?
j , e, n, k , o, p, b, f , a, c, l , g , m, d , h, i
c) A postorder traversal?
j , n, o, p, k , e, f , b, c, l , m, g , h, i, d , a
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+
Prefix form
Postfix form
Ex1: Find the value of
the prefix expression:
+-* 2 35/ 23 4
Prefix form:
operator(operand_1,operand_2)
Ex2: Find the value of
the postfix expression:
723*-49 3/+
Postfix form:
(operand_1,operand_2)operator
Example
Find the ordered rooted tree representing the compound proposition
(¬(p ∧ q)) ↔ (¬p ∨ ¬q).
Then use this rooted tree to find the prefix, postfix, and infix forms of this
expression.
Example
Find the ordered rooted tree representing the compound proposition
(¬(p ∧ q)) ↔ (¬p ∨ ¬q).
Then use this rooted tree to find the prefix, postfix, and infix forms of this
expression.
f
g d
h e
k i c
j b a
Depth-First Search
Breadth-First Search
EX: Use breadth-first search to find a spanning tree for the graph
shown in Figure 9.
e
b d f i
a c g j k
h
l m
Breadth-First Search
Backtracking Applications
Graph Colorings
Then-Queens Problem
Sums of Subsets
Graph Colorings - example
The n-Queens Problem
10.5 Minimum Spanning Trees
Which links should be made to ensure that there is a path between any
two computer centers so that the total cost of the network is minimized?
minimum spanning tree: a spanning tree that has the smallest
possible sum of weights of its edges.
Algorithms for Minimum Spanning Trees
Prim’s algorithm (1957 by Robert Prim) (originally discovered by
Vojtech Jarník in1930).
Choosing any edge with smallest weight, putting it into the
spanning tree.
Add to the tree edges of minimum weight that are incident to a
vertex already in the tree, never forming a simple circuit with
those edges already in the tree.
Stop when n−1 edges have been added.
Use Prim’s algorithm to design a minimum-cost communications
network connecting all the computers represented by the graph in
Figure 1.
Use Prim’s algorithm to find a minimum spanning tree in the graph
shown in Figure 3.
Kruskal’s algorithm
(by Joseph Kruskal in 1956)
Choose an edge in the graph with minimum weight.
Add edges with minimum weight that do not form a simple
circuit with those edges already chosen.
Stop after n−1 edges have been selected.
Use Kruskal’s algorithm to find a minimum spanning tree in the
weighted graph shown in Figure 3.
Summary
Introduction to Trees
Applications of Trees
Tree Traversal
Spanning Trees
Minimum Spanning Trees
Thanks