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
Introduction to Trees…
Introduction to Trees
circuit disconnected
Introduction to Trees…
Proof: page 684
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
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 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
?
Preorder traversal - example
a
Pre-order traversal: N L R •
• • •
a
• • • b c d
•
b c d • • • •
• • e f g h
• •
e f g h
a b e f c d g h
38
Inorder Traversal
e, b, f, a, c, g, d, h, i
?
Inorder traversal - example
a
•
in-order traversal: L N R
• • •
b c d
a
• • • • • • • •
b c d e f g h
• • • •
e f g h
e b f a c g d h
40
Postorder Traversal
e, f, b, c, g, h, i, d, a
?
Postorder traversals -
examples
a
post-order traversal: L R N •
• • •
a
• • • b c d
•
b c d • • • •
• • e f g h
• •
e f g h
e f b c g h d a
42
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
Infix, Prefix, and Postfix Notation
Spanning trees
⚫ Introduction
48
Spanning trees
Definition.
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.
49
Remov EdgesThat Form Simple Circuits.
50
THEOREM 1
A simple graph is connected if and only if it
has a spanning tree.
51
Depth-First Search
52
The edges selected by
depth-first search of a
graph are called tree
edges.
All other edges are
called back edges.
53
54
Breadth-First Search
55
56
Backtracking Applications
⚫ Graph Colorings
⚫ Then-Queens Problem
⚫ Sums of Subsets
57
Graph Colorings - example
58
The n-Queens Problem
59
Minimum Spanning Trees
60
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.
61
62
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.
63
64
Summary
⚫ Introduction to Trees
⚫ Applications of Trees
⚫ Tree Traversal
⚫ Spanning Trees
⚫ Minimum Spanning Trees
⚫ Thanks